Linux Cat Command Usage And Examples
This post will guide you how to use Linux cat command to concatenate multiple files and print files on the standard output.
NAME
cat – concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]… [FILE]…
DESCRIPTION
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-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
–help display this help and exit
–version
output version information and exit
EXAMPLES
Displaying Contents of Files
You can use cat command to display the contents of a given file in Linux, just use the following command:
# cat /etc/passwd
Outputs:
root@devops-osetc:/home/devops/working# cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin .......
If you want to put the contents of one file into another file or new file, you can use cat command with redirection operator to achieve it. Just type the following command:
# cat /etc/passwd > /tmp/fio.txt
This command will redirect all outputs from cat command to file fio.txt. then you can use cat command again to display the contents of fio.txt file.
Concatenate files
You can specify tow ore more file names in cat command, and it will display the contents of those files one after the other. so you can use the cat command to concatenate their contents to standard output. or you can also use redirection operator to redirect their contents to a another file. type:
# cat fio1.txt fio2.txt
or
# cat fio1.txt fio2.txt > fio.txt
Appending File
If you only want to append a source text file to another file, you can use cat command with the redirectio operator “>>”, type:
# cat fio1.txt >> fio.txt
this command will read the contents of fio1.txt, and then append them at the end of fio.txt file. Note: If fio.txt file does not exist, it will create new one.