The cat command is one of the most used commands in Linux. It can be used to print the contents of a file and also as an input to another command like echo. The output is displayed on screen. This article will discuss 10 examples of how we can use the Linux cat command effectively.
Create file
- Create file: If you want to create a file with the name “file”, use cat command followed by the string printing “file” and press enter. In this case, the new file is created in the current directory.
- Create file with content: If you want to create a file containing specific content, use cat command followed by -n option and input of your choice. For example, if you need to create a new text document named “myfile4” with contents “Hello World! It’s me again! Today I’m going to talk about Linux Cat Command Examples” type following command:
cat -n “Hello World! It’s me again! Today I’m going to talk about Linux Cat Command Examples” > mynewfile4
Append content to the file
Append content to the file:
- Use cat command with >> to append content to a file. For example, if you want to add text at the end of a file named “testfile”, type the following command:
- cat testfile >> newfile
Print line numbers in a file
If you have a text file containing lines of text, then you can use the cat command to print the line numbers in the file.
The following examples show how to print line numbers in a file:
- cat -n filename
- cat -b filename
Show Empty Lines
To show empty lines, use the following options:
- v option: Shows all files. If the file is deleted, it will show a “>” sign.
- e option: Displays only empty files (no contents). Also shows files that are not existing on your computer. Examples include /dev/zero, /dev/null and /etc/passwd if you don’t have root access to your computer.
- n option: Displays nonprintable characters as ASCII characters in octal format instead of hexadecimal format.
- s option: Display summary statistics instead of showing details for each file or directory listed on stderr output stream (which is standard error by default). Use this flag with -l flag to get file size statistics as well as other information about each item listed in the output., You can also use cat command without any arguments at all to display detailed information about your current working directory.”
File Combing Operations
cat file1 file2 > file3
This command concatenates (joins) the contents of two files, sending the output to a third file.
In other words: cat will take both of your files and combine them into one. If you’re in a hurry or if you just want a fast solution, this is probably what you’ll use most often.
cat file1 file2 | tee file3
You can also pipe your cat’s output into another command using this format: `cat`.
In other words: This takes both files and pipes their contents through tee so that each line goes to both places at once.
Move contents of one file to another file
To move contents of one file to another file, use cat:
> cat filename1 > filename2
To append to a file, use > and >>. The following command will add a line at the end of a log file:
> echo “I am new text” >> my_log.txt
You can also append multiple lines by using >> or even overwrite an existing log if it exists:
> echo “I am first text” >> my_log.txt # overwrites existing file with new content
< takes input from standard input and writes it to standard output:
Merge Lines with Tab Space
Linux cat command is used to display a file’s content. You can also use Linux cat command to merge multiple lines into one, join files, and join lines in a file with tab space and newline.
This example shows how to use Linux cat command to merge multiple lines into one:
Get input from keyboard
The cat command is a versatile tool, and it can be used to get input from your keyboard. The simplest way to do this is by using the cat command without any arguments.
“`
$ echo “the quick brown fox jumps over the lazy dog” | cat
the quick brown fox jumps over the lazy dog
Show contents of the particular file version in git repository
To show the contents of a particular version in git repository
cat -n hoge.txt
git cat-file -p HEAD:hage.txt > hoge.txt
Read File Content from Command Line with STDIN and PIPE
It is possible to read content from files with the cat command. The following example reads the content of file1, which is stored in /tmp:
cat /tmp/file1
Linux cat command has many options that can be used together to achieve the desired result.
The cat command is used to print the contents of a file. It is also used to concatenate files and print the contents of a file.
The syntax for using cat command:
cat [FILE]…|more
In this article, we have discussed some of the most important uses of cat command in Linux. There are many more advanced uses of this command which we will cover in future articles.