Skip to content
Related Articles

Related Articles

Different Ways to Create File in Linux
  • Difficulty Level : Basic
  • Last Updated : 03 Mar, 2021

There are mainly six ways of creating files in Linux. All of them have their own purpose and benefits. They are as follows:

1. cat command

It is the most universal command/tool for creating files on Linux systems. We cannot edit a file using the cat command. Major operations that can be done using it are as follows:

To create files and write the data into them

cat >file1

This command creates a new file file1 (in write mode) if it doesn’t exist in the present working directory. If any file with file name file1 exists in the current directory then it is overwritten. 

Note: After writing the text into the file, press ctrl+d to save and exit from the writing mode.



a.) creating files and then writing the data

To view the contents of a file

cat file1

This command simply prints the contents of file1 on the terminal screen.

To view data of a file

To concatenate files — adding the contents of two files into a new file or existing file

cat >file2
cat file1 file2 > file3
cat file3

This command creates a new file file3 with the contents of file1 & file2 if file3 doesn’t exist in the present working directory. If any file with file name file3 exists in the current directory then it is overwritten with the contents of file1 & file2. 

Concatenate files — adding two files into another a new file

To add the contents at end of a file



cat >> file2
cat file2

This command creates a new file file2 (in write mode) if it doesn’t exist in the present working directory. If any file with file name file2 exists in the current directory then it is opened (in append mode). 

To add data at end of a file

To copy the file’s data (the content of one file into another) 

cat file1 > file2
cat file2

This command creates a new file file2 with the contents of file1 if file2 doesn’t exist in the present working directory. If any file with file name file2 exists in the current directory then it is overwritten with the contents of file1.

Copy file's data (the content of one file into another)

To reverse the data of the file (tac)

tac file1

Reverse the data of the file (tac)

2. touch command

We can create an empty file (or multiple empty files) using this command. But its main purpose is to change or update the time-stamp of a file. Major operations that can be done using it are as follows:

Creating a file

touch filea
cat filea

Note: Creating a file and then using the cat command to view the data.

Creating a file

To change the timestamp of the file

stat filea
touch filea
stat filea

Note: We are using stat file_name to check the timestamp of the file. We can see the access, modify, and change is now updated.

To change the timestamp of the file

Change access time only

Access time is the last time when the file was accessed.

stat filea
touch -a filea
stat filea

Change access time only

Change modify time only

Modify time is the time when a file was modified.

stat filea
touch -m filea
stat filea

Change modify time only

3. vi command

Its main function is to edit files. It is commonly used by programmers to edit the textual content of any file on vi text editor. Major operations that can be done using it are as follows:

Note: To save and exit from the vi text editor, press the Escape key and then type :wq and hit enter.

Create a file

vi file_1

This command creates a new file file_1 and opens it on the vi text editor if it doesn’t exist in the present working directory. If a file with the file name file_1 exists in the current directory then this command just opens the file on the vi text editor.

Create a file

1.

Create a file

2.

Create a file

3.

Open and edit the file

use simple arrow keys to move and character keys to type in the file.

Open and edit file

 

4. nano command

It may/may not be found in all distributions of LINUX. We can create as well as edit files.

Note: To exit nano Text Editor press ctrl + x.

nano file_1

4. nano command.

1.

4. nano command.

2.

4. nano command.

3.

5. gedit command

Linux’s users normally use the command line interface (CLI) for writing or editing the text files. But if we want to edit a text file graphically on Linux machines without learning about the powerful editors like vim and nano then gedit text editor makes it easier for us.

“gedit” stands for GNOME text editor, it’s a standard default text editor found in any system with a GNOME desktop environment including Ubuntu, Fedora, Debian, CentOS, and Red Hat. Using gedit we can create as well as write/edit the text files.

Create a file

gedit file_2

1.

2.

This command creates a new file file_2 (in write mode) on the gedit text editor if it doesn’t exist in the present working directory. If any file with file name file_2 exists in the current directory then it is opened (in edit mode) on the gedit text editor.

Note: To use the terminal again, press ctrl + c.

Open and edit the file

As we create or edit a file using gedit command, by default the file gets open on the gedit text editor and wait for it to close before it returns you to the terminal prompt. If you want to use the terminal window while the gedit text editor is open, launch gedit with the following command instead. 

gedit file_2 &

This command opens gedit text editor as a background task. We get the command line prompt back straight away and carry on using the terminal window even when gedit text editor is running.

6. mv command

We normally use mv command to move the files or directories from one place to another in Linux systems. But we can also use it to create new files with the contents of some other file on the system. 

mv file_2 file_3

This command creates a new file file_3 with the contents of file_2 if file_3 doesn’t exist in the present working directory. If any file with file name file_3 exists in the current directory then it is overwritten with the contents of file_2.

Note: This command copies the content of file_2 to file_3 and deletes file_2.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :