What if you could tell your computer to handle hundreds of files for you in seconds?
Why Looping over files and directories in Bash Scripting? - Purpose & Use Cases
Imagine you have a folder full of hundreds of photos, documents, or logs. You need to rename, move, or check each file one by one by opening them manually.
Doing this by hand is slow and boring. You might miss some files or make mistakes. It's like trying to count grains of sand one by one -- it takes forever and is easy to mess up.
Looping over files and directories lets your script automatically visit each file, one after another. It's like having a helper who quickly and carefully checks every item for you without missing any.
mv photo1.jpg new_folder/ mv photo2.jpg new_folder/ mv photo3.jpg new_folder/
for file in *.jpg; do mv "$file" new_folder/ done
It lets you handle many files quickly and safely, saving time and avoiding errors.
Say you want to resize all images in a folder for a website. Looping lets you apply the resize command to every image automatically, instead of opening and editing each one.
Manual file handling is slow and error-prone.
Looping automates repetitive tasks over many files.
This saves time and reduces mistakes in file management.
Practice
for file in *; do echo "$file"; doneSolution
Step 1: Understand the loop variable and pattern
The loop uses*which matches all files and directories in the current folder.Step 2: Analyze the command inside the loop
Each matched item is stored infileand printed withecho.Final Answer:
It prints the names of all files and directories in the current folder. -> Option DQuick Check:
Loop over * prints all names [OK]
- Thinking * matches only files, not directories
- Assuming the loop deletes files
- Believing echo runs only once
Solution
Step 1: Understand the pattern for directories
The pattern*/matches only directories in the current folder because directories end with a slash.Step 2: Check the loop and echo command
The loop iterates over directories and prints their names correctly.Final Answer:
for dir in */; do echo "$dir"; done -> Option CQuick Check:
*/ matches directories only [OK]
- Using * matches files and directories, not only directories
- Using -f to check for directories instead of -d
- Using absolute path /* instead of current folder
file1.txt, file2.txt and a directory docs/?for item in *; do
if [ -f "$item" ]; then
echo "File: $item"
else
echo "Not a file: $item"
fi
doneSolution
Step 1: Identify file and directory checks
The script uses[ -f "$item" ]to check if the item is a file.Step 2: Apply checks to each item
file1.txt and file2.txt are files, so print "File: ..."; docs is a directory, so print "Not a file: docs".Final Answer:
File: file1.txt File: file2.txt Not a file: docs -> Option AQuick Check:
-f true for files only [OK]
- Assuming directories pass -f test
- Confusing -f with -d
- Expecting directory names with trailing slash
for item in *; do
if [ -f "$item" ]; then
echo "$item"
fi
doneSolution
Step 1: Understand the test used
The script uses[ -f "$item" ]which checks if the item is a file, not a directory.Step 2: Identify correct test for directories
To check directories,[ -d "$item" ]should be used instead.Final Answer:
It uses -f to check files instead of -d for directories. -> Option BQuick Check:
Use -d to test directories [OK]
- Confusing -f and -d tests
- Assuming * matches only directories
- Ignoring quotes around variables
Solution
Step 1: Understand the goal
We want to count files only, so we must check each item with[ -f "$item" ].Step 2: Check each option
count=0 for item in *; do if [ -f "$item" ]; then count=$((count + 1)) fi done echo $count correctly loops over all items, increments count only if item is a file, then prints count.Final Answer:
count=0 for item in *; do if [ -f "$item" ]; then count=$((count + 1)) fi done echo $count -> Option AQuick Check:
Use -f test and increment count [OK]
- Counting directories instead of files
- Counting all items without checks
- Using */ pattern which matches directories only
Ready to prove it?
Pass the test and this pattern moves unknown
