Signup/Sign In
PUBLISHED ON: MARCH 26, 2023

Different ways to create a file in Linux using terminal

Creating a file in Linux is a simple and straightforward process, and it is an essential task for many Linux users, especially developers and system administrators. Files are used to store data, code, and configuration settings that are used by different applications and services. Therefore, knowing how to create and manage files is an important skill for anyone working with Linux.

Because Linux files are case-sensitive, test.txt and Test.txt will be treated as separate files.

Let's take a closer look at the approaches listed below:

To begin, make a directory named New directory and run the mkdir command as follows:

mkdir New_directory 

Change to the following directory:

cd New_directory 

Output:

1

1. using the cat command

The cat command is one of the most commonly used in Linux. It may be used to make a file, show its contents, concatenate the contents of several files, display line numbers, and more.

In this section, we'll look at how to use the cat command to create files and add content to them.

Now use the cat command to create a file:

cat > test.txt 

The preceding command will create a text file and launch the stdin mode. Enter the necessary text and press CTRL + D to save and quit the file, returning to the command line.

Output:

2

2. Making use of the touch command

The touch command is also a common command in Linux. It is used to create new files and directories, as well as to update the time stamp on existing files and folders. By running this command numerous times, we may generate multiple files.

To create a file, type touch followed by the file name as seen below:

touch test1.txt 

Output:

3

Specify the files and their extensions following the touch command, followed by a single space. To create three files at once, run the following command:

touch test1.txt test2.txt test3.txt  

Execute the following command to generate two distinct sorts of files:

touch test4.txt test.odt 

Output:

4

3. Making use of the reroute (>) sign

We may also use the redirect symbol (>) on the command line to create a file. To make a file, simply put a redirect symbol (>) followed by the file name. This symbol is often used to reroute output. There are two methods for redirecting the output. The > operator will overwrite the existing file, whereas the >> operator will append the output.

To create a file with the redirect (>) operator, run the following command:

> test5.txt 

Output:

5

4. Use of the echo command

The echo command is used to create a file, however the file content must be specified on the command line.

To create the file with the echo command, use the following command:

echo " File content" > test6.txt 

Output:

6

5. Using printf command

We may also use the printf command to generate a file. On the command line, we must specify the file content.

To use the printf command to generate a file, use the following:

printf " File content" > test7.txt

Output:

7

6.Using Text Editor

We may also create a file using text editors such as vim, nano, vi, and others.Using the text editor Vim
Using the Vim text editor, we may create a file. If you do not already have the vim editor installed on your PC, run the following command:

sudo apt install vim -y

9

The following command will launch the text editor; press the I key to enter the editor's insert mode. Enter the file's content, then hit Esc followed by :wq to save and leave the file. The text editor looks like this:

Output:

11

Output:

8

Conclusion

Creating a file in Linux is an essential task that is easy to accomplish using various methods. The process may vary depending on the specific needs of the user, but the fundamental principles remain the same.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.