How to use cat command in Linux

How to use cat command in Linux

Command cat is used to concatenate files and print on the standard output. For example, if you need to print text file to console you can use this command

cat some_file.txt

You also can print multiple files

cat file1.txt file2.txt

That will print both of these files.

Limit output

You can limit how much content to print using command less or more

cat file.txt | less
or
cat file.txt | more

If will display only portion of data to fit you display, and you can use navigation keys to look throught file. In case of more you would be able to scroll only down, less will also allow you do scroll up.

Sorting

Next example is cat in combination with sort and uniq

cat file.txt | sort

This command will sort content of file.txt and print sorted result. If you need only unique lines then add uniq comand

cat file.txt | sort | uniq

The same result but all duplicated lines will be skipped.

File concatenation

If you need to concatenate two files file1.txt and file2.txt, use this command

cat file1.txt file2.txt > file3.txt

It will merge content of these two files into file3.txt, it will create file file3.txt or rewrite all content if that file already existed. If you need to append data to existing file just use >> instead of >

cat file.txt >> file1.txt
or
cat file1.txt file2.txt >> file3.txt

More about combination cat + sed

Available arguments

If you need to add line numbers to output you should use -n or --number parameter, so command would be

cat -n file.txt

Here is the whole list of parameters you can use

       -A, --show-all
              equivalent to -vET

       -b, --number-nonblank
              number nonempty output lines, overrides -n

       -e     equivalent to -vE

       -E, --show-ends
              display $ at end of each line

       -n, --number
              number all output lines

       -s, --squeeze-blank
              suppress repeated empty output lines

       -t     equivalent to -vT

       -T, --show-tabs
              display TAB characters as ^I

       -u     (ignored)

       -v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

Cat comand in combination with other commands

You can use cat command to pass file content to second command, for example

cat dump.sql | mysql -u username -p database_name

This command will pass content of the dump.sql file to mysql command, as result database_name will be restored with dump.sql

Similar example:

cat movie_file.mp4 | mplayer -

This command will pass content of the movie_file.mp4 to mplayer program, so it will play this file. You can also use this command to play multimedia file from remote server.

ssh [email protected]_hostname cat /home/movie.mp4 | mplayer -

To print only lines that contain specific words you can combine cat with grep

cat file.txt | grep "cool"

Will print only lines that contain only word cool

Conclusion

Command cat is very simple, but in combination with different commands it will help you to do a lot of powerful tricks.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.

Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up