Topic: fclean
this is a script i've been using for a while now with great success. what it does is clean up filenames so they are good to go on a linux-system. this means spaces will be replaced by underscores and special characters will be removed. also, the filename will be converted to lowercase. i've been using this mainly to clean up my mp3-files. along with my other scripts, append & firep (for appending text and doing find-and-replace), these are my go-to arsenal for making sure my mp3-filenames are in order.
#!/bin/bash
# obtained from paramthegreat @ Arch forums
# replaces spaces with underscores
# removes unwanted characters
# makes everything lowercase
# replaces hyphen after numbers with underscore
ls | while read -r FILE
do
mv -v -- "$FILE" `echo $FILE | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d "\'" | tr '[A-Z]' '[a-z]' | sed 's/\([0-9][0-9]\)-/\1_/'`
done
exit 0you just run this script in a directory and it will clean-up all files in there.
now, the time may come when you are handling an exceptionally large collection of files (mp3s, in my case), all divided in different directories and subdirectories and so on. to make stuff a little bit easier, i have also made this second script, fclean_dir, which will go down 1 level of directories and run fclean there. so, if you have a directory filled with 20 subdirectories, fclean_dir will clean up every file in each of those subdirectories.
of course, when the subdirs have more subdirs, you have to 'cd' a level deeper and run fclean_dir again from there.
#!/bin/bash
traverse() {
local d startdir
cd -- "$1" || exit 1
if [[ $(find -maxdepth 0 -type d -empty) != "." ]]; then
for d in *; do
[ -d "$d" ] && traverse "$d"
fclean
done
fi
cd .. || exit 1
}
traverse .edit: changed fclean_dir to just use my traverse-snippet and go multiple dirs deep. just to be clear, you'll also need fclean to have this work.
hope someone has some use for this!
Last edited by rhowaldt (2012-01-15 19:52:30)