Before we get into the ways to create a file using Bash, let’s first understand how Linux treats your files. Linux organizes all your data into files, and files are organized into directories. In addition, directories are organized into tree-like structures called file systems. When you have to work in a Linux environment, you would definitely have to spend a lot of time working on different file types.
There are several ways in which one can create a file on Linux. You can create a file from Bash Shell or you can use Desktop File Manager to do so. In this article, we will focus on different Shell commands that you can use to create a file.
Using
the “touch” command
The touch command is by far the most commonly used command to create a new file in Linux. To create a new file, you must run the touch command followed by the file name. For example,
$ touch hello.txt
will create an empty file named “hello.txt” in the current directory. Use the “ls” command to check if the file has been created or not.
Using
the “cat” command We normally use the “
cat” command to read the contents of a file; however, we can also use this command to create a new file. Let’s see how.
To create a new file, run the “cat” command and then use the “>” redirect operator followed by the file name. You will now be prompted to insert data into this newly created file. Type a line and then press “Ctrl + D” to save the file.
$ cat > secondFile.txt Welcome to Tutorialspoint!
The above command will create a new file named “secondFile.txt” and save it with the content “Welcome to Tutorialspoint”.
Again, use the “ls” command to check if the new file has been created or not.
$ ls hello.txt newdirectory secondFile.txt Then use the “cat”
command to view the contents of “secondFile.txt”. $ cat secondFile.txt Welcome to
Tutorialspoint!
Using
the Redirect Operator
You can simply use the “>” redirect operator to create a new blank file in the current directory. Run the “>” operator followed by the file name.
$ > thirdFile.txt Now use the “ls” command
again to check −$ ls
hello.txt newdirectory secondFile.txt thirdFile.txt
Note that the “>” operator overwrites the contents of a file if it is already present. For example, the following command will overwrite the contents of “secondFile.txt” because the file already exists and we know it contains the line “Welcome to Tutorialspoint!”
$ > secondFile.txt Now use the “cat” command to check the contents of “secondFile.txt
“. $ cat secondFile.txt
It will not show anything because the file is now empty
.
You can use the “>>” redirect operator to append the contents of one file to another. For example,
$ cat hello.txt This is the first file. $ cat secondFile.txt This is the second file.
Now we can use the following command to add the contents of “secondFile.txt” to the end of “hello.txt”.
$ cat secondFile.txt >> hello.txt $ cat hello.txt This is the first file. This is the second file.
Using the
“echo” command
The “echo” command takes a string as an argument and displays it as output. For example
,$ echo “This is the fourth file” This is the fourth file
We can redirect this output
to a new file, such as −$ echo “This is the fourth file” > fourth file.txt
The above command will create a new file (or overwrite the file if it already exists) with the string passed as an argument to “echo”. Verify using
the command “cat” −$ cat fourthFile.txt This is the fourth file If you
simply want to create a new blank file,
use the “echo” command without any arguments −$ echo > fourthFile.txt Using the “printf” command The “printf” command works the same as the “echo” command with the
only exception that the “printf” command provides additional formatting options that you can use to pass a formatted string as an argument.
The following command “printf” redirects the input format string to a new “fifthFile.txt” file. If the file already exists, it will overwrite its contents.
$ printf “First line. Second line.” > fifthFile.txt$ cat fifthFile.txt first line. Second line.