How to Quickly Create a Text File Using the Command Line in Linux

By Lori Kaufman on October 23rd, 2014

00_lead_image_creating_text_file

If you’re a keyboard person, a lot of things can be accomplished simply using the command line. For example, there are a few easy-to-use methods for creating text files, should you need to do so.

NOTE: When we say to type something in this article and there are quotes around the text, DO NOT type the quotes, unless we specify otherwise.

The first method for creating text files uses the “cat” command. Type the following command at the prompt and press Enter. Replace “sample.txt” with the name you want to use for your file.

cat > sample.txt

After pressing Enter, you are not returned to the prompt. Instead, the cursor is placed on the next line, allowing you to enter text into your file. Type your lines of text, pressing Enter after each one. When you are done, press Ctrl + D to exit the file and return to the prompt.

01_using_cat

To verify your file was created, type the following command at the prompt and press Enter.

NOTE: The command starts with a lowercase “L” and the option (-l) is also a lowercase “L”.

ls -l sample.txt

You should see directory listing of your file, as shown below.

02_listing_text_file

The “cat” command can also be used to view the contents of your file. To do this, type the following command at the prompt and press Enter.

cat sample.txt

The contents of the file is printed to the screen and you are returned to the prompt.

03_viewing_file_contents

One method for creating a blank text file is to use the right arrow (>). Type the right arrow, “>”, followed by a filename and press Enter, as shown in the following image.

Notice that you are returned to the prompt with no indication that a file was created. You can check that the file exists by using the “ls” command as discussed earlier.

04_create_file_using_right_arrow

When you use the “cat” command to view the contents of your file, nothing displays. The file was created but it contains no text.

05_nothing_in_file

To add text to the file, you can use Vi. Type the following command to open the text file in Vi.

vi sample.txt

06_opening_text_file_in_vi

Vi opens in command mode. To type text into the editor, type a lowercase “i” to enter editing mode. Type your text and press Esc to return to command mode. To close Vi and save your file, type “:wq” and press Enter.

Learn more about how to use vi to edit text files from our Beginners’ Guide to Editing Text Files With Vi.

07_entering_text_in_vi

Again, to view the contents of the file, type the following command at the prompt and press Enter.

cat sample.txt

08_text_in_file_after_vi

Another command that works similarly to using the right arrow (>) to create an empty text file is the “touch” command. Type “touch” followed by a filename, as shown in the following image.

09_creating_file_with_touch_command

The “touch” command creates an empty text file. Use Vi, or other text editor, to add text to the file.

Lori Kaufman is a writer who likes to write geeky how-to articles to help make people's lives easier through the use of technology. She loves watching and reading mysteries and is an avid Doctor Who fan.

  • Published 10/23/14
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!