8 Practical Examples for Utilizing the Cat Command in Linux
The cat command, which stands for “concatenate,” is a commonly used tool in Linux that is already included in the majority of Linux distributions. It is mainly employed to show the contents of files that already exist.
In addition, the cat command offers users a range of functionalities, including merging multiple files, generating new files, adding data to existing files, displaying file contents, and sending output to either the terminal or other files.
The cat command offers various options that can be utilized to customize the file output, including the ability to add line numbers before each line of the file’s content.
Furthermore, it has the ability to work together with other commands to carry out different functions such as facilitating page browsing and converting file formats to either binary or hexadecimal.
This article explores the practical applications of cat commands in Linux, along with examples.
Syntax for Cat Command
Using various options and filenames, the cat command can perform multiple functions.
$ cat [OPTION]... [FILE]...
Let’s understand the above syntax:
- [OPTION] – Users can provide multiple options to alter the behavior of the command. The options start with a hyphen
("-")
, such as"-E"
is used to display line ends and"-n"
to display numbers before lines. - [FILE] – The file argument specifies the file which will be manipulated by the command. However, users can provide names of multiple files separated by space.
Tip: To know the available options of the cat command, execute the
"cat --help"
command on the terminal:$ cat --help
Viewing File Contents in a Linux Environment
The main purpose of the cat command is to show the contents of a file that already exists in Linux. To do this, simply enter the file name without any additional options.
$ cat Documents/semayrahost1.txt
The command will show the contents of the file “semayrahost1
.txt” found in the “Documents” folder.
Display Contents of Multiple Files in Linux
You can use the cat command to display the content of multiple files by listing the file names with spaces in between.
$ cat semayrahost1.txt semayrahost2.txt
The terminal display above shows the contents of two files. The initial two lines pertain to “semayrahost1.txt
“, while the final line displays the content of “semayrahost2.txt
“.
Create a File with Cat Command
By utilizing the “>” symbol, also referred to as the “output redirection operator”, users can generate a new file and store content within it, with the command’s output being automatically redirected to the designated file, in this case, “semayra_tutorial.txt”.
cat > semayra_tutorial.txt
Once you have entered the command, a signal will flash on the next line. Input the text for the file and use the key combination “CTRL + D” to save and close the file.
To confirm when the file was created, you can employ the ls command, and to see what is inside the newly made file, you can utilize the cat command
ls
cat semayra_tutorial.txt
Adding Text to a File in Linux
Earlier as discussed, the cat command can be used for merging data. Execute the command to merge the contents of “semayrahost.txt” and “semayrahost2.txt” files and save the combined output in a fresh file named “cat_semayrahost.txt”:
cat semayrahost1.txt semayrahost2.txt > cat_semayrahost.txt
The command mentioned will retrieve the information from the files named “semayrahost1.txt” and “semayrahost2.txt” and save it in a newly created file called “cat_semayrahost.txt”.
In the future, we must confirm if the new file contains the combined content of both files.
ls
cat cat_semayrahost.txt
Display Text Content Including Line Breaks
You can utilize the “-E” option to display the EOL (End of Line) character within the file’s content. These characters, which are not printed visibly, are shown by the dollar symbol (“$”).
cat -E cat_semayrahost.txt
The anticipated result will display a dollar sign ($) at the conclusion of every line within the content.
Enumerate Files by Selected Formats
The cat command supports the use of the “*” wildcard to display the contents of every file in the current directory. Furthermore, you can narrow down the results by specifying a file extension, such as “.txt”, followed by the wildcard, to view the contents of all text files in the directory.
cat *.txt
The output displays the contents of each “txt” file sequentially.
Show Line Numbers in File
To display line numbers before each line of the file’s content without altering the original content, you can utilize the “-n” option.
Each line will be preceded by a corresponding line number in the anticipated output.
Display the line numbers of multiple files.
By using the “-n” option with the cat command, you can merge the contents of multiple files and display the combined output with line numbers, making it easier to view and manage the contents of multiple files simultaneously.
cat -n Fruits.txt veg.txt
In this command, the files named “Fruits.txt” and “veg.txt” will be merged, and the “-n” flag will insert line numbers at the start of each line in the result.