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.
To view data of a file
cat file1
Concatenate files — adding two files into another a new file
cat >file2
cat file1 file2 > file3
cat file3
To add data at end of a file
cat >> file2
cat file2
Copy file’s data (the content of one file into another)
cat file1 > file2
cat file2
Reverse the data of the file (tac)
tac file1
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.
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.
Change access time only
Access time is the last time when the file was accessed.
stat filea
touch -a filea
stat filea
Change modify time only
Modify time is the time when a file was modified.
stat filea
touch -m filea
stat filea
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
Open and edit file
use simple arrow keys to move and character keys to type in the 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
0 comments:
Post a Comment