Note -n adds line numbers, -A shows invisible characters (tabs, line endings). cat is best for short files; use less for anything longer than a screenful. Concatenating multiple files into one is where cat gets its name.
show filedisplay contentsread fileprint fileconcatenate filesline numbers
View Beginning of File
syntax
head [options] file
example
head -n 20 access.log
head -c 512 firmware.bin
Note -n specifies number of lines (default 10). -c specifies number of bytes. Useful for quickly inspecting CSV headers or log files.
first linesbeginning of filetop of filepreview file
Note -f follows the file in real-time as new lines are appended, which is indispensable for monitoring logs. Use Ctrl+C to stop following. -F will keep retrying if the file is rotated or recreated.
last linesend of filefollow logwatch logtail filereal-time log
Page Through Files
syntax
less [options] file
example
less +G server.log
less -N config.yaml
Note Navigate: Space (page down), b (page up), /pattern (search forward), ?pattern (search backward), n/N (next/previous match), g/G (start/end), q (quit). +G opens at the end of the file. -N shows line numbers.
scroll filepage throughview large fileread lognavigate file
Note -l counts lines, -w counts words, -c counts bytes, -m counts characters. With no flags, all three are shown. Piping with find is a quick way to count lines across an entire project.
Note -u removes duplicates, -n sorts numerically, -r reverses order, -h sorts human-readable sizes (1K, 2M, 3G), -t sets field delimiter, -k specifies which field to sort by.
sort linesorder filesort numericallysort by columnalphabetical sort
Filter Duplicate Lines
syntax
uniq [options] [input [output]]
example
sort access.log | uniq -c | sort -rn | head -20
output
847 GET /api/users
623 GET /api/health
412 POST /api/login
Note uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.
mystery_attachment: PDF document, version 1.7
database.dump: application/octet-stream; charset=binary
Note file examines the file's content (magic bytes), not the extension. -i outputs MIME type. Useful when you receive a file with no extension or a misleading one.
file typeidentify filewhat kind of filemime typecheck file format
Note Shows size, permissions (numeric and symbolic), ownership, inode, timestamps (access, modify, change), and more. On macOS, the output format differs slightly from GNU stat.