Create a New File in Linux Using the Command Line
Last updated on September 3rd, 2022
Linux is an open-source unix based operating system(OS) for devices, servers, mobile devices, mainframes, and computers. It is supportable, compatible and used on a wide range of platforms. Meanwhile, creating a new file on the Linux Operating System is completely straightforward. But still, you require some quick, clever, and hard-core techniques.
Basically, through this guide, we let you know the techniques that you can employ to Create a New File in Linux. Let’s get started!
- Access to Command Line (Ctrl–Alt–F2)
- Access to Terminal Window (Ctrl–Alt–T)
- User Account with Exclusive Privileges
Command to Create a File in Linux
Linux is designed to create specified or unexist files. One vital feature of working on the Linux operating system is you can directly create a text file in Linux without opening other applications and programs.
Here we hop you into the steps that help to Create a New File in Linux directly using the Command-Line.
#1. Using CAT Command
Cat Command is the most important and popular command for creating files on the Linux Operating Systems. With the “cat” command, you can’t edit the file, but you can do an ample range of operations. We will discuss these operations one by one:
- Follow the Command to create the file and write data into it:
When you type the command in the command line, a new file with the “file 1” name is created if no other files exist with the same name in the folder. If any file has the same name already existing in your current directory, then it is overwritten.
- Use cat file1 Command to View the content of your File
If you want to add the content of two files in a new or existing file, then you should use the “Concatenation” Command.
|cat >file2cat file1 file2 > file3cat file3|
When you run this command:
-It creates a new file, File3 if it doesn’t exist in the current directory.
-The new File File3 contains the data of File1 and File2.
- The following code displays the Output:
|Display the output of above mentioned CodeFollow the Command to create the file and write data into it:|
- If you want to add the content at the end of your file, you should use this command
|cat >> file2cat file2|
This Linux Command Creates File with the name that is not present in your current directory. For example, a new file with File2 name (in write mode). If any file exists with this name, then it opens the file(in append mode).
- If you want to copy the data of one file to another file, then use this command:
|cat file1 > file2cat file2|
- To view the data of particular file in reverse order, use the command tac:
#2. Using vi Command
The primary use of “Vi” Command is to edit the files. This command is used by the programmers and developers to edit the textual content available in the file using a vi text editor.
For creating a new file in Linux, type
When you run this command, it creates a new editable file File_1 if it does not exist in the current directory and opens the vi text editor. If any file exists with this name in your directory, the command automatically opens the vi text editor file.
Now the editor is ready to use. Open and edit your file.
To type any character in the file, use simple arrow keys on your keyboard. Please dont use Numpad present on the Right Side of the keyboard
NOTE: To save and exit from your vi editor,
- Press Escape Key
- Type :wq
- Hit Enter
#3. Using Touch Command
- To Create a File: Creating a new editable file.
|touch filecat file|
- To Change the timestamp of the File. For Checking timestamp.
|stat filetouch filestat file|
- To Change Access Time Only: The last time when you access the file.
|stat filetouch -a filestat file|
- To Change Modify Time Only: The time when the file is modified.
|stat filetouch -m filestat file|
#4. Using mv Command
mv Command is used for
- Moving the files or directories from one file to another in Linux operating Systems.
- Create new files with other files content on the system.
|mv file_2 file_3|
This command works like a concatenation in which the data of the second file(file3) updates in the newly created file(file2) and deletes the file3.
#5. Using Gedit Command
Linux users mostly prefer the use of Command Line Interface (CLI) for editing and writing textual files. But if you want to edit and write code on the Linux Platform without using the vim and nano Editors, then gedit text editor works for you.
“gedit” is the abbreviation for GNOME that basically a text editor uses to edit and write files. It is a standard text editor that is compatible with all Linux operating systems, including Ubuntu, Fedora, Debian, CentOS, and Red Hat.
Use Command to Create a New File in Linux.
#6. Using Nano Command
Nano Text Editor may or may not be found on all the Linux platforms. You can use this command to create and edit files.
Exit from Nano Text, use the command
ctrl + X
At this moment, you are aware of multiple commands and command line functions. So, By using this command, you can create a file in Linux. Notwithstanding, using any one of the mentioned commands in this guide, you can create, edit, add, append, view, and delete files in Linux. Even you can modify, copy, and manage files and directories smoothly and efficiently. Thank you!
Read More: How to Check Disk Space on Linux?