Little Girl's Mostly Linux Blog

CreateTextFileInTerminal

Create a text file in the terminal

This page was last updated on February 20, 2010.

Create an entire text file from within the terminal without using an editor!

Method 1

When using this method, press the Enter key to insert a blank line in the text.

  • Type this command in a terminal window, replacing filename with the name of the file you’d like to save the text to:
  • cat > filename
  • Example:
    1. Type this command in a terminal window:
    2. cat > readme.txt

      Press the Enter key.

    3. Type some content:
    4. This is an example letter written in the terminal.

      Press the Enter key.

    5. Save the file by pressing the Ctrl and d keys at the same time.
    6. View the file by typing this command:
    7. cat readme.txt

      Press the Enter key.

    Inserting blank lines in the letter can be done by pressing the Enter key.

Method 2

  • For a single line of content, type this command in a terminal window, replacing content with whatever you want inside the text file and filename with the name of the file you’d like to save it to:
  • echo "content" > filename

    Press the Enter key.

  • Example:
    • This will create a text file named info.txt on the Desktop:
    • echo "This is a single line of content" > info.txt
  • For multiple lines of content, add the -e argument to the command, and insert \n to start a new line or \n\n to insert a blank line in the text. Type this command in a terminal window, replacing content\nmorecontent with whatever you want inside the text file and filename with the name of the file you’d like to save it to:
  • echo -e "content\nmorecontent" > filename

    Press the Enter key.

  • Example:
    • This will create a text file named info.txt on the Desktop:
    • echo -e "This is one line of content.\nThis is another line of content.\n\nThis is a final line of content after a blank line." > info.txt


Obligatory Happy Ending

And they all lived happily ever after. The end.

2 Comments »

  1. To make the echo command create multiple lines add the “-e” switch (to let it use escape commands) and use “\n” within the string to be echoed.
    For example:
    echo -e “this is a\ntest” >test.txt

    Love your short, quick, exploration guides. Thanks for sharing them online.

    Comment by Miriam — February 18, 2010 @ 4:07 pm

    • Thank you for the help with the multiple lines in the echo command! I’m glad you like the guides. :)

      Comment by mostlylinux — February 20, 2010 @ 3:32 am


RSS feed for comments on this post. TrackBack URI

Comment:

Create a free website or blog at WordPress.com.