Skip to content

Instantly share code, notes, and snippets.

@jtsiomb
Last active November 15, 2022 13: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 jtsiomb/30816a04b9d4b95fe47062269dd22b3a to your computer and use it in GitHub Desktop.
Save jtsiomb/30816a04b9d4b95fe47062269dd22b3a to your computer and use it in GitHub Desktop.
Shell script to fix filenames to abide by my arbitrary "clean filename" rules
#!/bin/sh
# ___________________________________________________
# / \_
# | fixname - cleans up filenames | \
# | | |
# | author: John Tsiombikas <nuclear@member.fsf.org> | |
# | license: public domain | |
# \___________________________________________________/ |
# \___________________________________________________/
#
name=`basename "$0"`
proc_args=true
rename=false
verbose=false
dirs=false
torrent=false
keepsuffix=true
print_usage()
{
echo "Usage: $name [options] name1 name2 ... namen"
echo
echo 'Cleans up the names passed as arguments, and writes them out one per line'
echo 'options:'
echo ' - use stdin instead of arguments'
echo ' -- stop processing options, the rest are considered names to be fixed'
echo ' -d also rename directories'
echo " -r rename it, don't just print the clean name"
echo ' -t torrent-specific transforms (change dots to underscores, handle s??e?? parts, strip 720p/1080p suffixes)'
echo ' -s also transform suffixes'
echo ' -v verbose output'
echo ' -h print usage and exit'
echo 'more options will be added in the future to control exactly what fixes are performed'
}
uscore()
{
sed 's/ /_/g'
}
udashu()
{
sed 's/_-_/-/g'
}
dupl()
{
sed 's/__/_/g' | sed 's/--/-/g'
}
utrail()
{
sed 's/_$//g' | sed 's/-$//g'
}
tolower()
{
tr [:upper:] [:lower:]
}
brac()
{
sed 's/[][)(}{><]//g'
}
diacrit()
{
sed s/\'//g | sed 's/["`,;\\]//g'
}
symb()
{
sed 's/[!?@#$%^&*=]//g'
}
udots()
{
sed 's/\./_/g'
}
episode()
{
sed 's/_\(s[0-9]\+e[0-9]\+\)_/-\1-/'
}
vidcrap()
{
sed 's/_720p.*//' | sed 's/_1080p.*//'
}
torrent()
{
if $torrent; then
udots | dupl | episode | vidcrap
else
cat
fi
}
transform()
{
uscore | udashu | tolower | brac | diacrit | symb | dupl | torrent | utrail
}
fix()
{
suffix=`echo "$1" | sed 's/^.*\(\..*\)$/\1/'`
if [ "$keepsuffix" = true -a "$suffix" != "$1" ]; then
filenosuffix=`basename "$1" "$suffix"`
fixed=`echo "$filenosuffix" | transform`$suffix
else
fixed=`echo "$1" | transform`
fi
if [ "$1" = "$fixed" ]; then
return
fi
if $verbose; then
echo "$1 -> $fixed"
else
echo "$fixed"
fi
if $rename; then
mv "$1" "$fixed"
fi
}
fixed_any=false
while [ $# -gt 0 ]; do
if $proc_args; then
case $1 in
-)
shift
transform
exit 0
;;
--)
proc_args=false
;;
-r)
rename=true
;;
-v)
verbose=true
;;
-t)
torrent=true
;;
-s)
keepsuffix=false
;;
-d)
dirs=true
;;
-h)
print_usage
exit 0
;;
*)
fix "$1"
fixed_any=true
;;
esac
else
fix "$1"
fixed_any=true
fi
shift
done
if ! $fixed_any; then
for i in *; do
if $dirs || [ -f "$i" ]; then
fix "$i"
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment