Cat Command in Linux/Unix with ExamplesThe 'cat' command is the most universal and powerful tool. It is considered to be one of the most frequently used commands. It can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files, display the line number, display $ at the end of the line, etc. Cat can be described as a standard Unix utility that sequentially reads files, writing them on the standard output. The name is derived from the function to catenate files. It has been shipped to several operating systems. This command is used to concatenate files which include multiple files into one file. Usage of the Cat CommandThe Single Unix Specification describes the cat operation to read files in the order provided in its arguments, specifying their contents in the same order as the standard output. The specification mandates the one option flag support, i.e., u (unbuffered output), meaning that all bytes are specified after it has been read. By default, a few operating systems do it and avoid the flag, such as the ones with the GNU Core Utilities. If any input filename is described as one hyphen, the cat command reads through standard input in the order at that point. The cat command only reads from standard input if no file is mentioned. Use Cases of the Cat CommandThe cat command can be used for piping a file to any program that expects binary data or plain text on the input stream. The cat command doesn't damage non-text bytes when outputting and concatenating. As such, the two primary use cases of this command are certain format-compatible binary file types and text files. The resulting merged file may not be authorized for several structured binary data sets. For instance, if a file contains a unique footer or header, the result will duplicate these. Although, for a few multimedia digital container formats, the final file is authorized, and so the cat command offers an effective appending file means. Video streams can be an important file example that the cat command can merge without problem, e.g., the DV (Digital Video) and MPEG program stream (MPEG-2 and MPEG-1) formats, which are simple packet streams fundamentally. Options in the cat Command
Linux cat command: to display file contentThe 'cat' command can be used to display the content of a file. Syntax: Example: In the above snapshot, file 'jtp.txt' is displayed with the help of command "cat jtp.txt". Note: To display the content of multiple files at once, type file names in one single line like "cat file1 file2 file3... fileN. Linux cat command usage
Linux cat command (to create a file)The 'cat' command can be used to create a new file with greater than sign (>). Syntax: Example: In the above snapshot, we have created a new file called "javatpoint". Now let's see how to create it. Type the command "cat >javatpoint" and press 'enter'. You will be directed to the next line. Press 'enter' after every line and you will be directed to the next line. To save your file, go to the next line, press 'ctrl+d' and your file will be saved. To Append the Content of A FileThe 'cat' command with double greater than sign (>>) append (add something in the last of a file) something in your already existing file. Syntax: Example: Look at the above snapshot, a new line at the end is added in the file 'javatpoint'. After passing "cat >> javatpoint" command, type the lines as much as you want to add. To save the file press 'ctrl + d'. Linux cat command (to copy file)The 'cat' command can be used to copy the content of a file into another file. Syntax: Example: In the above snapshot, we have copied the content of file 'combo' into the file 'combo2' with the command "cat combo > combo2". Linux cat command (to concatenate files)The 'cat' command can be used to concatenate the contents of multiple files in a single new file. Syntax: Example: Look at the above snapshot, we have combined three files "file1, file2, and file3" into a single file "combo" with the command "cat file1 file2 file3 >combo". Notice the content of three separate files and then the content of a new concatenated file that is "combo". To Insert A New LineA new line will be inserted while concatenating multiple files by using a hyphen (-). syntax: Example: In the above snapshot, we have inserted a new line at the beginning while concatenating file1, file2 and file3 with the command "cat - file1 file2 file3 >combo". Note: Line will be inserted at the beginning of the file only. Linux cat -n command (to display line numbers)The 'cat -n' option displays line numbers in front of each line in a file. Syntax: Example: Look at the above snapshot; the file 'jtp.txt' has a line number in front of every line by passing the command "cat -n jtp.txt". cat -b (file name) The 'cat -b' option removes the empty lines. Syntax: Example: In the previous snapshot, after line 19, line number 20 has also been marked but it is an empty line. In the above snapshot, line 20 is removed with the help of command "cat -b jtp.txt". Linux cat -e command (to display $)The 'cat-e' option displays a '$' sign at the end of every line. Syntax: Example: Look at the above snapshot; some lines include spaces also. A user won't be able to recognize whitespace at the end of each line. The "cat -e program" command will put the $ sign at the end of every line including spaces. Linux cat command (as an end marker)The 'cat << EOF ' option displays an end marker at the end of a file. It is called here directive and file content will be saved at the given end marker. The file can be saved with the help of 'ctrl + d ' keys also. It works like the end marker. Note: Any word other than 'EOF' can be used for the end marker. Syntax: Example: In the above snapshot, we have created 'exm.txt' file with 'EOF' as the end marker by passing the command "cat > exm.txt << EOF". Linux Cat FiltersWhen cat command is used inside pipes, it does nothing except moving stdin to stout. Syntax: Example: Look at the above snapshot, the output of one 'cat' or 'tac' command is passing onto another as input. Show All File ContentsThe basic usage of the cat command is showing the file contents. Simply, we need to type "cat" followed by the filename to show the file contents using the terminal window: We can apply the wildcard character along with the cat command to show every file in the current directory: To show only the contents of a text file inside a directory, we can type the below command: Show Multiple FilesAlso, we can combine and show multiple file contents together inside the terminal window with the help of the cat command. We can use the below syntax to simultaneously show multiple files: Copy the Result of a File to AnotherThe cat command can also be used to copy the result of a file to another. First, it creates it if not found. It overwrites the desired file otherwise. We can use the below syntax to copy the result of a file to another: Append the Result of One File to AnotherRather than overwriting the result of a desired file in the old example, we can also use the cat command for appending the result: If the destination file doesn't exist, it creates it. It appends the result otherwise. Sort the ResultAlso, we can merge the sort using the cat command for alphabetically sorting the result: Next TopicLinux tac |