![]() |
looking for a method of stripping special characters from filenames
Hi. So I've run into the ages old issue of special characters in filenames. Specifically, in my music collection, there are several files I have which I have ripped on older Windows computers, etc, which have things like quotes and question marks in the filenames (files auto-named by whatever ripping software I was using at the time, based on the cddb lookups). This problem wreaks havoc when I attempt to copy albums or songs over to a portable player, or to other disks, or even just opening the file on gnu/linux players, because the files or directories containing the special characters are just skipped with an error. It can be extremely annoying when I try to load an entire album and then find out one song in the middle wasn't added to the playlist due to this. As stated above, files and directories containing special characters are also omitted whilst copying.
Since I have a fairly large music collection, I'd like to find a semi-automated way to just navigate through an entire directory and remove all special characters. I would write a script to do this, but last time I tried, I wound up making a mistake and renamed several files into nothing (based on a one-liner a friend gave me), losing the files. Is there a fairly uncomplicated way to do this? I tried searching around a bit, but didn't find anything specifically describing this. Thanks. |
Look at the tr command. (man tr for details.)
Something like this find ./ -type f -exec mv '{}' tr <args> '{}' ';' (where "<args>" are the tr specification you want) might work. Note: the find command I suggest is from memory, and not checked. I'm unsure if the '{}' argument-substitution can be used twice.:scratch: |
There's probably a better way to do it, but I would just go through each special character one by one and write a short script to find any files/dirs with that character and remove it. Something like:
Code:
find dir -iname "* *" -print0 | while read -d $'\0' file; do echo mv "$file" "${file// /_}"; doneCode:
$ ls -l dir/Code:
$ ls -l dir/ |
You can do a for loop script using sed. I created two filenames with special characters for this example
Code:
ls -1Code:
for i in *; do echo mv "$i" "$(echo $i | sed 's/[!@#\$%^&*()?_]//g' | tr -s " ")"; doneWhatever character(s) you want omitted, just insert it between the brackets i.e in bold It is good to use echo to preview the results before committing the actual conversion. If satisfied with the preview, then remove the echo in blue. This is optional. If you want to capitalize the first character in each word, then add an extra sed statement. Code:
for i in *; do echo mv "$i" "$(echo $i | sed 's/[!@#\$%^&*()_?]//g;s/^.\| [Aa-Zz]/\U&/g' | tr -s " ")"; doneThe new sed statement is in bold Hope this helps. Remember keep the echo part to preview the results. Then remove it to make the real changes. |
| All times are GMT -5. The time now is 06:56 PM. |