Using the Cat Commands in Linux
The cat commands in linux reads files in the order specified by its parameters, presenting their content in the same format as the standard output, as per the Single Unix Standard. The one option flag the standard requires is u (unbuffered output), which means that all bytes are specified after they have been read. Some operating systems, including those that use the GNU Core Utilities, do it by default and ignore the flag.
Cat commands in linux examines standard input in the order it appears if any input filename is represented by a single hyphen. In the absence of a file name, the cat command merely reads from standard input.
The basic syntax of the cat command
cat [OPTIONS] [FILE…]
The options are used to modify the behavior of the cat command, while the file arguments are the names of the files to be concatenated and displayed. Some of the most commonly used options for the cat commands in linux include:
- -n: displays line numbers before each line
- -b: displays line numbers before non-blank lines only
- -s: squeezes multiple blank lines into a single blank line
- -v: displays non-printable characters as escape sequences
Here are some examples of how the cat command can be used
-
Display the content of a file
You only need to pass the file’s name as an argument when using the cat commands in linux to display a file’s content. Here’s the basic syntax:
cat filename
For example, if you have a file named example.txt and you want to display its contents, you would run the following command:
cat example.txt
This will display the contents of the file on the terminal.
Use the less command to examine a file page by page if it is too large to fit on a single screen. Simply pipe the output of the cat to less, like this:
cat example.txt | less
This will display the contents of the file in a pager, allowing you to scroll through it one page at a time. You can navigate through the file using the arrow keys or the page up/down keys.
Note that if the file contains special characters or control codes, they may be displayed as gibberish on the terminal. In this case, you can use the -v option to display them as escape sequences, like this:
cat -v example.txt
This will display the contents of the file with non-printable characters replaced by escape sequences.
-
Create a file
While you can use any text editor to create a file, you can also create a file using the cat command in the terminal.
Here’s how you can create a file named example.txt using the cat command:
cat > example.txt
This will create a new file named example.txt and put you into “input mode”. Anything you type will be saved into the file.
For example, if you want to create a file with the following contents:
This is an example file. It contains some text.
You would type each line of text followed by the Enter key, like this:
This is an example file.
It contains some text.
When you’re finished typing the contents of the file, press Ctrl-D to save the file and exit input mode.
You can also create a file with multiple lines of text by using a “here document”. For example, create a file named example.txt with the following contents:
This is line 1.
This is line 2.
This is line 3.
You would run the following command:
cat > example.txt << EOF
This is line 1.
This is line 2.
This is line 3. EOF
This will create a new file named example.txt with the specified contents.
-
To Append the Content of a File
To add the content of one or more files to another file, use the cat command. Here’s how you can do it:
cat file1.txt file2.txt >> output.txt
This command will append the contents of file1.txt and file2.txt to the end of output.txt. If output.txt does not exist, it will be created.
Note that the double >> operator is used to append the contents to the file, rather than overwriting it. If you use a single > operator, it will overwrite the contents of the file.
With the same command, you may easily add a file’s content to another file.:
cat file.txt >> file.txt
This will append the contents of file.txt to the end of the same file.
Use the pipe (|) operator to transmit the program’s output to the cat command if you want to append the output to a file, like this:
cat >> output.txt
This will add the command’s output to the end of output.txt.
-
To Insert a New Line
The cat command does not have a direct way to insert a new line into a file. However, you can use a combination of echo and redirection operators to achieve this.
Here’s how you can insert a new line into a file named example.txt:
>> example.txt
This command inserts a new line by appending a blank line to the end of the file. The echo command prints a blank line, which is then redirected using the >> operator to append it to the end of the file.
If you want to insert a line of text into the file, you can simply replace the empty quotes with the text you want to insert, like this:
“This is a new line.” >> example.txt
This will insert the text “This is a new line.” into the file as a new line.
Note that if you want to insert the new line at a specific position within the file, you will need to use a text editor or other tools to manipulate the file’s contents.
-
n command
The cat command doesn’t have an n option or command. However, you can use the nl command to add line numbers to the contents of a file.
Here’s how to use the nl command to display a file’s contents together with its line numbers:
nl filename
For example, to display the contents of a file named example.txt with line numbers, you would run the following command:
nl example.txt
This will display the contents of the file with line numbers on the terminal.
By default, nl numbers all lines in the file, including blank lines. If you want to number only the non-blank lines, you can use the -ba option, like this:
nl -ba example.txt
This will number only the non-blank lines in the file.
The nl command also allows you to customize the line numbering format, adding leading zeros or changing the delimiter between the line number and the text. You can refer to the manual page of the nl command for more information about these options.
-
e command (for display $)
The cat command does not have an e option or command. However, you can use the echo command with the -n and -e options to display the dollar sign ($) on the terminal.
Here’s how you can display the dollar sign on the terminal using the echo command:
echo -n -e “$\n”
While the -e option enables the interpretation of escape sequences, the -n option instructs echo not to append a newline character at the end of the output. The “\n” escape sequence represents a newline character, and the “$” character represents the dollar sign.
When you run this command, it will display the dollar sign on the terminal followed by a new line.
Note that the -e option is not supported by all versions of the echo command. If your version of echo does not support this option, you can try using the printf command instead. For example:
printf ‘%s\n’ ‘$’
This will also display the dollar sign on the terminal followed by a new line.
-
as an end marker
The cat command supports the use of an end marker to indicate the end of the file or input stream. You can use the cat command with the – option and an end marker to concatenate input from multiple sources and indicate the end of each input.
Here’s how you can use the end marker with the cat command:
cat – file1.txt – file2.txt – <<END_MARKER Content to be added END_MARKER
In this example, the cat command is used to concatenate input from four sources: standard input (-), file1.txt, standard input again (-), file2.txt, and the content to be added (specified using a here-document). The <<END_MARKER syntax is used to indicate the start of the here-document, and END_MARKER is used to indicate the end of the here-document.
When you run this command, it will concatenate the input from all sources and display it on the terminal.
Note that the end marker can be any string that does not appear in the input. If you use a string that appears in the input, it will prematurely terminate the input.
-
Show several files simultaneously
To display multiple files at once using the cat command, you can use the following syntax:
cat file1.txt file2.txt file3.txt
This will display the contents of file1.txt, file2.txt, and file3.txt one after the other.
Alternatively, you can use a wildcard (*) to display all files with a particular extension, like this:
cat *.txt
All files in the current directory with a.txt extension will have their contents displayed as a result.
You can also use the cat command in combination with other commands to manipulate or process the contents of the files. For example, you could use the grep command to search for specific text within the files, like this:
cat file1.txt file2.txt file3.txt | grep “search text”
This will display only the lines from the files that contain the specified search text
-
Take Blank Lines Out
To remove blank lines using the cat command, you can use the following syntax:
cat file.txt | sed ‘/^$/d’
This will display the contents of file.txt with all blank lines removed.
Explanation:
The file’s contents are seen on the screen using the cat command.
The sed command is used to perform text transformations on the file contents.
The ‘/^$/d’ argument is used to specify the transformation to be performed, which is to delete all blank lines (i.e., contain no characters).
Alternatively, you can create a new file with the blank lines removed using the following syntax:
cat file.txt | sed ‘/^$/d’ > newfile.txt
This will create a new file named newfile.txt with the contents of file.txt but with all blank lines removed.
-
Display Lines with A Tab
To display TAB-separated lines using the cat command, you can use the -T option. Here’s the syntax:
cat -T filename
This will display the contents of the filename with TAB characters represented as ^I.
For example, if a filename contains the following TAB-separated lines:
apple banana orange grape kiwi lemon
Using the cat -T filename will display the following output:
apple ^Ibanana^Iorange grape ^Ikiwi^Ilemon
The ^I represents the TAB character, a special character used to separate data columns in many file formats.
Note that the -T option can also be combined with other options, such as -n to display line numbers or -b to display line numbers for non-blank lines only.
-
Manage Large Files
If you run a cat on a particularly large file, you’ll get a long, difficult-to-read string of data as a result. To divide it into pages, use | more:
cat test4.txt | more
This just shows the first page of the file. The next page will scroll when you touch a key.
Use | less if you’d want to be able to move the display forward and backward.
cat test4.txt | less
-
Display the Contents in Reverse Order
The tac command can be used to use the cat command to display a file’s contents in reverse (which is cat spelled backward). The tac command reads a file from bottom to top, so it displays the contents in reverse order. Here’s the syntax:
tac filename
For example, if a filename contains the following lines:
Apple
banana
orange
grape
kiwi
lemon
Using tac filename will display the following output:
Lemon
kiwi
grape
orange
banana
apple
As you can see, the lines are displayed in reverse order, with the last line of the file displayed first.
Note that the tac command is not available on all Unix-like systems by default. If tac is not available on your system, you can use the tail and tac commands together to achieve the same effect:
tail -r filename | tac
The -r option for the tail reads the file from the end, while the tac command reads the output from the tail in reverse order.
Final Words
The cat commands in Linux is a command that is used for concatenating and displaying files. The name cat stands for “concatenate,” which means to join two or more files together.
The cat commands in linux is a very basic and versatile command that has been around since the early days of Unix. It can be used to carry out a variety of operations, including showing a file’s contents, merging several files into one, and adding the contents of one file to another.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
Learn about the other linux commands by clicking the links below
https://linuxiron.com/echo-command-in-linux/
https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/
https://linuxiron.com/how-to-use-kill-commands-in-linux/
https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/
https://linuxiron.com/15-useful-yum-commands-in-linux/
https://linuxiron.com/how-to-use-the-top-command-in-linux/
https://linuxiron.com/17-ps-command-to-monitor-linux-process-with-examples-linuxiron/
The cat command is a utility command in Linux. One of its most common uses is printing a file’s content to the standard output stream. Also, we can use the cat command to write some text into a file.
The cat command’s ability to concatenate files is where it gets its name. It can read, combine, and output file contents to the standard output. It reads from the standard input if no file is supplied or if the input file name only contains a single hyphen (-).
Linux commands are executed in the Terminal by pressing Enter at the end of a line. You can perform a variety of tasks with commands, including as managing users, installing packages, and modifying files. Commands contain both options and parameters. Without these, it might still work in some circumstances.