Reading Files with cat
cat prints the whole contents of one or more files to the screen.
Syntax
cat [options] file...cat (short for concatenate) dumps a file's contents straight to your terminal. It is perfect for short files.
More than one file
Give it several files and it prints them one after another — this is where the name comes from, it joins files together.
cat file.txt— show a file.cat a.txt b.txt— show both in sequence.cat -n file.txt— show with line numbers.
For long files use less instead, or you will be flooded with text.
Example
cat notes.txt
# Buy milk
# Call the dentist
# Number every line
cat -n notes.txt
# 1 Buy milk
# 2 Call the dentist
# Join two files into one
cat header.txt body.txt > page.txtWhen to use it
- A developer uses 'cat .env' to quickly inspect environment variables without opening an editor.
- A sysadmin runs 'cat /var/log/auth.log | grep Failed' to search for failed SSH login attempts.
- A DevOps engineer uses 'cat Dockerfile' to review container build steps in a CI log.
More examples
View file contents
Prints a file to stdout; -n adds line numbers useful when debugging config files.
cat /etc/hostname
# webserver-01
# Show line numbers
cat -n /etc/hostsConcatenate multiple files
Shows cat's original purpose: concatenating multiple files into a single output or file.
# Combine two files and view together
cat header.txt body.txt footer.txt
# Merge into a new file
cat part1.sql part2.sql > full_migration.sqlCreate a small file with cat
Uses a heredoc with cat to write multi-line content to a file directly from the terminal.
# Write multi-line content using a heredoc
cat > greeting.txt << 'EOF'
Hello from Linux!
Today is $(date)
EOF
cat greeting.txt
# Hello from Linux!
# Today is $(date)
Discussion