Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Last active June 11, 2017 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcanfield/2974916c96ffb7357385f3e9e9b6674f to your computer and use it in GitHub Desktop.
Save jcanfield/2974916c96ffb7357385f3e9e9b6674f to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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