Cat Command - Display File Content | Online Free DevTools by Hexmos

Display file content with the Cat command. Learn how to use cat to concatenate and display files, with examples for viewing logs and avoiding useless uses.

Cat Command

Understanding the Cat Command

The cat command, short for "concatenate," is a fundamental utility in Unix-like operating systems. Its primary function is to read data from files and output their contents to standard output. This makes it incredibly useful for displaying the content of text files, viewing logs, and even combining multiple files into one.

Basic Usage of Cat

The most straightforward use of cat is to display the contents of a single file. You can also provide multiple file names, and cat will display them sequentially.

# POSIX way in which to cat(1); see cat(1posix).
cat -u [FILE_1 [FILE_2] ...]

# Output a file, expanding any escape sequences (default). Using this short
# one-liner let's you view the boot log how it was show at boot-time.
cat /var/log/boot.log

Efficiently Using Cat

While cat is powerful, there are instances where its use can be considered redundant, especially when piping its output to another command. In many cases, the command receiving the piped input can directly read from the file itself, leading to more efficient execution.

# This is an ever-popular useless use of cat.
cat /etc/passwd | grep '^root'
# The sane way:
grep '^root' /etc/passwd

# If in bash(1), this is often (but not always) a useless use of cat(1).
Buffer=`cat /etc/passwd`
# The sane way:
Buffer=`< /etc/passwd`

Further Resources