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


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.


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.


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.


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.


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


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

vi sample.txt


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.


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

cat sample.txt


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.


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.