Create New File in The Linux Terminal

by|inArticles||2 min read
Linux Terminal
Linux Terminal

In general, you have three possible ways to create a file from the Linux command terminal. In short, those are the following:

  • touch
  • cat
  • echo

In this article, we will provide examples of how to create files with those commands in the Linux terminal.

Creating a file in the Linux terminal by using the touch command

You can use the "touch" command along with the desired file name and extension to create a file in the Linux terminal. This command will create an empty file with the specified name and extension in the current directory. 

touch sample.txt

Additionally, you can also specify the file path to create it in a specific location within your file system.

touch /home/ersocon/sample.txt

To create multiple files at once, you can simply list the file names and extensions separated by spaces after the touch command. This will create all the specified files in the current directory or the specified location. Remember to use appropriate file extensions to indicate the type of file you are creating, such as .txt for text files or.php for PHP scripts. 

touch sample1.txt sample2.php sample3.py

Creating a file in the Linux terminal by using the cat command

Alternatively, you can use the "cat" command to create a file in the Linux terminal. This command allows you to create a file and simultaneously add content to it by using the ">" symbol followed by the desired file name and extension. This will create a new file with the specified name and extension, and any subsequent text entered will be added to the file.

cat > example.php

Creating a file in the Linux terminal by using the echo command

Although this method is less popular, you can use the "echo" command to create a file in the Linux terminal. This command allows you to create a file and add content to it by using the ">>" symbol followed by the desired file name and extension. Any text entered after this command will be appended to the existing file or created if it doesn't exist.

echo "Sample phrase" >> example.txt

Conclusion

In conclusion, there are three different methods to create a file in the Linux terminal. The first method is using the touch command, which creates an empty file. The second method is using the cat command, which allows you to create a file and add content to it simultaneously. Lastly, the echo command can be used to create a file and append content to it. Each method offers its own advantages and can be chosen based on specific requirements or preferences. 

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

© Copyright 2024 - ersocon.net - All rights reservedVer. 415