SH

File Content

Bash & Linux · 10 entries

Display File Contents

syntax
cat [options] file...
example
cat server.log
cat -n deploy.sh
cat header.html body.html footer.html > page.html

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.

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.

View End of File / Follow Logs

syntax
tail [options] file
example
tail -n 50 error.log
tail -f /var/log/nginx/access.log
tail -f app.log | grep --line-buffered 'ERROR'

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.

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.

Word, Line & Byte Count

syntax
wc [options] file...
example
wc -l src/*.py
wc -w essay.txt
find . -name '*.go' | xargs wc -l | tail -1
output
  142  387  4821 app.py
   38   95  1102 utils.py
  180  482  5923 total

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.

Sort Lines

syntax
sort [options] file
example
sort -u names.txt
sort -t',' -k3 -n sales.csv
du -sh */ | sort -rh

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.

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.

Compare Files

syntax
diff [options] file1 file2
example
diff old_config.ini new_config.ini
diff -u main.py main_v2.py > changes.patch
diff -rq dir_a/ dir_b/
output
--- old_config.ini
+++ new_config.ini
@@ -3,4 +3,4 @@
-timeout=30
+timeout=60

Note -u produces unified format (most readable). -r compares directories recursively. -q shows only whether files differ, not how. --color adds color output on supported systems.

Identify File Type

syntax
file [options] file...
example
file mystery_attachment
file -i database.dump
output
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.

Detailed File Metadata

syntax
stat [options] file
example
stat package.json
output
  File: package.json
  Size: 1843       Blocks: 8       IO Block: 4096  regular file
Access: (0644/-rw-r--r--)  Uid: (1000/deploy)  Gid: (1000/staff)
Modify: 2026-03-28 14:22:10.000000000 +0000

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.