- 4 Ways to Create a Text File in Linux Terminal
- Create file in Linux command line
- 1. Create an empty file using touch command
- 2. Create files using cat command
- 3. Create new file using echo command
- 4. Create a new file using a text editor like Nano or Vim
- 7 Ways to Create a File in Linux Terminal
- 1) Create a file with touch command
- 2) Create a file with cat command
- 3) Create a file with echo command
- 4) Create a file with printf command
- 5) Create a file with nano text editor
- 6) Create a file with vi text editor
- 7) Create a file with vim text editor
- Conclusion
- How to Create a File in Linux Using Terminal/Command Line
- Creating New Linux Files from Command Line
- Create a File with Touch Command
- Create a New File With the Redirect Operator
- Create File with cat Command
- Create File with echo Command
- Create File with printf Command
- Using Text Editors to Create a Linux File
- Vi Text Editor
- Vim Text Editor
- Nano Text Editor
- Creating and Removing Files and Directories Under Linux
- Creating Files
- Creating Directories
- Removing Files
- Removing Directories
4 Ways to Create a Text File in Linux Terminal
In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.
If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.
Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.
Create file in Linux command line
There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.
1. Create an empty file using touch command
One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.
If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.
2. Create files using cat command
Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.
You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.
If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.
3. Create new file using echo command
The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.
To create a new empty file using echo you can use something like this:
The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.
You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:
This will create a new file with just one empty line. You can check the number of lines with wc command.
4. Create a new file using a text editor like Nano or Vim
The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.
Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.
Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:
What’s your favorite command?
So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.
Which command do you prefer here? Please share your views in the comment section below.
Источник
7 Ways to Create a File in Linux Terminal
In this tutorial, I will show you how to create a file from a Linux terminal. There are many text editors like (vim, nano, vi) and many commands like (cat, echo, printf, touch) to create a file in the Linux operating system via command line. Here will explain the following linux tools.
1) Create a file with touch command
We will use touch command with any extension to create file, this command will create an empty file touch.txt in your current directory as an example below.
To see the file type command below.
2) Create a file with cat command
We will use cat command to create file, this command will create an empty file cat.txt in your current directory as an example below, but you must add text in the file.
Add the text below.
To save the file hit Ctrl + d , and to see the file type command below.
To open the file, we will use cat command to open it.
3) Create a file with echo command
We will use echo command to create file, this command will create a file echo.txt in your current directory as an example below, but you should add text in the line command.
To see the file,type command below.
To open the file, we will use cat command to open it.
4) Create a file with printf command
We will use printf command to create file, this command will create a file printf.txt in your current directory as an example below, but you should add text in the line command.
To see the file type command below.
To open the file, we will use cat command to open it.
5) Create a file with nano text editor
To create a file using nano text editor, first install it, after that type command below and the text editor will be opened to adding text.
Add the text below.
To save the file type Ctrl + x and type y , to see the file type command below.
To open the file, We will use nano command to open it.
6) Create a file with vi text editor
To create a file using vi text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.
Add the text below.
To save the file and exit hit Esc after that :wq , To see the file type command below.
To open the file, we will use vi command to open it.
7) Create a file with vim text editor
To create a file using vim text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.
Add the text below.
To save the file and exit hit Esc after that :wq , to see the file type command below.
To open the file, we will use vim command to open it.
Conclusion
In this tutorial, we learned the different ways to create a file from Linux terminal. Hope you enjoyed reading and please leave your comments in the below comment section.
Источник
How to Create a File in Linux Using Terminal/Command Line
Home » SysAdmin » How to Create a File in Linux Using Terminal/Command Line
Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.
In this tutorial learn how to to create a file from a Linux terminal.
- Access to a command line/terminal window (Ctrl–Alt–F2 or Ctrl–Alt–T)
- A user account with sudo privileges (optional for some files/directories)
Creating New Linux Files from Command Line
Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.
Here are a few commands for creating a file directly from the command line.
Create a File with Touch Command
The easiest way to create a new file in Linux is by using the touch command.
In a terminal window, enter the following:
This creates a new empty file named test.txt. You can see it by entering:
The ls command lists the contents of the current directory. Since no other directory was specified, the touch command created the file in the current directory.
If there’s already a file with the name you chose, the touch command will update the timestamp.
Create a New File With the Redirect Operator
A redirection operator is a name for a character that changes the destination where the results are displayed.
Right angle bracket >
This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:
This creates a new empty file.
Use the ls command to list the contents of the current directory and find the file test2.txt.
Create File with cat Command
The cat command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.
To create an empty file using cat , enter the following:
Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.
Verify that the file was created:
The system should now have test.txt, test2.txt, and test3.txt in the list.
Create File with echo Command
The echo command will duplicate whatever you specify in the command, and put the copy into a file.
Enter the following:
Verify that the file was created:
You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file:
The system should display Random sample text (or whatever you entered with the echo command.)
Create File with printf Command
The printf command works like the echo command, and it adds some formatting functionality. To add a single line of text, enter:
To add two lines of text, separate each line with the \n option:
You can use the cat command on either of these files to display their contents.
Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.
Using Text Editors to Create a Linux File
All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.
Vi Text Editor
Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.
To create a file using Vi, enter the following:
Your screen will change. Now you’re in the text editor. Press the letter i to switch to insert mode, then type a few words to try it out.
To save and exit press Esc 😡 and hit Enter .
Vim Text Editor
You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.
Use vim to create a new text file:
This screen will look similar to the Vi editor screen. Press i to insert text, and type a few words. Save file and exit by entering:
(Escape, colon wq, then Enter.)
Nano Text Editor
Nano is a newer and much easier text editor to navigate.
Create a new file by entering the command:
By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.
Enter some text, then press Ctrl+O to save the changes.
Press Ctrl+X to exit the editor.
Note: Learn all you need about Nano in the Install and Use Nano in Linux article.
Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.
Источник
Creating and Removing Files and Directories Under Linux
Here is an absolute beginner post on creating and removing Files and Directories under Linux.
Creating Files
The touch command creates a new empty file.
You can create multiple files with the same command. If the file name or directory name already exists, the touch command updates the modification time and access time to the current date and time. You can use absolute or relative path names on the command line when creating new files.
To create an empty file named file1 in the /tmp directory, enter the following commands:
To create multiple empty files use the touch commands with the file names in one line as shown below.
Creating Directories
The mkdir command creates new directories.
Include the –p option if the directory name includes a path name. The command used with the -p option creates all of the non-existing parent directories that do not yet exist in the path to the new directory. You can use absolute or relative path names on the command line when creating new directories.
For example, create a new directory, named dir1, within the /tmp directory.
You can use the command ‘ls -ld’ to view the created directory.
To create a new directory named dir_in located inside a directory named dir_out, use the mkdir command with the -p option. The dir_out directory does not yet exist.
To create the dir1, dir2, and dir3 directories, enter the mkdir command with all the directory names in one line as shown below.
Removing Files
You can permanently remove files from the directory hierarchy with the rm command.
The rm command is a destructive command if not used with the correct option. The table describes the options that you can use with the rm command when removing files and directories.
Option | Description |
---|---|
-r | Includes the contents of a directory and the contents of all subdirectories when you remove a directory |
-i | Prevents the accidental removal of existing files or directories |
The –r option allows you to remove directories that contain files and subdirectories. The -i option prompts you for confirmation before removing any file.
– A yes response completes the removal of the file.
– A no response aborts the removal of the file.
For example, remove the file named file1 from the /tmp directory.
Lets see an example of using the -i option to delete the files.
Removing Directories
You can use the rm command with the -r option to remove directories that contain files and subdirectories.
For example, remove the dir1 directory and its content by using the rm –r command.
If you do not use the -r option with the rm command while removing directories, the following error message appears:
To interactively remove a directory and its contents, use the –i option along with the rm –r command. For example,
The rmdir command removes empty directories.
For example to remove the empty directory dir3, use the command below.
To remove a directory in which you are currently working in, you must first change to its parent directory.
Источник