cat command
cat
- Concatenate and display file content.
cat
command in Linux (short for "concatenate") is used to display file contents, combine files, or create small files directly from the terminal. It’s a versatile and widely-used tool for handling text files.
Usage: cat [OPTION]... [FILE]...
OPTION
: Flags which enhances thecat
abilities.FILE
: The file(s) you want to work with (optional; reads from input if omitted).
Examples
-
Displaying a file's contents
The simplest use of
cat
is to print a file’s contents to the terminal.$ cat notes.txt
- Displays the entire contents of
notes.txt
. - If the file doesn’t exist,
cat
outputs an errorNo such file or directory
.
- Displays the entire contents of
-
Concatenating multiple files
cat
can combine multiple files and display them as one continuous output.$ cat file1.txt file2.txt
- Prints the contents of
file1.txt
followed byfile2.txt
.
Save to a new file by redirecting the output to create a combined file:
$ cat file1.txt file2.txt > combined.txt
- Creates
combined.txt
with the contents of both files.
- Prints the contents of
-
Creating a file
You can use
cat
with input redirection to create a file interactively.$ cat > notes.txt
Hello this is the content for notes
$ cat notes.txt
Hello this is the content for notes- Type the text to place in the file and press
Ctrl+D
orcommand+D
to save and exit notes.txt
now contains your input.
- Type the text to place in the file and press
-
Appending to a file
Use
>>
to append content to an existing file instead of overwriting it.$ cat >> notes.txt
Reminder to do things today
$ cat notes.txt
Hello this is the content for notes
Reminder to do things today- Add new text and press
Ctrl+D
orcommand+D
to save and exit
- Add new text and press
-
Numbering lines
Add line numbers to the output with
-n
(all lines) or-b
(non-blank lines only).$ cat -n greet.txt
1 Hello
2 Hola
3
4
5 Ciao
6
7 Bonjour
8
9 Marhabaan- Displays
greet.txt
with line numbers
Non-Blank Lines:
$ cat -b greet.txt
1 Hello
2 Hola
3 Ciao
4 Bonjour
5 Marhabaan- Numbers only non-empty lines.
- Displays
-
Showing End of Lines
Use -E to display a $ at the end of each line, useful for spotting hidden line breaks.
$ cat -E greet.txt
Hello$
Hola$
$
$
Ciao$
$
Bonjour$
$
Marhabaan$ -
Squeezing Blank Lines
Remove repeated empty lines with
-s
(squeeze).$ cat -ns greet.txt
1 Hello
2 Hola
3
4 Ciao
5
6 Bonjour
7
8 Marhabaan- Reduces multiple blank lines to a single blank line.
-
Displaying Tabs and Non-Printable Characters
Use
-T
to show tab characters as^I
.$ cat -T config.txt
- Tabs appear as
^I
in the output. - Combine with
-v
for other non-printable characters:
$ cat -vT strange.txt
- Tabs appear as
To get help related to the cat
command use --help
option
$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.