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
- Type this command in a terminal window:
- Type some content:
- Save the file by pressing the Ctrl and d keys at the same time.
- View the file by typing this command:
cat > readme.txt
Press the Enter key.
This is an example letter written in the terminal.
Press the Enter key.
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.
- This will create a text file named info.txt on the Desktop:
echo "This is a single line of content" > info.txt
echo -e "content\nmorecontent" > filename
Press the Enter key.
- 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.
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