Different Ways To Create a File in Linux Command Line

In this guide, we want to teach you to some Different Ways To Create a File in the Linux Command Line. Creating files in Linux is straightforward. You can easily use various Linux commands to create a file. In this article, you can follow the steps and see how you can create an empty file, create a file and open it, create a file, and write something to it. Let’s see how Linux Commands work here.

Different Ways To Create a File in Linux Command Line

At this point, you must access your server as a root or non-root user with sudo privileges. You can visit the Orcacore website and search for the initial server setup guides for different Linux distros.

Now follow the steps below to use different Linux commands for creating a file.

Step 1 – Create an Empty File in the Linux Command Line

We want to start with creating an empty file in Linux. Here we will show you different commands to do this.

Touch Command

The first Linux command line utility is the touch command. You can use this to create an empty file in your Linux server. For example:

touch file.txt

In this way, you can easily create empty files. If you use the cat command to show you the file info, it doesn’t show anything because it is an empty file.

Echo Command

Another command that you can use to create an empty file is a popular command named echo command. It can be used for various purposes, but you can use the following syntax to create an empty file. For example:

echo -n > file2.txt

By using the -n > option you can tell your system to create the empty file.

Greater Than Operator “>”

In this way, you can simply use the “>” option to create your empty files. This will work like this:

> file3.txt

You can verify that your file has been created by using the ls command.

Printf Command

Also, you can use another command line utility named Printf command to create empty files. You just need to use two single quotes that represent the empty files. For example:

printf '' > file4.txt

You can use the ls command to verify your file has been created.

Step 2 – Create an Empty File and Open it in the Linux Command Line

At this point, if you plan to create a file and open it, you can easily use the text editors like Nano, Vi, and Vim. When you use these editors, they look for the file, if it exists, they will open it, but if it doesn’t exist, they will create it and then open it.

For example, we use the Vi Editor to create a Python file:

vi app.py

This will open a file as follows:

Create a file with Vi editor in Linux

Because we want to create an empty file, we don’t write anything to it. To exit from the Vi editor, you can use the :q!.

Note: If you don’t write anything to the file, the new file will not created. You can check it by using the ls and cat commands.

Step 3 – Create a File and Write Into It in the Linux Command Line

Now you can use different Linux commands to create a file and write into it. Like creating empty files with echo and print commands, you can use these commands to write to the files.

Write into files with Echo and Printf Linux Commands

You can easily use the echo command to create a file and write to it. For example, we create a text file and write a sample text to it with the following syntax:

echo 'This is a test text file from orcacore' > file5.txt

You can verify your file with the following command:

cat file5.txt
Output
This is a test text file from orcacore

Also, you can create and write to the file with the printf command same as the echo command:

printf 'This is a test text file from orcacore' > file6.txt

Write into files with cat Linux Command

At this point, you can use the cat command to create a file and write to it by using the greater than icon:

cat > file7.txt

From there you see the new line from your output that you can add your contents to the file.

orca@deb:~# cat > file7.txt
This is a test file
^C

Then, you can verify your text file by using the cat command, you will see your content added to the file.

Step 4 – Combine Multiple files into one file in Linux

At this point, if you want to combine multiple file contents into a new file, you can use the cat command to do this. For example, we create two files and write to them with the following commands:

# echo 'testcontent1' > f1
# echo 'testcontent2' > f2

Now we want to combine the contents of the f1 and f2 files into a new file named f3. To do this, we use the following cat command:

cat f1 f2 > f3

When verifying the f3 file, we should see the contents have been added to the file:

cat f3

Output
testcontent1
testcontent2

Also, you can use the mv command to rename or relocate the file contents. If the destination file doesn’t exist the mv command will create it. For example, we want to move the f3 file to a new file with the command below:

mv f3 file8.txt

When verifying the new file, we should see the contents have been added to the file:

cat file8.txt
Output
testcontent1
testcontent2

That’s it. You are done.

Conclusion

At this point, you have learned to use Different Linux Commands to create files, write into them, and combine multiple files into a new file. Every Linux user must know these amazing commands and the usage of them.

Hope you enjoy using it. You may be interested in these articles:

Uninstall and Remove the PPA Repository From Ubuntu and Debian

Using Fasd in Linux for Quick Access to Files & Directories

Tail Command in Linux for Logs

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!