You are not logged in.
Pages: 1
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)
Offline
Do you just save these scripts in ~/bin and then when in the required directory via Terminal execute what ever the filename/command was?
#! Waldorf - 64bit - Xfce
Offline
well, you just save them in a directory that is inside your $PATH variable.
echo $PATHin my case they're in ~/scripts, but if you'd rather use bin, go ahead. if it is in ~/bin and that is in your path, you can just call the scriptname from where-ever you are on your system and it should work.
oh damn and i always forget this because i have a script that does it for me (@script, probably in my signature too)... but you need to do
chmod +x YOURSCRIPTNAMEHEREto make it executable.
gotta remember to put that in with every script i post...
Offline
^ Thanks, since 'bin' is there I tend to just use that.
oh damn and i always forget this because i have a script that does it for me (@script, probably in my signature too)... but you need to do
chmod +x YOURSCRIPTNAMEHEREto make it executable.
gotta remember to put that in with every script i post...
I would of done that but as you say probably a good idea to put that in.
#! Waldorf - 64bit - Xfce
Offline
^ people gotta just start using my @script, and this problem wouldn't exist. can't really understand why that script hasn't gotten more attention, as it works so fantastically. @script SCRIPTNAME and you have a chmodded script in an open editor.
well, as long as i love it 
Offline
^I'm going to use it tonight, so your effort isn't totally in vane. 
I have a question though, do you use Dropbox? As i noticed that when Dropbox is installed the folder created in ~/ is 'Dropbox' and not 'dropbox'. Would changing the capitalistion of this folder prevent Dropbox from working/syncing?
I shall probably find out later myself anyway. 
#! Waldorf - 64bit - Xfce
Offline
Thanks for those Rhowald, 'fclean' works a charm. 
#! Waldorf - 64bit - Xfce
Offline
thanks kri5, glad you like it! i use it myself all the time. and nice that you're also trying out @script. if you like it and continue to use it, please drop by its thread and leave a message there too.
about the Dropbox-dir: i think changing the name to lowercase will prevent it from working, but am not 100% sure. also be careful with this script around downloads from torrent-files etc, as your torrent-client won't be able to find the files when they're suddenly all lowercase (had that happen myself so speaking from experience here
)
Offline
Pages: 1
Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.