Creating files using the command line is an essential skill for Linux users. There are several methods to create a file in Linux quickly. We will be creating text (.txt) files as examples.
Note: Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.
Using the touch Command
The touch command is, arguably, the easiest command to create a new empty file.
To create a file with the touch command, from the command line, enter:
# touch newfile.txt
Where newfile.txt is the name of the file to be created.
After we create the command, we can verify its existence with the ls -al command.
# ls -al
Note that you can create multiple files at once using the touch command.
# touch newfile1.txt newfile2.txt newfile3.txt
Using the Redirect Operator (>)
The redirect operator, or right angle bracket, > allows Linux users to create a new empty file quickly.
# > newfile2.txt
Verify the file was created with the ls -al command.
# ls -al
Note that you can use the redirect operator, >, with other commands to create a new file with the results of a command. For example
# ls -al > directorylisting.txt
will create a new file, directorylisting.txt, to output, or ‘redirect’, the directory listing to directorylisting.txt rather than the screen.
Using the cat Command
Although the cat command is primarily used to read and concatenate files, it can also be used to create a new file. To use the cat command to create a new file, enter cat, followed by the redirection operator, >, and the name of the file you want to create. Press <Enter> and type the text you want (press <Enter> for new line). Finally, press <Ctrl><D> to save the file.
# cat > newfile3.txt
This is the first line.
This is the second line.
This is the third line.
Verify that the file was created:
# ls -al
Using the echo Command
The echo command outputs strings passed as arguments to the standard outputs that can be forwarded to a new file using the redirection operator, >. For example:
echo "Let's create a new file." > newfile4.txt
Use the cat command to display the new file’s content.
# cat newfile4.txt
Note that you can use the redirect operator twice, >>, to append text to the file.
echo "We've create a new file!" >> newfile4.txt
Use the cat command to display the new file’s content.
# cat newfile4.txt
Additionally, the echo command with the redirection operator, >, and no text to create an empty file.
# echo > newfile5.txt
Using the printf Command
The printf command works almost identical to the echo command but has added formatting functionality. In this example, we will create a new file, newfile5.txt The file has content. . Use the ls -al command to verify the file, and list the file’s contents.
# printf 'The printf command is similar to the echo command. But has additional formating functions.n' > newfile5.txt
# ls -al
# cat newfile5.txt
Note the “n” in the command above. This signals printf to start a new line.
Like the echo command, with the printf command, you can use double redirect operators, >>, append text to the file.
# printf 'I told you we could add a new line!n' >> newfile5.txt
# cat newfile5.txt