cat
- Concatenate And Display File Contents
The cat
command is used to print the content of one or more files to the standard output(terminal). It can also be used to combine multiple files into one.
Essential Options
Options | Description |
---|---|
cat file.txt | Display the contents of a file. |
cat file1.txt file2.txt | Display contents of multiple files in order |
cat file1.txt file2.txt > both.txt | Combine both files into a new file |
cat -n file.txt | Number all output lines |
cat -b file.txt | Number non-blank lines only |
cat -s file.txt | Squeeze multiple blank lines |
cat -E file.txt | Show $ at end each line |
Example
Use this sample file: notes.txt
Line 1: This is the first line.
Line 2: This line has trailing spaces.
Line 3: This line is followed by multiple blank lines.
Line 4: The end of the sample file.
- Number all lines in
notes.txt
cat -n notes.txt
- Number only the non-blank
notes.txt
file
cat -b notes.txt
- You have a file
notes.txt
with many consecutive blank lines. Display it with all multiple blank lines reduced to a single blank line.
cat -s notes.txt
- How do you know where the lines ends?
cat -E notes.txt
head
| tail
- Show The First/Last Lines Of A File
The head
command displays the first N
lines of a file (default is 10). The tail
command displays the last N
lines of a file (default is 10).
Example
head file.txt # first 10 lines
head -n 8 file.txt # first 8 lines
head -n 2 file.txt # Only the first 2 lines
tails file.txt # last 10 lines
tails -n 8 file.txt # last 8 lines
tails -n 2 file.txt # Only the last 2 lines
Note:
tail -f
, this flag is used to show the text files’s live update. Meaning, it follows the files as it grows. AKA, that’s how logging work, you see the last update (recently happened ones).
less
- Interactive Pager For Viewing Large Files
the less
command allows you to view and scroll through large files interactively, making it easier to navigate logs, reports, or any lengthy text files withtout loading the entire file into memory.
Essential Options
Key | Description |
---|---|
/pattern | Search forward for pattern |
n /N | Goto next/prev search match |
b | Page up |
f | Page down |
Examples
- View
notes.txt
and search for “yum”
less notes.txt
# Press -> /yum -> Enter Button
- How to page up and down while inside the
less
view
b
= Page Upf
= Page Down
- How to go to next/prev search matches
n
= NextN
= Previous