Linux & shell: Files & search
8 Linux & shell commands: Files & search. Click a command to see what each part does.
Linux & shell: Files & searchIndex
- Whole sheet251
- Navigate & list13
- Create, copy, move, delete13
- View & compare files9
- Permissions & ownership31
- Help & shell basics11
- Users & groups6
- Install packages7
- Package & software versions29
- System info9
- Network basics & SSH10
- Zip & compress4
- Shell operators6
- Processes6
- Files & search8
- Text processing pipelines4
- Disk3
- Network5
- Services & logs6
- Archives & copying4
- Firewall7
- Scheduling: cron & systemd timers6
- Storage, mounts & LVM10
- Kernel, hardware & limits8
- Performance & tracing10
- TLS & certificates8
- Packet capture & network debugging10
- Terminal tools8
Files & search8
grep -rn "<text>" <dir>Search text in all files under a folderWhat each part does
grepprint lines matching a pattern-rrecurse into sub-directories-nshow the line number of each match"<text>"the pattern (quote it if it has spaces or special characters)<dir>where to start searching
grep -rni --include="*.yaml" "<pattern>" <dir>Case-insensitive search in one file type onlyWhat each part does
-r -nrecursive, with line numbers-iignore upper/lower case--include="*.yaml"only search files whose name matches the glob"<pattern>"regular expression to find
Tip Useful extras: -l (only file names), -v (lines that do NOT match), -w (whole word), -C 3 (3 lines of context).
find <dir> -name "*.log" -mtime +7 -deleteFind and delete log files older than 7 daysDestructiveWhat each part does
find <dir>walk the directory tree starting at <dir>-name "*.log"file name matches the pattern (quoted so the shell does not expand it)-mtime +7last modified more than 7 days ago-deletedelete each match
Tip Run it with -print instead of -delete first to see what would go.
find <dir> -type f -size +100MFind big filesWhat each part does
-type fregular files only (d = directories)-size +100Mlarger than 100 MiB ("+" = more than, "-" = less than)
find <dir> -name "*.tmp" -print0 | xargs -0 rm -fRun a command on every found file (safe with spaces)DestructiveWhat each part does
-print0print each name ending with a NUL byte instead of a newline|pipe the names to xargsxargs -0read NUL-separated names, so spaces/newlines in names are saferm -fremove the files, no prompts
tail -F <file> | grep --line-buffered "<word>"Follow a log and show only matching linesWhat each part does
tailshow the end of a file-Ffollow new lines and keep following when the file is rotated or recreated|stream new lines into grepgrep --line-bufferedprint each match immediately instead of buffering"<word>"text to look for, e.g. ERROR
sed -n '<start>,<end>p' <file>Show lines N to M of a fileWhat each part does
sedstream editor-ndon't print every line automatically'<start>,<end>p'address range (line numbers) + p = print those lines
sed -i.bak 's/<old>/<new>/g' <file>Replace text in a file in place (keep a backup)CautionWhat each part does
-i.bakedit the file in place and save the original as <file>.baks/<old>/<new>/substitute: regex <old> → <new>greplace every match on a line, not just the first
Nothing matches.
My DevOps Playbook by Angelo John Calleja