catPrint and concatenate files
cat prints files to the terminal and joins several files into one stream. Use it for short files, for feeding a file into a pipe, for writing small files with a heredoc, and with -A to reveal invisible characters like Windows line endings that break configs and secrets.
cat [OPTION]... [FILE]...You landed on an unknown server and want to know which OS it runs and see a config file with line numbers.
catRead each file in order and write its bytes to standard output.
cat is one of the simplest programs on the system: for each file argument it calls open(), then loops read() into a buffer (128 KiB on modern coreutils) and write() to file descriptor 1 until end of file, then moves to the next file. A '-' or no argument means read standard input. cat doesn't know or care what the bytes are, which is why it happily dumps binary files and why it can join any files. All the interesting behaviour comes from the shell around it: > and >> are opened by the shell before cat runs (so cat file > file truncates the input first), | connects cat's stdout to the next program, and a heredoc (<<'EOF') feeds lines from the script itself into cat's stdin. With -n, -A, -s and friends cat switches to a slower path that inspects every byte to number lines or render invisible characters.
The shell opens and truncates the output file before cat even starts; cat just copies its inputs to file descriptor 1.
bash splits cat a.csv b.csv > c.csv into a command with two arguments and a redirection. The redirection is the shell's job, not cat's.
-nNumber all output lines-bNumber only non-empty lines-AShow all: $ at line ends, ^I tabs, ^M carriage returns (= -vET)-EShow $ at the end of each line (spot trailing spaces)-TShow tabs as ^I-vShow non-printing characters (^X and M- notation)-sSqueeze repeated blank lines into one-Read standard input at this position: cat header - footercat app.log | grep ERRORWorks, but starts an extra process and pipe for nothing (the 'useless use of cat').
grep ERROR app.logcat file.txt > file.txt or sort file > fileThe shell truncates the output file before the command reads it, so the input is empty and the data is lost.
Write to a new file, then mv it over: sort file > file.tmp && mv file.tmp filesudo cat new.conf > /etc/app.confThe redirection is performed by your unprivileged shell before sudo runs.
sudo tee /etc/app.conf < new.conf > /dev/null (or sudo sh -c 'cat new.conf > /etc/app.conf')cat on a 4 GB log to look for somethingIt floods the terminal and scrollback; you'll only see the end.
less, grep, head or tail -n 100Try these on a Linux machine of your own. Each task has a hint and a solution.
Print /etc/app/app.conf with line numbers.
Show the invisible line endings and whitespace in ~/app/.env.
Combine ~/data/servers.txt and ~/data/notes.txt into ~/combined.txt and count its lines.
Create ~/hello.conf containing two lines (name=demo and port=9000) using cat and a heredoc.
catPrint and concatenate files
cat prints files to the terminal and joins several files into one stream. Use it for short files, for feeding a file into a pipe, for writing small files with a heredoc, and with -A to reveal invisible characters like Windows line endings that break configs and secrets.
cat [OPTION]... [FILE]...You landed on an unknown server and want to know which OS it runs and see a config file with line numbers.
catRead each file in order and write its bytes to standard output.
cat is one of the simplest programs on the system: for each file argument it calls open(), then loops read() into a buffer (128 KiB on modern coreutils) and write() to file descriptor 1 until end of file, then moves to the next file. A '-' or no argument means read standard input. cat doesn't know or care what the bytes are, which is why it happily dumps binary files and why it can join any files. All the interesting behaviour comes from the shell around it: > and >> are opened by the shell before cat runs (so cat file > file truncates the input first), | connects cat's stdout to the next program, and a heredoc (<<'EOF') feeds lines from the script itself into cat's stdin. With -n, -A, -s and friends cat switches to a slower path that inspects every byte to number lines or render invisible characters.
The shell opens and truncates the output file before cat even starts; cat just copies its inputs to file descriptor 1.
bash splits cat a.csv b.csv > c.csv into a command with two arguments and a redirection. The redirection is the shell's job, not cat's.
-nNumber all output lines-bNumber only non-empty lines-AShow all: $ at line ends, ^I tabs, ^M carriage returns (= -vET)-EShow $ at the end of each line (spot trailing spaces)-TShow tabs as ^I-vShow non-printing characters (^X and M- notation)-sSqueeze repeated blank lines into one-Read standard input at this position: cat header - footercat app.log | grep ERRORWorks, but starts an extra process and pipe for nothing (the 'useless use of cat').
grep ERROR app.logcat file.txt > file.txt or sort file > fileThe shell truncates the output file before the command reads it, so the input is empty and the data is lost.
Write to a new file, then mv it over: sort file > file.tmp && mv file.tmp filesudo cat new.conf > /etc/app.confThe redirection is performed by your unprivileged shell before sudo runs.
sudo tee /etc/app.conf < new.conf > /dev/null (or sudo sh -c 'cat new.conf > /etc/app.conf')cat on a 4 GB log to look for somethingIt floods the terminal and scrollback; you'll only see the end.
less, grep, head or tail -n 100Try these on a Linux machine of your own. Each task has a hint and a solution.
Print /etc/app/app.conf with line numbers.
Show the invisible line endings and whitespace in ~/app/.env.
Combine ~/data/servers.txt and ~/data/notes.txt into ~/combined.txt and count its lines.
Create ~/hello.conf containing two lines (name=demo and port=9000) using cat and a heredoc.