$_ CLI In Depth
Learning progress 0%
Build · Expansion, Quoting & Word Splitting

Build: A Safe Batch File Renamer

List planned renames NUL-safely, perform one rename with the end-of-options marker, and skip a collision without overwriting.

~40 min Intermediate 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: List matching names with a NUL-safe loop

The directory photos holds three entries, including one with a space. The loop reads each name whole, and the case statement keeps the names that contain the word draft. Run this in /workspace.

BASH6 lines
cd /workspace/photos
while IFS= read -r -d '' f; do
  case ${f##*/} in
    *draft*) printf '%s\n' "${f##*/}" ;;
  esac
done < <(find . -mindepth 1 -maxdepth 1 -print0) > /workspace/plan.txt

Run cat /workspace/plan.txt. It should list two names, one with a space and one with a dash.

Step 2: Rename matches with mv and quoted names

A glob is safe for this job because each matching name arrives as one word. The end-of-options marker protects names that begin with a dash, and the quotes protect the space. Run this in /workspace.

BASH2 lines
cd /workspace/photos
for f in *draft*; do mv -- "$f" "${f//draft/done}"; done

Run ls in the photos directory. You should see done one.txt and done-two.txt, and final.txt unchanged.

Step 3: Skip a rename when the target exists

The directory photos holds draft-x.txt and done-x.txt. Renaming the first to the second would overwrite the existing file, so the script must test first. This branch records the skip instead of moving the file.

BASH6 lines
cd /workspace/photos
if [ -e done-x.txt ]; then
  echo 'skip: done-x.txt exists' > /workspace/conflict.txt
else
  mv -- draft-x.txt done-x.txt
fi

Run cat /workspace/conflict.txt. The draft file should still be there.

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH44 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: List matching names with a NUL-safe loop
# (the lab prepares this first)
# cd /workspace
# rm -rf photos plan.txt
# mkdir photos
# cd photos
# touch &#039;draft one.txt&#039; draft-two.txt final.txt
# 
cd /workspace/photos
while IFS= read -r -d &#039;&#039; f; do
  case ${f##*/} in
    *draft*) printf &#039;%s\n&#039; "${f##*/}" ;;
  esac
done < <(find . -mindepth 1 -maxdepth 1 -print0) > /workspace/plan.txt

# Step 2: Rename matches with mv and quoted names
# (the lab prepares this first)
# cd /workspace
# rm -rf photos plan.txt
# mkdir photos
# cd photos
# touch &#039;draft one.txt&#039; draft-two.txt final.txt
# 
cd /workspace/photos
for f in *draft*; do mv -- "$f" "${f//draft/done}"; done

# Step 3: Skip a rename when the target exists
# (the lab prepares this first)
# cd /workspace
# rm -rf photos conflict.txt
# mkdir photos
# cd photos
# printf &#039;old&#039; > done-x.txt
# printf &#039;new&#039; > draft-x.txt
# 
cd /workspace/photos
if [ -e done-x.txt ]; then
  echo &#039;skip: done-x.txt exists&#039; > /workspace/conflict.txt
else
  mv -- draft-x.txt done-x.txt
fi

Lessons this build uses