CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / cat

cat

Print and concatenate files

Beginneressentialviewingpipes

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.

Syntaxcat [OPTION]... [FILE]...

Watch it run

You landed on an unknown server and want to know which OS it runs and see a config file with line numbers.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
cat /etc/os-release

cat dumps the whole file at once. Perfect for short files like this one; for long ones use less, head or tail.

Anatomy of a command

$
commandcat

Read each file in order and write its bytes to standard output.

How it works

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.

typeopen + truncatefork + execreadreadwrite fd 1stderrYoucat a b > cbashruns as kunalcatfd 1 = c.csva.csvinput 1b.csvinput 2c.csvoutputTerminalfd 2: errors
1/7
The shell parses the line

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.

← → to step

Options you'll actually use

-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 - footer

Common mistakes

cat app.log | grep ERROR

Works, but starts an extra process and pipe for nothing (the 'useless use of cat').

grep ERROR app.log
cat file.txt > file.txt or sort file > file

The 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 file
sudo cat new.conf > /etc/app.conf

The 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 something

It floods the terminal and scrollback; you'll only see the end.

less, grep, head or tail -n 100

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print /etc/app/app.conf with line numbers.

  2. 2

    Show the invisible line endings and whitespace in ~/app/.env.

  3. 3

    Combine ~/data/servers.txt and ~/data/notes.txt into ~/combined.txt and count its lines.

  4. 4

    Create ~/hello.conf containing two lines (name=demo and port=9000) using cat and a heredoc.

Related commands

lessBrowse files and output: scroll, search, filter, followheadShow the first lines or bytes of a filetailShow the last lines of a file and follow it livetacPrint a file with the lines in reverse orderteeCopy a stream to the screen and to files at once
PreviouslnNexttac
Linux / File Viewing & Editing / cat

cat

Print and concatenate files

Beginneressentialviewingpipes

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.

Syntaxcat [OPTION]... [FILE]...

Watch it run

You landed on an unknown server and want to know which OS it runs and see a config file with line numbers.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
cat /etc/os-release

cat dumps the whole file at once. Perfect for short files like this one; for long ones use less, head or tail.

Anatomy of a command

$
commandcat

Read each file in order and write its bytes to standard output.

How it works

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.

typeopen + truncatefork + execreadreadwrite fd 1stderrYoucat a b > cbashruns as kunalcatfd 1 = c.csva.csvinput 1b.csvinput 2c.csvoutputTerminalfd 2: errors
1/7
The shell parses the line

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.

← → to step

Options you'll actually use

-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 - footer

Common mistakes

cat app.log | grep ERROR

Works, but starts an extra process and pipe for nothing (the 'useless use of cat').

grep ERROR app.log
cat file.txt > file.txt or sort file > file

The 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 file
sudo cat new.conf > /etc/app.conf

The 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 something

It floods the terminal and scrollback; you'll only see the end.

less, grep, head or tail -n 100

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print /etc/app/app.conf with line numbers.

  2. 2

    Show the invisible line endings and whitespace in ~/app/.env.

  3. 3

    Combine ~/data/servers.txt and ~/data/notes.txt into ~/combined.txt and count its lines.

  4. 4

    Create ~/hello.conf containing two lines (name=demo and port=9000) using cat and a heredoc.

Related commands

lessBrowse files and output: scroll, search, filter, followheadShow the first lines or bytes of a filetailShow the last lines of a file and follow it livetacPrint a file with the lines in reverse orderteeCopy a stream to the screen and to files at once
PreviouslnNexttac