How to Create a New File in Linux from Bash?

LinuxOpen SourceMobile Operating System

Before getting into the ways of creating a file using Bash, let's first understand how Linux treats its files. Linux organizes all its data into files and files are organized into directories. Further, the directories are organized into tree-like structures called the filesystem. When you have to work in a Linux environment, you would definitely have to spend a lot of your time working on different types of files.

There are various ways in which one can create a file in Linux. You can create a file from the Bash Shell or you can use the 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 frequently used command for creating a new file in Linux. To create a new file, you need to run the touch command followed by the name of the file. For example,

$ touch hello.txt

It will create an empty file called "hello.txt" in the current directory. Use the "ls" command to verify if the file has been created or not.

Using the "cat" command

Normally we 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 redirection operator ">" followed by the name of the file. Now you will 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 called "secondFile.txt" and save it with the content "Welcome to Tutorialspoint".

Again, use the "ls" command to verify if the new file has been created or not.

$ ls
hello.txt newdirectory secondFile.txt

Next, use the "cat" command to see the contents of "secondFile.txt".

$ cat secondFile.txt
Welcome to Tutorialspoint!

Using the Redirection Operator

You can simply use the redirection operator ">" to create a new blank file in the current directory. Run the ">" operator followed by the name of the file.

$ > thirdFile.txt

Now use the "ls" command again to verify −

$ 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 display nothing because the file is now empty.

You can use the redirection operator ">>" to append the contents of a file into 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 append the contents of "secondFile.txt" at 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 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" > fourthFile.txt

The above command will create a new file (or overwrite the file if it already exists) with the string passed as the argument to "echo". Verify using the "cat" command −

$ cat fourthFile.txt
This is the Fourth File

If you simply want to create a blank new file, use the "echo" command without any argument −

$ echo > fourthFile.txt

Using the "printf" command

The "printf" command works just like 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 the argument.

The following "printf" command redirects the input formatted string into a new file "fifthFile.txt". If the file already exists, then it will overwrite its contents.

$ printf "First Line.\nSecond Line.\n" > fifthFile.txt
$ cat fifthFile.txt
First Line.
Second Line.
raja
Updated on 12-Apr-2022 13:23:16

Advertisements