Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rename files in a folder to match folder name (useful for htpc usage)
#!/bin/bash
# SYPOPSIS: Rename files in folder to match folder.
# CREDIT: Unamed user on superuser or superadmin. If you find this, let me know and I will add you to the credits.
# UPCOMING: Allow for secondary input such as `rename-files-to-folder $MOVIENAME` so not to rename all movies in directory just one or two.
find * -type f -maxdepth 1 | while read file
do
dirname="$(dirname "$file")"
new_name="${dirname##*/}"
file_ext=${file##*.}
if [ -n "$file_ext" -a -n "$dirname" -a -n "$new_name" ]
then
echo "mv '$file' '$dirname/$new_name.$file_ext'"
fi
done
@jcanfield

This comment has been minimized.

Copy link
Owner Author

jcanfield commented Jun 11, 2017

This script was tested on Ubuntu 16.10 and Debian 8.8. I have not had the chance to test on other system but do have friends and colleagues who will test on various platforms. All that is in use is basic bash scripting, find and mv.

@jcanfield

This comment has been minimized.

Copy link
Owner Author

jcanfield commented Jun 11, 2017

Not working on FreeNAS/FreeBSD 10.3-STABLE. Working on solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.