Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active January 11, 2016 23:40
Show Gist options
  • Save kjbrum/1ebaf452ae8e9c2fbecd to your computer and use it in GitHub Desktop.
Save kjbrum/1ebaf452ae8e9c2fbecd to your computer and use it in GitHub Desktop.
Rename all the files in a directory with their parent directory name.
#!/usr/bin/env bash
# Dir to Filename
# Rename all the files in a directory with their parent directory name.
# Copyright (C) Kyle Brumm <http://kylebrumm.com>
if [ "$1" = "-h" -o "$1" = "--help" ]; then cat <<EOF
Dir to Filename
Rename all the files in a directory with their parent directory name.
This will also lowercase and hyphenate the parent directories.
Note: Run in the directory that contains the directories you want to be changed.
Usage:
dir-to-filename
EOF
exit; fi
for dir in * ; do
d=${dir// /-}
d=$(echo "$d" | tr '[:upper:]' '[:lower:]')
mv "$dir" "$d"
if [ "$(ls -A "$d")" ]; then
num="1"
for file in "$d"/* ; do
ext="${file##*.}"
mv "$file" "$d/$d$num.$ext"
((num++))
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment