cat (Display File Content)
The cat
(conCATenate) command is used to display the contents of a file without opening it. The command can be used to create a new file with redirection of standard output (>
). To know more about redirection of standard output, check Chapter 5 Standard Input Output and Redirection.
Display the content of a file
To display the content of a file, use the file path as an argument of the cat
command.
For example, if you want to display the content of file_a under your working directory, run the command below.
cat file_a
file_a content
Create and edit a file with the cat command
Before explaining how to create or edit a file with the cat
command, let's check how the cat
command works without any argument.
When running the cat
command without any argument, the cursor moves to the beginning of the line below and the command line waits for your input. After typing some words and hitting the enter key...
ubuntu $ | cat
test test
... you'll see that the command line returns the same text as the one you typed.
ubuntu $ | cat
test test
test test
You stay in the typing mode until you quit it by pressing Ctrl + C or Ctrl + D.
Redirecting standard output
You can redirect the output to a new file or an existing file using the redirection special character (>
or >>
) followed by the file name.
There are differences between >
and >>
.
>
: overwrite contents
>>
: add contents
Note: Redirection and standard output will be explained in Chapter 5 Standard Input Output and Redirection.
1. Create a new file with text content
When you use >
followed by a file path with a new file name, you can create a new file. For example, to create a new file named sample_cat.txt under your working directory, run the command below.
cat > sample_cat.txt
After hitting the enter key, you can type multiple lines of text. For example, you can type some lines like shown below.
sample text
sample text
sample text
Quit the typing mode with Ctrl + C. By running the ls command, you can confirm that there is a new file named sample_cat.txt. By running the cat
command, you can see that the new file contains the text you typed.
ls
sample_cat.txt
cat sample_cat.txt
sample text
sample text
sample text
2. Add text to an existing file
When you use >>
followed by an existing file path, you can add contents to the existing file. For example, to add new text to the sample_cat.txt file, run the command below.
cat >> sample_cat.txt
After hitting the enter key, you can type multiple lines of text. For example, you can type some lines like shown below.
added text
added text
After quitting the typing mode with Ctrl + C and running the cat
command, you can confirm that the new lines of text are added to the file.
cat sample_cat.txt
sample text
sample text
sample text
added text
added text
3. Overwrite an existing file
When you use >
followed by an existing file path, you can overwrite the content of the file. For example, to overwrite the sample_cat.txt file, run the command below.
cat > sample_cat.txt
After hitting the enter key, you can type multiple lines of text. For example, you can type the lines shown below.
overwrite text
overwrite text
After quitting the typing mode with Ctrl + C and running the cat
command, you can confirm that the file was overwritten with the text you typed.
cat sample_cat.txt
overwrite text
overwrite text