opscape

Viewing files cheat sheet

The most useful viewing files commands on one printable page, every line checked by running it.

cat Concatenate files and print them to standard output; also the quickest way to see a short file.

cat todo.txt
Print a short file output
cat logs/app.log.1 logs/error.log
Join several files into one stream output
cat -n notes.txt
Number every line output
cat -A data/windows.txt
Reveal Windows line endings output

head Print the first lines (or bytes) of files; 10 lines by default.

head -n 3 data/users.csv
Choose how many lines output
head -n -3 todo.txt
Everything except the last lines output
cut -d ' ' -f 1 access.log | sort | uniq -c | sort -rn | head -n 3
Keep the top results of a pipeline output

hexdump Display file contents in hex, octal, decimal or ASCII, with a canonical hex-plus-text layout via -C.

hexdump -C data/windows.txt
Hex and text side by side output
hexdump -C -n 16 photos/beach.png
Only the first bytes output

less Page through a file or command output one screen at a time, with search and scrolling in both directions.

grep -n INFO logs/app.log | less
Page the output of another command output
less -F -X notes.txt
Skip the pager when the text fits on one screen output

nl Print files with line numbers added, with control over which lines get numbered and how.

nl todo.txt
Number the lines of a file output
nl -b a data/settings.conf
Number every line, including blank ones output
nl -w 2 -s '. ' todo.txt
Narrow numbers with a custom separator output

od Dump a file's bytes as octal, hex, decimal or characters, to see exactly what it contains.

od -c data/windows.txt
See every byte as a character output
od -An -t x1 todo.txt | head -n 2
Hex bytes without offsets output
od -A d -t x1z -N 32 photos/beach.png
Hex next to printable text output

tac Print files with their lines in reverse order, last line first.

tac todo.txt
Reverse the order of lines output
tac logs/app.log | head -n 3
Show the newest log entries first output

tail Print the last lines of files, or follow a file as it grows with -f.

tail -n 3 access.log
Choose how many lines output
tail -n +2 data/users.csv | head -n 3
Skip a header line output
(sleep 1; echo 'line added 1s later' >> logs/app.log) & timeout 3 tail -n 2 -f logs/app.log echo "tail stopped with status $?"
Follow a file as it grows output
tail -F /var/log/nginx/access.log
Keep following across log rotation output

wc Count lines, words, characters and bytes in files or standard input.

wc -l access.log
Count lines output
count=$(wc -l < todo.txt) echo "There are $count items to do"
Get just the number, for a script output
grep -c . access.log; grep ' 404 ' access.log | wc -l
Count the results of a pipeline output

xxd Make a hex dump of a file, or turn a hex dump back into binary with -r.

xxd -l 32 photos/beach.png
Hex dump of the start of a file output
xxd -p -l 16 README.md
Plain hex string output
echo 48656c6c6f2c20776f726c640a | xxd -r -p
Turn a hex string back into bytes output