How to create a file in Unix or Linux.
File related commands.
cat command.
tac command.
vi command.
vim command.
vi or vim text editor.
nano text editor.
nano command.
touch command.
stat command.
echo command.
printf command.
Learn more from my YouTube channel: (IT Illusions)
https://www.youtube.com/channel/UCiV576sPPPPgggyS_RHcORw
2. How to create a file in Linux?
There is no specific command used for such purposes.
Use any command: command >Filename or >Filename
Following commands are commonly used to create a file in
Linux terminal:
• cat
• touch
• vi or vim (Note: To create a file, we can use any text editor name and then filename.)
• nano
• echo or printf
4. File creation with cat command:
cat >file1
------------
----text---
------------
Ctrl + D to save and exit.
Note:
“>” is used to redirect output.
5. Overwrite a file with cat command:
cat >file1
-------------------------------------
----------------text----------------
-------- to be overwritten------
--------------------------------------
Ctrl + D to save and exit.
Note: “>” is used to redirect output.
cat file1 >file2 used to overwrite data of file1 into file2.
6. Concatenate text into a file with cat command:
cat >>file1
--------------------------------
-------------text--------------
---to be concatenated----
--------------------------------
Ctrl + D to save and exit.
Note:
“>>” is used to concatenate redirected output with existing file.
7. Copy text of two files into a new file with cat
command: (Merging)
cat file1 file2 >file3
Copy text of a file into a new file with cat
command:
cat file3 >file4
Note: It’ll create file4 with the content of file3.
8. To view output text of one or more files with
cat command:
To view output of a single file:
cat file1
To view output of multiple files:
cat file1 file2 file3 file4
9. tac command:
tac command is also used to view file or set of files but tac command
produces opposite result as compare to cat command. It is used to see
the content from bottom to top.
To view output text of one or more files with
tac command:
To view output of a single file: tac file1
To view output of multiple files: tac file1 file2 file3 file4
10. touch command:
touch
Create a single
or multiple
empty files
change all
timestamps
of a file
change
access time
of a file
change
modify time
of a file
11. Create a single or multiple empty files with
touch command:
• To create a single empty file:
touch file1
• To create multiple empty files:
touch file1 file2 file3 file4
12. 3 Types of Time Stamp:
• Access time (last time when a file was accessed).
touch -a
E.g. touch -a file1
• Modify time (last time when a file was modified).
touch -m
E.g. touch -m file1
• Change time (last time when file’s metadata was changed) is
automatically updated when a single time stamp is modified.
13. stat command:
stat command is used to check information of a file including time
stamps.
Syntax:
stat Filename
E.g. stat file1
14. vi editor:
vi editor stands for “Visual Instrument”. It is a full screen text editor. It
is already installed in every Unix or Linux system.
An improved version of vi editor is vim but most Linux systems have vi
editor installed.
Syntax to create a file with vi editor: vi Filename
Syntax to create a file with vim editor: vim Filename
15. Modes of operation:
Two modes of operation in vi are entry mode and command mode.
Command mode is used to type commands that perform
specific vi functions. Command mode is the default mode for vi.
Entry or Insert mode is used to type text into a file.
Type “i” for insert mode and press “Esc” key to return to
command mode.
16. vi editing commands:
• i - Insert at cursor (goes into insert mode).
• :w - To save.
• :wq - To save and quit.
• :q - Quit.
• :q! - Quit forcefully.
18. Concatenate text into a file with nano editor:
nano file1
--------------------------
--------------------------
----------Text-----------
--------------------------
--------New text------
--------------------------
Ctrl + O to concatenate.
Ctrl + X to exit.
After it press Y to save