Last active
January 11, 2016 23:40
-
-
Save kjbrum/1ebaf452ae8e9c2fbecd to your computer and use it in GitHub Desktop.
Rename all the files in a directory with their parent directory name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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