cat Command: Tutorial & Examples

Concatenate and display files

The cat command is a Unix utility that concatenates and displays the contents of one or more text files. It can also be used to create new files or to append text to existing files.

Here are some examples of how you can use the cat command:

To display the contents of a file, you can use cat followed by the name of the file:

cat file.txt

This will print the contents of file.txt to the terminal.

To concatenate multiple files and display the combined output, you can specify the names of all the files you want to concatenate:

cat file1.txt file2.txt file3.txt

This will print the contents of file1.txt, followed by file2.txt, followed by file3.txt.

To create a new file and add some text to it, you can use the cat command followed by the > operator and the name of the new file:

cat > newfile.txt

This will open a blank line in the terminal, and you can type the text you want to add to the new file. When you're done, press CTRL+D to save the file and exit.

To append text to an existing file, you can use the cat command followed by the >> operator and the name of the file:

cat >> existingfile.txt

This will open a blank line in the terminal, and you can type the text you want to append to the existing file. When you're done, press CTRL+D to save the changes and exit.

Note that the cat command can also be used with a number of options and arguments to modify its behavior. For example, you can use the -n option to number the lines of the output, or the -s option to suppress repeated empty lines. You can use the --help option to display a list of all the available options and arguments.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA