Master Unix Shell: Automate File Tasks with For Loops and Find
This guide shows how to use Unix shell for‑loops and the find command to batch‑process files, resize images with ImageMagick, limit iterations, handle numeric loops, and adapt the syntax for different shells, providing practical examples and complete command snippets.
Why Learn Shell Loops?
Unix shell scripting lets you automate repetitive file operations, a core reason many programmers turn to the command line. A for‑loop is the basic construct that defines what action the computer should perform on each item in a set, such as a file.
Classic For‑Loop Syntax
The simplest loop iterates over every file in the current directory:
$ for f in * ; do
file $f ;
doneYou can also write it on a single line:
$ for f in *; do file $f; doneCreating a Test Environment
First, make a directory and copy a few sample images into it:
$ mkdir example
$ cp ~/Pictures/vacation/*.{png,jpg} exampleEnter the directory and list its contents to verify:
$ cd example
$ ls -1
cat.jpg
design_maori.png
otago.jpg
waterfall.pngUsing the Loop to Inspect Files
Inside the loop, the file command reports basic information about each file:
$ for f in * ; do
file $f ;
doneOutput shows the file type and image dimensions for each picture.
Real‑World Example: Batch Resize Images
Suppose you have many vacation photos that are too large to email. Install ImageMagick, create a destination folder, and run a loop that scales each image to 33 % of its original size:
# Install ImageMagick (Fedora/RHEL)
$ sudo dnf install ImageMagick
# Ubuntu/Debian
$ sudo apt install ImageMagick
$ mkdir tmp
$ for f in * ; do convert $f -scale 33% tmp/$f ; doneAfter the loop, tmp contains the resized images.
Combining Multiple Commands in One Loop
You can place any number of commands between do and done. For example, resize, copy to a remote server, then delete the local copy:
$ for f in * ; do
convert $f -scale 33% tmp/$f
scp -i seth_web tmp/$f [email protected]:~/public_html
trash tmp/$f ;
doneLimiting the Loop Scope
Process only JPEG files:
$ for f in *.jpg ; do convert $f -scale 33% tmp/$f ; doneOr iterate over a numeric range:
$ for n in {0..4} ; do echo $n ; doneShell Variants
While Bash uses for, other shells like tcsh use foreach with a stricter line‑by‑line syntax:
$ foreach f (*)
foreach? file $f
foreach? endUsing find as an Alternative Loop
The find command can execute a command on each matched file via -exec. Example: resize only PNG files:
$ find . -name "*png" -exec convert {} -scale 33% tmp/{} \;Filter results, control recursion depth, and avoid processing files in the output directory:
# Search only current directory
$ find . -maxdepth 1 -name "*png"
# Include one level of sub‑directories
$ find . -maxdepth 2 -name "*png"Key Takeaways
Loops dramatically reduce manual effort when handling many files. By understanding variable expansion, command placement, and shell differences, you can build reliable, repeatable workflows for tasks such as image processing, remote uploads, or any batch operation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
