opscape

Finding things cheat sheet

The most useful finding things commands on one printable page, every line checked by running it.

find Walk a directory tree and print, or act on, every file that matches a set of tests.

find . -name '*.py'
Find files by name output
find . -type d echo --- find . -type l
List only directories, or only files output
find . -type f -mtime -4 | sort
Find recently modified files output
find . -name '*.py' -exec grep -n TODO {} +
Run a command on all results at once with -exec {} + output
find . -maxdepth 1 -name '*.txt' -print0 | xargs -0 wc -l
Pipe names safely to xargs with -print0 output

whereis Locate the binary, source and manual page files for a command in the standard system directories.

whereis bash
Find a command's files output
whereis -b python3 sshd ip
Show only binaries with -b output

which Show which executable file in PATH would run for a command name.

which grep
Find the program a command runs output
which -a ls
Show every match in PATH with -a output
greet() { echo hi; } which cd greet echo; echo "which exit status: $?" type cd greet echo
which cannot see builtins, aliases or functions output

xargs Read items from standard input and run a command with them as arguments, in batches or in parallel.

printf '%s\n' web01 web02 db01 | xargs echo ping
Turn lines into arguments output
xargs -n 2 < data/hosts.txt
Limit the number of arguments per command with -n output
cut -d' ' -f1 data/hosts.txt | xargs -I{} echo "checking {} on port 22"
Put the item in the middle of the command with -I output
printf '%s\0' *.txt | xargs -0 wc -l
Handle any file name with -0 output
time (seq 4 | xargs -P 4 -I{} sh -c 'sleep 1; echo "job {} done"')
Run commands in parallel with -P output