Skip to content

Linux & shell: Files & search

8 Linux & shell commands: Files & search. Click a command to see what each part does.

Index
8 items
Click a command to see what each part does.
  • grep -rn "<text>" <dir>Search text in all files under a folder

    What each part does

    1. grepprint lines matching a pattern
    2. -rrecurse into sub-directories
    3. -nshow the line number of each match
    4. "<text>"the pattern (quote it if it has spaces or special characters)
    5. <dir>where to start searching
  • grep -rni --include="*.yaml" "<pattern>" <dir>Case-insensitive search in one file type only

    What each part does

    1. -r -nrecursive, with line numbers
    2. -iignore upper/lower case
    3. --include="*.yaml"only search files whose name matches the glob
    4. "<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 daysDestructive

    What each part does

    1. find <dir>walk the directory tree starting at <dir>
    2. -name "*.log"file name matches the pattern (quoted so the shell does not expand it)
    3. -mtime +7last modified more than 7 days ago
    4. -deletedelete each match

    Tip Run it with -print instead of -delete first to see what would go.

  • find <dir> -type f -size +100MFind big files

    What each part does

    1. -type fregular files only (d = directories)
    2. -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)Destructive

    What each part does

    1. -print0print each name ending with a NUL byte instead of a newline
    2. |pipe the names to xargs
    3. xargs -0read NUL-separated names, so spaces/newlines in names are safe
    4. rm -fremove the files, no prompts
  • tail -F <file> | grep --line-buffered "<word>"Follow a log and show only matching lines

    What each part does

    1. tailshow the end of a file
    2. -Ffollow new lines and keep following when the file is rotated or recreated
    3. |stream new lines into grep
    4. grep --line-bufferedprint each match immediately instead of buffering
    5. "<word>"text to look for, e.g. ERROR
  • sed -n '<start>,<end>p' <file>Show lines N to M of a file

    What each part does

    1. sedstream editor
    2. -ndon't print every line automatically
    3. '<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)Caution

    What each part does

    1. -i.bakedit the file in place and save the original as <file>.bak
    2. s/<old>/<new>/substitute: regex <old> → <new>
    3. greplace every match on a line, not just the first

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…