Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2471545 to your computer and use it in GitHub Desktop.
Save anonymous/2471545 to your computer and use it in GitHub Desktop.
for album in event-*/album-*/; do
echo "$album";
# Execute every directory in its own shell, so we do not need to worry
# about "cd -" on errors, etc.
(
cd "$album";
# Fill the positional parameter array with all files. "*" is expanded
# alphabetically by default.
set -- *;
# First, determine the number of digits to use for the zero-padded
# number.
num_digits=1;
while [ $# -gt $((10 ** $#)) ]; do
let num_digits++;
done;
# Rename all files.
i=0;
for file; do
let i++;
new_file="$(printf "%0${num_digits}d.%s" $i "${file##*.}")";
# Unless they already have the right name.
[ "$new_file" = "$file" ] && continue;
# Make sure to ask before overwriting, and be verbose about the operation.
mv -vi "$file" "$new_file";
done;
);
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment