Linux Create File
To create a file in Linux from the Terminal, you can use commands such as touch or shell output redirection with >. You can also create a text file and add content immediately with commands such as printf or cat.
The mkdir command is used to create directories, not regular files. For an empty file, touch is usually the simplest choice. If you use > filename, the shell creates the file when it does not already exist, but it truncates an existing file to zero bytes.
In this tutorial, we first create an empty file with touch, then create a file using the > redirection operator. We also cover creating text files with content, creating a file in a specific directory, and checking the resulting file.
Linux – Create File using touch command
The touch command can create an empty file if the specified file does not already exist. If the file already exists, touch normally updates its access and modification timestamps instead of deleting its contents.
Following is a step by step process to create a file in Linux using touch command.
Step 1: Open a Terminal. You may use the keyboard and type Ctrl+Alt+T, or open Applications, and search for Terminal.
And navigate to the directory in which you would like to create the file. In this example, we shall create a file named mynotes.txt in workspace directory.
)
Step 2: Run the touch command, followed by file name.
$ touch mynotes.txt
)
The file is successfully created. Because no content was supplied, a newly created mynotes.txt file is empty.
Step 3: We can cross check if the file is created, by using ls command. Run the ls command, and you would see the file in the list, as shown below.
)
You can also use ls -l when you want to see details such as the file size, permissions, owner, and modification time.
ls -l mynotes.txt
Linux – Create File using “Redirecting Standard Output” Command
Following is a step by step process to create a new file in Linux via Terminal using >. The > symbol is the shell’s standard-output redirection operator. When it is used without a command before it, the shell can create an empty file.
Important: if the target file already exists, > filename truncates it and removes its existing contents. Use this method only when creating a new empty file or when you intentionally want to empty an existing file.
Step 1: Open a terminal and navigate to the directory in which you would like to create a new file.
)
Step 2: Run Redirecting Standard Output > command followed by file name.
> mygroceries.txt
)
The new file is successfully created.
Step 3: To check if the file is created, run list command ls. The new file mygroceries.txt should be listed as shown in the following Terminal.
)
Create a Text File in Linux with Content
If you want the new file to contain text immediately, redirect the output of a command into the file. For example, printf can create notes.txt and write one line into it.
printf '%s\n' 'Remember to update the server' > notes.txt
To display the contents of the new text file, use cat.
cat notes.txt
Remember to update the server
The single > operator replaces existing file contents. If you need to add output to the end of an existing file instead, use >>.
printf '%s\n' 'Check the backup' >> notes.txt
Create a Multi-line Text File with cat in Linux
For several lines of text, you can combine cat with a here-document. Everything between the two EOF markers is written to the file.
cat > tasks.txt <<'EOF'
Check disk space
Install updates
Restart the service
EOF
As with other uses of >, this command overwrites tasks.txt if that file already exists.
Create and Edit a File with nano in Linux
If you want to create a text file interactively and edit it at the same time, open a filename with a terminal text editor such as nano. If the file does not exist, Nano starts with a new buffer that can be saved under that filename.
nano notes.txt
Enter the required text, save the file, and exit the editor. Nano may not be installed by default on every Linux distribution, so touch and shell redirection remain useful methods that do not require a graphical editor.
Create a File in a Specific Linux Directory
You do not have to change directories before creating a file. Supply a relative or absolute path with the filename. For example, the following command creates report.txt inside an existing documents directory under your home directory.
touch ~/documents/report.txt
The parent directory must already exist. If it does not, create the directory first with mkdir, and then create the file.
mkdir -p ~/documents
touch ~/documents/report.txt
You also need write permission for the destination directory. If Linux reports Permission denied, check the directory path and permissions rather than repeatedly running the file-creation command.
Linux touch vs > for Creating Files
| Method | When the file does not exist | When the file already exists |
|---|---|---|
touch file.txt | Creates an empty file | Updates file timestamps without normally changing its contents |
> file.txt | Creates an empty file | Truncates the file to zero bytes |
command > file.txt | Creates the file and writes command output | Replaces the existing contents |
command >> file.txt | Creates the file and writes command output | Appends output to the end |
Check Whether a Linux File Was Created
For a quick visual check, use ls or ls -l. In shell scripts, the test command can check whether a path exists as a regular file.
test -f mynotes.txt && echo 'File exists'
File exists
Remember that Linux filenames are case-sensitive on typical Linux file systems. For example, Notes.txt and notes.txt can refer to different files.
Choosing the Right Linux File Creation Command
Use touch filename when you need a new empty file without risking removal of the contents of an existing file. Use > filename when an empty file is required and truncating an existing file is acceptable. Use output redirection with printf, cat, or another command when you want to create the file with content immediately.
Conclusion
Concluding this Linux Tutorial, we learned how to create a new file in Linux Terminal, with the help of commands. We used touch for an empty file, > for shell redirection, and additional commands for creating text files with content and files inside specific directories.
TutorialKart.com