The file is the container that stores the important information. In Linux/Ubuntu creating a file is not a difficult task but if you are a new user then you may face the problem of creating the files from the terminal. Creating a folder or directory in Linux/Ubuntu from the GUI method is a straightforward way but you may get stuck on creating the text file by using this method. This article gives you an overview of performing the task of creating the files in Linux/Ubuntu from the terminal.
How to Create a File in Linux/Ubuntu from the Command Line?
In the Linux/Ubuntu operating system, most tasks are performed by the terminal. It is a robust tool used by developers to run their programming scripts. But this article gives information on creating a file from the terminal. There are different commands to create the files from the terminal in the Linux/Ubuntu operating system.
- Use the “touch” command to create a file
- Use the “cat” command to create a file
- Use the “redirection operator” to create the file
- Use the “echo” Command to create the file
- Use the “printf” command to create the file
- Use the “zip” command to create the zip file
- Use the “gedit” command to create the file
- Use the “kate” command to create the file
- Use the “dd” command to create the file
- Use the “fallocate” command to create the file
- Use the “vim” command to create the file
- Use the “nano” command to create the file
Use the “touch” Command to Create a File
Open the terminal by using the shortcut key “Ctrl+Shift+T” from your keyboard and execute the below-given command to create a file in your Linux/Ubuntu operating system.
touch abc.txt
In the above command
- “touch” is a command that is used to create a file in the terminal of the Linux/Ubuntu operating system.
- “abc.txt” is the file name that is created by the touch command.
The below figure is the output result for the above command. It clearly shows that when we listed the “home” directory before executing the above command there was no “.txt” file found. However, after running this command a file “abc.txt” has been created.
Create the Multiple files
Now use the touch command to create the multiple files by executing the below given command in the Linux/Ubuntu terminal.
touch file1 file2 file3
In the above command “file1”, “file2” and “file3” are three files that are created by the “touch” command. The below figure shows that when we list the “home” directory before implementing the above command there are no files but after executing the command in the terminal three files have been created.
Use the “cat” Command to Create a File
The main task performed by the “cat” command is to print the text of a file in the terminal however, it is possible for this command to create a file. The command to be executed for this purpose in the terminal is given as.
cat > filename
In the above command
- “cat” is used to create the file.
- “>” is the redirection operator which overwrites the written text in the file.
- “Filename” is the name of the file which is going to be created.
The below figure is the output of the above command. There is no file present before executing the above command, by executing this command a file “example.txt” has been created clearly shown in the below figure.
Create a file that contains Multiple Strings
To create a file that contains multiple strings on its page the below given command will be executed in the Linux/Ubuntu terminal.
cat <<EOF > filename
string 1
string 2
string 3
EOF
In the above command “EOF” is the end of file meaning you can not write after this argument. The below given figure is the output for the above command. The result clearly shows that there are three strings and in the end “EOF ” argument is passed to stop the string. The “cat” command is used to print the strings in the “file.txt”.
Use the “redirection operator” to Create the File
The easy way to create the blank file is by using the redirection operator. The command to be executed for the purpose of creating the blank file is given below.
> filename
In the above command “>” is the redirection operator used to create the blank file. The below figure is the output of the above command. By executing this command a file “Linux_user” has been created, clearly shown in the figure.
Use the “echo” Command to Create the File
The “echo” command acts similarly to the print command in C-language; it gives the output text given to it.
The “echo” command can also be used to create the files in the Linux/Ubuntu terminal. The command to be executed in the terminal for the creation of a file is given as.
echo "string" > filename
In the above command
- “Echo” is used to write text in the file.
- “String” is the text written in the file.
- “>” is used to create the file.
The below figure is the output of the above command. By executing this command a file “document.txt” with the string “this is my document” has been created, clearly shown in the below figure.
A little modification is done in the above command and runs in the Linux terminal.
echo "string" >> document.txt
In the above command “>>” with the echo command is just appending the text in the file. The result of the above command clearly shows that when using the “>>” operator and giving another string this appends the text.
Use the “printf” Command to Create the File
The command “printf” is similar to the “echo” command the only difference is that “printf” gives some additional formatting for the string in the file you are creating. The command to create the file in the Linux/Ubuntu terminal is given as.
printf "string" > filename
In the above command “printf” print the sting in the created file. The below figure clearly shows that there is no file created before executing the above command but when executed in the terminal a file “file_1.txt” has been created.
Use the “zip” Command to Create the zip File
To create the zip file which contains single or multiple files the command to be executed in the Linux/Ubuntu terminal is given as.
zip -j zipContent.zip file1 file2
In the above command
- “zip” is used to create the zip file.
- “-j” helps the file to not follow the directory path by ignoring the directory structure.
- “zipContent.zip” is the zip file that carries files.
The figure below is the output of the above command which clearly depicts two files with the name “Testfile.txt” and “abc.txt” combined to make a file by using the “zip” command in the terminal.
Use the “gedit” Command to Create the File
To create a text file in the Linux/Ubuntu operating system the command to be executed in the terminal is given as.
gedit filename
In the above command “gedit” directly creates the text file with the mentioned name. You can use this text file to type your information and save it on your operating system. The below figure shows the results before and after the execution of the above-mentioned command.
Use the “kate” Command to Create the File
To create the advanced text editor the command to be executed in the Linux/Ubuntu terminal is given as.
kate filename
In the above command “kate ” creates the advanced text editor with the mentioned name. Advanced text editor gives you some additional features as compared to the normal text editor. The below figure shows the results before and after the execution of the above-mentioned command.
Use the “dd” Command to Create the File
The basic purpose of the “dd” command is to copy or convert the file but this command is also used to create the file in the Linux/Ubuntu operating system. The full command to create a file in the Linux terminal is given as.
dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G
In the above command
- “dd” stands for data description and is used to create files according to required data.
- “if=/dev/zero” is used to clear the disk for the mentioned file such as “of=1G.test”.
- “seek=1G” is an argument to move the 1*1024 bytes from the start of the file.
The below figure is the output for the above command. This figure clearly shows that the “1G.test” file with the 1G capacity has been created.
Use the “fallocate” Command to Create the File
The command to allocate the specific real space to a file is the “fallocate” command. To create the file in the Linux/Ubuntu operating system the command to be executed in the terminal is given as.
fallocate -l 1G 1G.file
In the above command
- “fallocate -l” allocate the real space to a file.
- “1G” is the size of the file.
- “1G.file” is the file name to be created with the given size.
The below is the output for the above command. A file of 1G size has been created after running the command in the terminal clearly shown in the figure.
Use the “vim” Command to Create the File
To create a text editor and write the string or numerical values from the terminal then execute the below given command in the Linux/Ubuntu terminal.
vim filename
In the above command “vim” creates the text editor in the terminal from where you write the strings or any numeric values and then type the “:wq” command and press enter to exit from the terminal. In this way, your text file will be created. The below figure is the output of the above command which clearly shows the results of before and after running the command.
Use the “nano” Command to Create the File
This is another way to create the text file by using the “nano” command in the terminal. The command to be executed for this purpose is given as.
nano filename
In the above command “nano” directly opens the text editor in the terminal. You write the strings or any numeric values and then use the shortcut key “Ctrl+O” to save it and press “Ctrl+x” to exit from the terminal. The below figure is the output of the above command which clearly shows the results of before and after running the command.
Conclusion
To create the files in the Linux/Ubuntu operating system from the terminal there are many different commands available such as Use the “touch”, “cat”, “redirection-operator”, “echo”, “printf”, “zip”, “kate” and “fallocate”. Each and every command performs a unique function such as “cat” which is used to create the file with single string and multi-line strings. The “zip” command creates the file which contains one or more files in the zip file. The “fallocate” command is very useful when you are allocating and creating a file with a specific size. The “kate” command is used to create the advanced text editor directly from the terminal.