Linux tricks, one-liners & shell magic

#!/bin/vpp.dk

Community Linux tricks, one-liners & shell scripts — searchable & formatted.

Category: text-processing — 3 trick(s)

+ Add a New Trick

wc is the Swiss Army knife for file statistics. Combine flags or chain with other commands.
bash
$ wc -l file.txt # lines wc -w file.txt # words wc -c file.txt # bytes # Count non-empty lines: grep -c . file.txt # Total lines across multiple files: wc -l *.log | tail -1
Delete this trick? This cannot be undone.
Use sed with find to do a batch search-and-replace across many files at once.
bash
$ # Replace 'foo' with 'bar' in all .conf files: find /etc -name '*.conf' -exec sed -i 's/foo/bar/g' {} + # Preview changes first (dry-run): grep -rl 'foo' /etc/*.conf
Delete this trick? This cannot be undone.
Use grep to search through all files in a directory tree for a pattern, showing filenames and line numbers.
bash
$ grep -rn 'search_term' /path/to/dir/ # Case-insensitive, show 2 lines of context: grep -rin -C2 'error' /var/log/
Delete this trick? This cannot be undone.