Technical skill

Linux command: cat

Subject: .../LINUX

Show or pass along file contents

If you want to read a short text file or pass its contents to another program, cat reads the file and copies its contents to standard output, the stream that normally appears in the terminal. Its name comes from “concatenate”: it can display one file, join several streams, or pass data along. It does not interpret file contents as shell commands, format them as a report, or let you edit them.

To display a short file, run:

cat notes.txt

With several inputs, cat writes them one after another without adding a separator:

cat header.txt body.txt

This is useful when another program expects a stream. For example, cat access.log | grep ' 404 ' can send text through a pipe, though grep ' 404 ' access.log avoids starting an extra process when grep can open the file directly. A pipeline makes sense when several producers supply input or when the next command expects standard input.

If no filename is supplied, cat reads standard input until it reaches end-of-file. A lone - also means standard input in common implementations and POSIX. Thus printf '%s\n' 'extra line' | cat - notes.txt puts generated text before the file contents.

The shell, not cat, handles output redirection. In cat source.txt > copy.txt, the shell opens copy.txt and connects it to standard output before cat starts. Keep output paths separate from input paths. cat source.txt > source.txt truncates the source before cat can read it. Likewise, cat > notes.txt waits for typed input and overwrites an existing destination by default; press Ctrl+D in an interactive terminal to end that input. Check the target path before redirecting output.

GNU cat offers -n to number output lines and -A to make certain control characters visible. These options are implementation-specific and do not edit the input file. Check the local manual if a script needs to run on several Unix-like systems.

For a long file, less allows paging and search; head, tail, and grep can show a smaller part. cat copies a byte stream, so sending binary data to a terminal can produce unreadable characters or control effects. Quote path expansions such as cat "$file" so spaces stay in one argument. GNU/Linux cat -- "$file" also ends option parsing for a filename that begins with a hyphen.

Interactive exercises

Materials

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful