cat (short for "concatenate") command is one of the most frequently used
commands in Linux. It reads data from files, and outputs their contents. It is
commonly used for displaying the content of files, combining copies of different
files and creating new ones.
The basic syntax of the
cat command is as follows:
cat [OPTION]... [FILE]...
[OPTION] is used to specify the behavior of the
[FILE] is the name of one or more files you want to read. If no file is
specified, or if the
- is used as one of the input files,
cat will read from
the standard input.
Below is a table of common options for
|Number non-empty output lines, overrides
|Number all output lines
|Squeeze multiple adjacent blank lines
$ at the end of each line
|Display TAB characters as
M- notation, except for LFD and TAB
Example 1: Displaying Content of a File
To display the content of a file named
example.txt, you would use:
Example 2: Creating a File with
To create a file, you can redirect the output of
cat to a file:
cat > newfile.txt
After running the command, type your desired content and then press
Hello, this is a text file.
This is the second line.
Example 3: Combining Files
To combine the contents of
cat file1.txt file2.txt > combined.txt
file2.txt for this example:
echo -e "First file's content." > file1.txt
echo -e "Second file's content." > file2.txt
Example 4: Using Options with
To number all lines of a file:
cat -n example.txt
If you want to squeeze multiple blank lines into a single blank line:
cat -s example.txt
Example 5: Viewing Non-Printable Characters
To see tabs and end-of-line markers in a file:
cat -ET example.txt
cat with Other Commands
cat command can also be used in conjunction with other Linux commands
through pipes (
|). For example, to paginate the contents of a file, you can
cat example.txt | less
Or to count the number of lines in a file:
cat example.txt | wc -l
By understanding and using the
cat command, Linux users can perform a wide
array of file manipulation tasks quickly and efficiently. Whether you are
concatenating files, viewing content, or piping data into other commands,
is an indispensable tool in command-line operations.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.