Linux cat command: Display text files
Linux cat command Function Description
Use the cat command to display the contents of a text file, or to append the contents of several files to another file.
Linux cat command Syntax
cat [Option] [File]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-n | Numbering all rows of the output |
-b | Numbering non-empty output lines |
-s | When more than two consecutive blank lines are encountered, replace them with one blank line |
-E | Show $ at the end of each line |
Linux cat command Demo
Displays the contents of the /etc/grub.conf file:
[root@rhel ~]# cat /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
#NOTICE: You have a/boot partition. This means that
# all kernel and initrd paths are relative to/boot/, eg.
# root(hd0,0)
# kernel/vmlinuz-version ro root=/dev/sda2
# initrd/initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --encrypted
61aFd0Gw3uB0r/oQE$y80vx6.tXQQdJTr457ORMUaqOuouP1wajM15vHjzvo4oI6sc P8HZfUwhrpKEAbOwDb4oUxmdwFDMcoJ59LMxH0
title Red Hat Enterprise Linux (2.6.32-358.el6.x86_64)
root (hd0,0)
kernel/vmlinuz-2.6.32-358.el6.x86_64 ro root=UUID=2ac36994-5a18-4741-8118-96fe2c57a6c6 r
d_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-358.el6.x86_64.img
Enter the contents of textfile1 with line numbers into textfile2
[root@rhel ~]# cat -n textfile1 > textfile2
Append the contents of textfile1 and textfile2 with line numbers (blank lines are not added) to the file textfile3
[root@rhel ~]# cat -b textfile1 textfile2 >> textfile3
Display the /etc/passwd file with the $ symbol at the end of each line
[root@rhel ~]# cat -E /etc/passwd
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologin
........................
Use the cat command to create the mm.txt file
[root@rhel ~]# cat >mm.txt<<EOF
>Hello //Enter the character Hello here
>Linux //Enter the character Linux here
>EOF //Enter the character EOF here and it will automatically return to the Shell prompt screen