Do you need to memorize Linux commands?Daily Linux Command: manDaily Linux Command: cd and pwd
Today, let’s talk about one of the most commonly used yet often underestimated commands in Linux: cat (short for concatenate). Its main function is to read the contents of one or more files and output them to standard output (which is the terminal).
The cat command is very suitable for quickly viewing file contents without using editors like vim or nano, especially when you just want to take a quick look at a configuration file or log, making it particularly convenient.
✦•———-Basic Usage———-•✦
- View a single file
Simply add the file path after cat:
$ cat /etc/issueDebian GNU/Linux 12 \n \l
- View multiple files
You can view multiple files at once, and the contents will be output in order. You can also use wildcards ‘*’.
$ cat /etc/issue /etc/os-releaseDebian GNU/Linux 12 \n \l
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"NAME="Debian GNU/Linux"VERSION_ID="12"VERSION="12 (bookworm)"VERSION_CODENAME=bookwormID=debianHOME_URL="https://www.debian.org/"SUPPORT_URL="https://www.debian.org/support"BUG_REPORT_URL="https://bugs.debian.org/"
- View a file and display line numbers
By adding the -n parameter, you can display line numbers before each line, which is very suitable for viewing code or configurations:
$ cat -n /usr/bin/c89-gcc 1 #! /bin/sh 2 3 # Call the appropriate C compiler with options to accept ANSI/ISO C 4 # The following options are the same (as of gcc-2.95): 5 # -ansi 6 # -std=c89 7 # -std=iso9899:1990 8 9 extra_flag=-std=c89 10 11 for i; do 12 case "$i" in 13 -ansi|-std=c89|-std=iso9899:1990) 14 extra_flag= 15 ;; 16 -std=*) 17 echo >&2 "`basename $0` called with non ANSI/ISO C option $i" 18 exit 1 19 ;; 20 esac 21 done 22 23 exec gcc $extra_flag ${1+"$@"}
- Merge multiple files
cat can also merge the contents of multiple files and output them to a new file (note: this is not “merging files”, but “concatenating contents and redirecting”):
$ cat /etc/issue /etc/os-release > all.txt$ cat all.txtDebian GNU/Linux 12 \n \l
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"NAME="Debian GNU/Linux"VERSION_ID="12"VERSION="12 (bookworm)"VERSION_CODENAME=bookwormID=debianHOME_URL="https://www.debian.org/"SUPPORT_URL="https://www.debian.org/support"BUG_REPORT_URL="https://bugs.debian.org/"
✦•———-Advanced Usage Tips———-•✦Next, we will introduce some fun tricks.
- Quickly clear file contents
By redirecting /dev/null to a file, you can instantly clear its contents without changing the file’s inode number (suitable for log cleaning scenarios):
$ ls -lhi file.log759762 -rw-r--r-- 1 dybai dybai 1.6K Sep 12 17:25 file.log$ cat /dev/null > file.log$ ls -lhi file.log759762 -rw-r--r-- 1 dybai dybai 0 Sep 12 17:25 file.log
- Display invisible charactersBy using the -A parameter, you can view invisible characters in a file.
$ cat -A text.txtHello$^IWorld$ !$
$ cat text.txtHello World !
-A will display $ at the end of the line (indicating a newline), tab characters as ^I, and other non-printing characters. This is very useful for debugging scripts or viewing files containing special characters.
- Write multi-line data to a fileIf we want to automate the creation of configuration files during script execution, instead of relying on interactive text editors like vim(1), we can achieve this using the cat(1) command.The following example needs to be run with root privileges because the path we want to write to /etc/apt/sources.list.d/ is not writable by regular users.
# cat > /etc/apt/sources.list.d/docker.sources << EOFdeb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bookworm stable# deb-src [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bookworm stableEOF
In this example, cat will read everything between << EOF and the next EOF and write it to /etc/apt/sources.list.d/docker.sources file.Variables between the two EOFs will be expanded; if you do not want the variables to be expanded, use << ‘EOF’. For example:
$ cat > path.txt << EOFPATH=$PATHEOF$ cat path.txtPATH=/sbin:/home/dybai/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
$ cat > path.txt << 'EOF'PATH=$PATHEOF$ cat path.txtPATH=$PATH
Both usages are very useful when generating configuration files.
💡 Notes
-
cat(1) is suitable for viewing text files; processing binary files may output garbled text.
-
If you want to view large files (like several GB logs), it is recommended to use less(1) or tail(1) to avoid freezing the terminal.
-
cat(1) is part of GNU coreutils and is included in basically all Linux distributions.
The cat(1) command is far more than just “viewing files”—it can also merge contents, clear files, and generate configurations with redirection, making it an essential Swiss Army knife in every Linux user’s toolbox.This concludes the content on the cat(1) command.Congratulations, you have gained more useful knowledge✨