Logo

How to create a file in Linux from terminal window?

You can create a file from the Linux terminal in various ways. Below are the most common approaches:

  1. Using touch

    touch filename.txt
    • Creates a new empty file named filename.txt if it doesn’t already exist.
    • If the file does exist, touch updates its last modified timestamp.
  2. Using Redirection with >

    echo "Hello world!" > filename.txt
    • Writes "Hello world!" to filename.txt, creating the file if it doesn’t exist.
    • Overwrites any existing content in filename.txt.
  3. Using cat

    cat > filename.txt
    • Opens an interactive prompt. Whatever you type will be written into filename.txt.
    • Press Ctrl + D on an empty line to finish and save.
  4. Using a Text Editor

    • Nano:
      nano filename.txt
      Edit and save using Ctrl+O, then exit with Ctrl+X.
    • Vim:
      vim filename.txt
      Press i to insert text, then Esc, type :wq to write and quit.
  5. Using > for Empty File

    > filename.txt
    • Creates or truncates an empty file if you don’t provide any input.

Recommended Resource

If you’re looking to deepen your command-line capabilities and strengthen your overall coding proficiency, check out this course from DesignGurus.io:

CONTRIBUTOR
TechGrind