In Linux, we can create files in the following ways:
- By using the
touchcommand (to create an empty file) - By using the
catcommand - By using editors like
gedit,vi,nano, etc.
cat > file1.txtExample:
$ cat > file1.txt
Hello Friends
Listen Carefully
Otherwise Linux will give
Left and Right- Press
ctrl+dto save and exit.
If file1.txt is not already available, it will be created with the provided data. If file1.txt already exists with some content, the old data will be overwritten with the new data.
To append instead of overwriting, use >> with the cat command.
cat >> file1.txtExample:
$ cat >> file1.txt
extra content- Press
ctrl+dto save and exit.
-
Q1. What is the difference between
touchandcat?touchis used for creating an empty file, whereascatis used for creating a file with some content. -
Q2. How can we perform overwriting and appending with the
catcommand?>is meant for overwriting and>>is meant for appending/concatenation. -
Q3. If we use the
touchcommand, but the file is already available, what will happen?The content of the file won't change, but the last modified date and time (timestamp) will be updated.
Assume that we have a shell script that sends all current date files to a remote server. This script doesn't run on Saturdays and Sundays. However, on Monday, it needs to send files from all three days. The script only recognizes current date files, so to change the timestamp of Saturday and Sunday files, we can use the touch command.
Note: The touch command can be used for two main purposes:
- To create an empty file
- To change the timestamp of an existing file
We can view the contents of a file using the following commands:
cattacrevheadtaillessmore
gedit: Similar to Windows Notepadvieditor- Open file:
vi file3.txt - The editor opens in command mode
- Enter insert mode: Press
i - Add your required data
- Return to command mode: Press
ESC - Save and exit: Type
:wq
- Open file:
vimeditor: Advanced version ofvieditornanoeditor:nano file4.txt- Save:
ctrl+O - Exit:
ctrl+X
- Save: