Tuesday 12 January 2021

Different Ways to Create File in Linux

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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.

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

To view data of a file

cat file1

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Concatenate files — adding two files into another a new file

cat >file2
cat file1 file2 > file3
cat file3

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

To add data at end of a file

cat >> file2
cat file2

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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

cat file1 > file2
cat file2

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Reverse the data of the file (tac)

tac file1

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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.

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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.

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Change access time only

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

stat filea
touch -a filea
stat filea

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Change modify time only

Modify time is the time when a file was modified.

stat filea
touch -m filea
stat filea

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Open and edit file

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

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

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

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

LPI Exam Prep, LPI Learning, LPI Certification, LPI Learning, LPI Guides, LPI Career, LPI Cert Prep, LPI PDF

Related Posts

0 comments:

Post a Comment