DEV Community

Cover image for Which Linux Command Creates Files Best?
Omkar Sharma
Omkar Sharma

Posted on • Edited on

1 1 1 1 1

Which Linux Command Creates Files Best?

1. Using cat Command

The cat command can be used to create and view files, but cannot be used to edit existing content.

Create a File

cat > file1
Enter fullscreen mode Exit fullscreen mode
  • (>) redirects output into the file.
  • Use Ctrl + D to save and exit.

Append to a File

cat >> file1
Enter fullscreen mode Exit fullscreen mode
  • (>>) appends new data to the file without modifying existing content.
  • Note: cat is not a text editor.
  • cat >> file1 { >> use to add new data without changing existing data}

Concatenate Files

cat file1 file2 > file3 
Enter fullscreen mode Exit fullscreen mode
  • Combines contents of file1 and file2 into file3.
  • tac command is used to reverse the content of the file.
  • In cat we cannot create multiple files at once whereas in touch we can.
cat file1 > file2
Enter fullscreen mode Exit fullscreen mode
  • Copy Content from One File to Another

2 Using Touch Command

touch <filename>
Enter fullscreen mode Exit fullscreen mode
  • create empty file (Main purpose is Time Stamping)
  • stat command to see the stat (access stamp, modify stamp, change/update stamp)

3 using Vi/Vim (editor)

Modes in VI Editor

  • Normal Mode (default) – Used for navigation and command execution.
  • Insert Mode – Used for text editing (press i to enter, Esc to exit).
  • Command Mode – Used for saving, quitting, and searching (press : in Normal mode).
vi <filename>
Enter fullscreen mode Exit fullscreen mode
  • i – Insert before cursor
  • Esc - to escape from insert mode
  • :wq - to save and exit
  • :wq! - to forcefully saving and exiting.
  • :q! - Quit without saving

4 Nano (editor)

  • nano is a simpler command-line text editor. It is user-friendly and suitable for beginners.

Top comments (0)