Linux Commands (Linux commands for file creation)
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
cat command: cat create file overwrite concatenate copy output (view) of file
File creation with cat command: cat >file1 ------------ ----text--- ------------ Ctrl + D to save and exit. Note: “>” is used to redirect output.
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.
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.
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.
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
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
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
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
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.
stat command: stat command is used to check information of a file including time stamps. Syntax: stat Filename E.g. stat file1
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
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.
vi editing commands: • i - Insert at cursor (goes into insert mode). • :w - To save. • :wq - To save and quit. • :q - Quit. • :q! - Quit forcefully.
nano editor: nano file1 -------------------------- -------------------------- ----------Text----------- -------------------------- Ctrl + X to exit. After it press Y to save.
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
echo command: echo “---------text---------” >file1 printf command: printf “---------text---------” >file1 printf “---------text---------n” >file1
Create a hidden file: To create a hidden file you have to place a dot ( . ) before file name. Examples: >.file1 touch .file2

Linux or Unix commands (commands for file creation)

  • 1.
  • 2.
    How to createa 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
  • 3.
  • 4.
    File creation withcat command: cat >file1 ------------ ----text--- ------------ Ctrl + D to save and exit. Note: “>” is used to redirect output.
  • 5.
    Overwrite a filewith 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 intoa 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 oftwo 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 outputtext 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 commandis 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 asingle 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 singleor 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 ofTime 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 commandis used to check information of a file including time stamps. Syntax: stat Filename E.g. stat file1
  • 14.
    vi editor: vi editorstands 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: Twomodes 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.
  • 17.
  • 18.
    Concatenate text intoa file with nano editor: nano file1 -------------------------- -------------------------- ----------Text----------- -------------------------- --------New text------ -------------------------- Ctrl + O to concatenate. Ctrl + X to exit. After it press Y to save
  • 19.
    echo command: echo “---------text---------”>file1 printf command: printf “---------text---------” >file1 printf “---------text---------n” >file1
  • 20.
    Create a hiddenfile: To create a hidden file you have to place a dot ( . ) before file name. Examples: >.file1 touch .file2