🌐
freeCodeCamp
freecodecamp.org › news › the-cat-command-in-linux-how-to-create-a-text-file-with-cat-or-touch
The Cat Command in Linux – How to Create a Text File with Cat or Touch
June 11, 2020 - The most common use of the cat command is to output the contents of a file. The following is an example that you can try. echo "Dance, Dance" > cat_create #create a file cat cat_create
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › cat-command-in-linux-with-examples
Cat Command in Linux - GeeksforGeeks
September 14, 2017 - The cat (concatenate) command in Linux is used to view, create, and combine file contents directly from the terminal.
🌐
Linuxize
linuxize.com › home › linux commands › how to use the cat command in linux
How to Use the cat Command in Linux | Linuxize
October 10, 2018 - If the file2.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file. Use the (>>) operator to append the contents of file1.txt to file2.txt: ... Same as before, if the file is not present, it will be created. To display contents of a file with line numbers, invoke cat with the -n option:
🌐
Unix Community
community.unix.com › unix for beginners q & a › unix for dummies questions & answers
Creating File using the CAT Command - UNIX for Dummies Questions & Answers - Unix Linux Community
January 12, 2010 - Hello , I am newbie to UNIX platform . I have read that there are two ways of creating files that is using 1.) Cat 2.) touch . With Cat Command i am unable to create a File , i is saying **No file or Directory exist…
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › linux cat command (with examples)
Linux cat Command (With Examples) | phoenixNAP KB
September 4, 2025 - The cat (concatenate) command in Linux displays file contents. It reads one or multiple files and prints their content to the terminal. cat is used to view file contents, combine files, and create new files.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › ubuntu linux › ubuntu linux create a text file using cat command
Ubuntu Linux Create a Text File Using cat Command - nixCraft
April 27, 2023 - For instance: Let everything happen to you Beauty and terror Just keep going No feeling is final. ^D · Finally press [CTRL] + [D] to save and exit. Verify it: $ cat bar.txt ... Sadly cat command can not edit the text file or delete lines. Hence, using a text editor such as vim, nano, or emacs or a GUI text editor such as kate text editor under KDE or gedit under GNOME Linux desktop would be best.
🌐
Baeldung
baeldung.com › home › scripting › writing text to file using linux cat command
Writing Text to File Using Linux Cat Command | Baeldung on Linux
August 27, 2025 - From the documentation, we can see that if no value is passed for the FILE argument, the cat command will read from standard input. Similarly, it will behave the same when a dash “-” value is passed for the FILE argument. In combination with the Linux redirection operators, we can make the cat command listen to the standard input stream and redirect the content to a file.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › cat-command-in-linux-with-examples
How to View the Content of File in Linux | cat Command
September 14, 2017 - ... This allows you to type text directly into the terminal, and once you press Ctrl + D, the entered text is saved into jayesh1. ... The `cat` command can combine the content of one or more files and redirect it into another file using >.
Top answer
1 of 4
17

cat's primary purpose is to concatenate files. cat file1 file2 ... will show the contents of file, file2 and the others one after the other, as if the contents were in a single file. See the manpage:

NAME
       cat - concatenate and print files

It is meant for usage where either:

  • a target command cannot read from files and you need to pass multiple files to it. An example is the tr utility. Ordinarily, with one file, you'd do:

    tr < file
    

    But with multiple files, redirection can't be used, so you have to do:

    cat file1 file2 ... | tr
    
  • a target command can read from multiple files, but its behaviour may change when it's given multiple files. An example is wc, which prints the counts for each file, along with the filenames, where you might have wanted just the total, without a filename.

Remember that most commands you encounter (grep, sed, awk, sort, ...) can read files perfectly fine.

If you want to view the contents of a file, use a pager - less and more are both eminently capable of presenting files for viewing, and are far more convenient to use.

2 of 4
14

cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files:

  1. displaying them
  2. combining copies of them
  3. creating new ones.
  4. Copy files

cat's general syntax is:

cat [options] [filenames] [-] [filenames]

Reading Files

The most common use of cat is to read the contents of files, and cat is often the most convenient program for this purpose. All that is necessary to open a text file for viewing on the display monitor is to type the word cat followed by a space and the name of the file and then press the ENTER key. For example, the following will display the contents of a file named file1:

cat file1

Concatenation

The second role of cat is concatenation. (This is the source of cat's curious name.) There is no effect on the original files.

For example, the following command will concatenate copies of the contents of the three files file1, file2 and file3:

cat file1 file2 file3

The contents of each file will be displayed on the monitor screen. This output could just as easily be redirected using the output redirection operator to another file, such as file4, using the following:

cat file1 file2 file3 > file4

File Creation

Thanks to @muru comment : cat is capable of create new files depending on the shell redirection feature and not itself

For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the Ctrl & d keys.

For example, a new file named file1 can be created by typing

cat > file1

then press ENTER and simultaneously press the Ctrl & d keys.

PS1: If a file named file1 already exists, it will be overwritten

PS2: you can append to exited file using append operator >> example cat >> file1

Copy Files

The cat command can also be used (depending on shell redirection feature) to create a new file and transfer to it the data from an existing file. Example: make a copy of file oldfile.txt:

cat oldfile.txt > newfile.txt

References:

  • Linux and Unix cat command
  • The cat Command
  • cat (Unix)
  • HowTo: Use cat Command In Linux / UNIX
🌐
Server Academy
serveracademy.com › blog › the-linux-cat-command
The Linux cat Command - Server Academy
By using the cat command to create and write to files, you can quickly and efficiently manage file content directly from the command line. For more practical examples and in-depth tutorials, consider enrolling in our [Linux Essentials Course] at ServerAcademy.com.
🌐
Hostman
hostman.com › tutorials › how to use the linux cat command: tutorial and examples
How to Use the Linux Cat Command | Hostman
December 26, 2025 - Example: printf "First Line\nSecond Line\nIndented\tThird Line\n" > formatted_file.txt Run the cat command to inspect the file's content and ensure the formatting matches expectations. Append Without Overwriting: To add content to an existing file without overwriting its current data, replace > with the append operator >>: printf "Additional content here.\n" >> formatted_file.txt Using a Text Editor You can also create new files in linux text editors.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
Top answer
1 of 2
48

Yes, it's expected.

We say that Ctrl-D makes cat see "end of file" in the input, and it then stops reading and exits, but that's not really true. Since that's on the terminal, there's no actual "end", and in fact it's not really "end of file" that's ever detected, but any read() of zero bytes.

Usually, the read() system call doesn't return zero bytes except when it's known there's no more available, like at the end of a file. When reading from a network socket where there's no data available, it's expected that new data will arrive at some point, so instead of that zero-byte read, the system call will either block and wait for some data to arrive, or return an error saying that it would block. If the connection was shut down, then it would return zero bytes, though. Then again, even on a file, reading at (or past) the end is not an interminably final end as another process could write something to the file to make it longer, after which a new attempt to read would return more data. (That's what a simple implementation of tail -f would do.)

For a lot of use-cases treating "zero bytes read" as "end of file detected" happens to work well enough that they're considered effectively the same thing in practice.


What the Ctrl-D does here, is to tell the terminal driver to pass along everything it was given this far, even if it's not a full line yet. At the start of a line, that's all of zero bytes, which is detected as an EOF. But after the letter b, the first Ctrl-D sends the b, and then the next one sends the zero bytes entered after the b, and that now gets detected as the EOF.

You can also see what happens if you just run cat without a redirection. It'll look something like this, the parts in italics are what I typed:

$ cat
fooCtrl-Dfoo

When Ctrl-D is pressed, cat gets the input foo, prints it back and continues waiting for input. The line will look like foofoo, and there's no newline after that, so the cursor stays there at the end.

2 of 2
1

A few words about.

As far as I can tell from a quick glance at the source code, cat uses a buffer to optimize its workflow. Referring to the behavior of the simple cat invocation, from standard input and without special command line options:

  1. when the buffer is empty a single Ctrl+D it is enough to exit;
  2. when not, the first Ctrl-D forces the buffer dump (which becomes empty), and the second is interpreted as the command to quit.

This means that if you run your cat> test and simply enter Ctrl-D you will create an empty file (named test) in the current directory, or you will empty the file if it already exists, without the need for a second Ctrl-D .

Beyond the scope of this question, but not so remote: it should be noted that if you are sending characters to a terminal/shell instance an unnecessary second Ctrl-D could cause an unwanted exit (mainly connected to the example above).

🌐
Unstop
unstop.com › home › blog › how to create a file in linux: step-by-step guide
How to create a file in Linux: Step-by-step guide
February 16, 2023 - To create a new file using cat, run the cat command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert file content into this newly created file.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › commands › how to save file in linux using cat command
How to save file in Linux using cat command - nixCraft
February 15, 2022 - We are going to create a new file, use the cat command as follows: cat > todays.txt Press the ENTER key. Type data you want.
🌐
TutorialsPoint
tutorialspoint.com › writing-text-to-file-using-linux-cat-command
Writing Text to File Using Linux Cat Command
The echo command outputs the string "Hello, World!", and the | The symbol (called "pipe") redirects the output of the echo command to the input of the cat command. The > prompt redirects the output of the cat command to the hello.txt file, overwriting the file if it already exists or creating a new file if it doesn't exist.
🌐
Hostinger
hostinger.com › home › tutorials › how to use the linux cat command
How to Use the Linux cat Command With Examples
February 24, 2025 - Practice using the cat command ... basic Linux commands. The cat command in Linux is primarily used to display file contents, concatenate files, and create new files....
🌐
ManageEngine
manageengine.com › home › troubleshooting
How to create a file in Linux
May 26, 2025 - Type :q (colon q) and press Enter to quit the editor · Launch the Terminal and type nano followed by file name to create and open the file in nano. nano myfile.txt · Enter the text you want to include in the file.
🌐
TecMint
tecmint.com › home › linux commands › how to use the cat command in linux [22 useful examples]
22 Essential Cat Command Examples for Linux Users
July 14, 2023 - The cat (short for “concatenate“) command is one of the most frequently used commands in Linux that comes pre-installed in most Linux distribution systems and is primarily used to display the content of existing files.