LLI Wiki — My Digital Knowledge Base

Bash for loops and the find -exec alternative

Practical shell one-liners (killing processes, lsof) plus a refresher on for loops, ranges, tcsh foreach and find -exec.

Loops let you repeat one or more commands over a list of files, processes or numbers. This article collects a few practical shell one-liners, then summarises the for loop and the find -exec alternative (based on the opensource.com article on Bash loops).

Practical one-liners

Kill every process whose command line contains the Oracle SID (use with care, kill -9 gives the processes no chance to clean up):

for a in $(ps -ef | grep $ORACLE_SID | grep -v grep | awk '{ print $2}'); do
  kill -9 $a
done

List the TCP connections with lsof, then show the change time of each matching file descriptor, most recent first:

lsof -P -i tcp | awk '{print $2,$4}' | tr -d 'u' | sort -u \
| while read pid fd; do stat --printf "%z %N\n" /proc/$pid/fd/$fd ; done | sort -r

The for loop

The general form is for <variable> in <list>; do <commands>; done. The shell does not run anything until the loop is syntactically complete, so it can be typed over several lines or on a single line.

for f in *; do file $f; done

Here f takes each name matched by * in turn, and $f is replaced by its current value.

You can place as many commands as needed between do and done. For example, to resize each image with ImageMagick into a tmp directory:

mkdir tmp
for f in *; do convert $f -scale 33% tmp/$f; done

Limiting the list

Use a wildcard pattern to process only some files:

for f in *.jpg; do convert $f -scale 33% tmp/$f; done

The list can also be a numeric range, to repeat an action a fixed number of times:

for n in {0..4}; do echo $n; done

Other shells

for is a Bash keyword. Some shells such as tcsh use foreach ... end instead, with foreach and end each on their own line:

foreach f (*)
  file $f
end

Using find instead of a loop

find selects files by path, name or depth, and -exec runs a command for each one. {} stands for the current file and the terminating semicolon must be escaped so that the shell does not consume it:

find . -name "*png" -exec convert {} -scale 33% tmp/{} \;

find descends into all subdirectories by default, including the output directory of a previous run. Restrict the depth with -maxdepth:

# current directory only
find . -maxdepth 1 -name "*png"
# current directory plus one level of subdirectories
find . -maxdepth 2 -name "*png"

Notes

Mastodon LLIMastodon SocialFosstodon