Skip to content
Related Articles

Related Articles

Different Ways to Create File in Linux
  • Difficulty Level : Easy
  • Last Updated : 06 Jan, 2021

There are mainly four 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 tool for creating files. We cannot edit a file using cat command. Major operations that can be done using it are as follows:

Creating files and then writing the data 

cat >file1

Note: After writing the text, press ctrl+d.

a.) creating files and then writing the data



To view data of a file

cat file1

To view data of a file

Concatenate files — adding two files into another a new file

cat >file2
cat file1 file2 > file3
cat file3

Concatenate files — adding two files into another a new file

To add data at end of a file

cat >> file2
cat file2

To add data at end of a file

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

cat file1 > file2
cat file2

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



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 texts. Major operations that can be done using it are as follows:

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

Create a file

vi file_1

Create a file

1.

Create a file

2.

Create a file

3.

Open and edit 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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :