In Linux, creating a file is a basic ability that every user needs to know. Knowing how to create files is crucial for effective file management and organization on the Linux operating system, regardless of user experience level.
We will look at many ways to create files in this article, so you can manage your data and operate more efficiently. Many ways, such as in different methods where you can understand easily and clear your doubts about creating files in Linux. To find the file size in Linux with the different commands like ls, du, stat, etc., that helps to get which file is using what space improves the online experience and runs it smoother.
Essential Necessities
- Having access to a terminal window or command line (Ctrl-Alt-F2 or Ctrl-Alt-T)
- Should have sudo access for a user account (optional for specific files/directories)
How to Use the Command Line to Create a File in Linux
Any file you specify can be created by Linux, even if it doesn’t currently exist. The ability to generate files instantly without opening an application is one clever feature.
The following commands allow you to create a file right from the command line.
1. Create a File with the Touch Command
The touch command is the simplest method in Linux for creating a new file.
Enter the following command in a terminal window:
touch test.txt
This produces a brand-new, blank file called test.txt. You can enter to view it:
ls
The current directory’s contents are listed by the ls command. The file was created in the current directory by the touch command because no alternative location was given.
The touch command will update the timestamp if a file already exists with the chosen name.
2. Use the Redirect Operator to Create a New File
A character that modifies the location where the results are shown is known as a redirection operator.
Angle bracket to the right >
This icon instructs the system to output the results into the next specified format. Typically, the target is a filename. This symbol can be used on its own to start a new file:
> test2.txt
This makes a brand-new, blank file. To locate the file test2.txt, use the ls command to list everything in the current directory.
3. Use the cat Command to Create a File.
Concatenate is short for the cat command. The contents of several files, a single file, or even a portion of a file can be printed using it. The Linux cat command will create the file if it doesn’t already exist.
Use cat to create an empty file by entering the following:
cat > test3.txt
Observe the operator for redirecting. This command usually shows what’s in test2.txt on the screen. The system is instructed to save it in the test2.txt file by the redirection operator >.
Confirm the creation of the file:
ls
Now test.txt, test2.txt, and test3.txt ought to be in the system’s list.
4. Use the echo Command to Create a File.
When you use the echo command, anything you enter will be copied and saved in a file.
Enter the following command:
echo 'Random sample text' > test4.txt
Confirm the creation of the file:
ls
The file test4.txt ought to appear in the list now. To see the contents of the new file, use the cat command:
cat test4.txt
Random sample tex or whatever you typed in using the echo command should appear on the screen.
5. Use printf to Create a File
The printf command provides some formatting capabilities and functions similarly to the echo command. Enter to add a single line of text:
printf 'First line of text\n' test5.txt
Use the \n option to divide each line of text into two to add more content:
printf 'First line of text\n Second line of text' test6.txt
To view the contents of either of these files, use the cat command.
Making a Linux File using Text Editors
At least one text editor is included with every Linux distribution. Some have more than one editor. Every editor has unique features and strengths. You’ll see three of the most well-liked which we will understand and explain one by one below.
1. The Text Editor Vi
The first Linux text editor is called Vi. It was designed to be used with the Linux operating system to edit text files directly. It’s a safe editor to know about, as it’s rare that you’ll find a Linux distribution without it.
Enter the following to start a file in Vi:
vi test7.txt
There will be a screen update. You’ve entered the text editor now. To test it out, enter a few words after selecting insert mode by pressing the letter I.
Press Esc:x and Enter to save and exit.
2. The Text editor Vim
It’s possible that you observed the Vi editor’s lack of user-friendliness. A more recent iteration is called Vim, or Vi editor, Modified.
To start a new text file, use vim:
vim test8.txt
This screen will resemble the editing interface in Vi. To insert text, press i and enter a few words. Save the file, then type this to exit:
Esc :wq Enter
3. The Text Editor Nano
A more recent and user-friendly text editor is called Nano.
To start a new file with text editor nano, type the following command:
nano test9.txt
Nano automatically switches to editing mode. A useful list of commands is also shown at the bottom of the screen.
Type some text, then save the changes by pressing Ctrl+O.
For the editor to close, press Ctrl+X.
Conclusion
Gaining proficiency in creating files in Linux gives users access to a plethora of options that allow them to efficiently organize, store, and manipulate data. The first step to fully utilizing the Linux operating system is learning how to create files, whether using graphical interfaces or command-line tools.
Users can now confidently traverse the Linux system and improve their computing experience with this acquired competence. Hope you got your doubts clear and understand different ways to create files in Linux. We tried to explain each way with steps in detail applying the commands.