how do i remove ALL special characters in file names with one line command
hi guys,
daily i come across files that were not created by me but by windows users, who i have no control over their naming conventions, so they name files with white spaces and special characters. as an example: Code:
1234_rev(a) draft [1].pdf Code:
find . -type f -name "* *" | while read src; do mv "$src" `echo $src | tr " " "_"`; done Code:
find . -type f -name "*[*" | while read src; do mv "$src" `echo $src | tr "[" "_"`; done so i have to run the command once for white spaces and then once for each special character (this can add up to several commands to rename the files) so i'd like to give just one command to remove white spaces and special characters all at once. Todd |
Are '[' and ']' special characters?
I think you have to define what names are acceptable. |
Finding names with whitespaces AND ['s and ]'s could be done with:
Code:
find . -type f \( -name "*\ *" -o -name "*[*" -o -name "*]*" \) |
Look at the manual page for tr more closely, it can include a set of characters. And any of those characters can even be class of characters.
Code:
tr -s '[:space:][]()' '_____' Code:
tr -cs '[:alnum:]\.-_' '____' |
I take it you are looking for something more a long the lines of this?
Code:
# touch Dnfs^754£.txt Code:
# cat rename.sh Code:
# sh rename.sh |
Turbo,
both of your commands run just fine, but both of them also leave a trailing underscore Code:
1234_rev_a_draft_1_.pdf_ r3 aaah, no |
Ok. The replacement was a bit aggressive, I was thinking along slightly different lines ( -print0 ) and the extra underscore would be because of the [:space:] class. Substitute it with a regular space in the first one or add a newline in the second one and it should stop replacing the trailing return.
Code:
tr -cs '\n[:alnum:]\.-_' '_' For what it's worth, there is a perl script rename on most systems that can process file names with s/// tr and more |
Your comment about Windows and not having any control over naming conventions is incorrect. In fact you would struggle to create those file names under Windows GUI.
They are far more likely to have been created by a piece of software that was written by someone without specific computer knowledge. |
thanks Turbo! although they are my "friend" the man pages are often cryptic to me
dave, that's very possible, so i should shoot a programmer for this :) |
The large software package suppliers tend to use inexperienced people to write the boring bits and they like to use "meaningful names" even if those names contravene all sensible standards.
And talking of meaningful names: I have been on call out most of my life for various corporations. Most of their serious computing processing is done in batch overnight so it was rare for me to get an uninterupted night with nothing going wrong. One program that failed and required debugging was very cleverly written using very funny names and read almost like a story rather than a program. Not exactly what you want at 3:30 in the morning. |
Code:
mv "$filename" `echo "$filename" | tr "[:punct:][:space:]" ":"` Then send them back to your Windows guys and watch them fighting with the colons! ;) Honestly, in shell/commandline always have "quotes" around the filenames, and you don't need to rename them. |
Are 'filename[[' and 'filename[[[[[' to be renamed to the same name?
Perhaps if 'filename' already exists, you need to generate a name such as 'filename(1)' or alternatively halt and ask for a bit of interactive intelligence! |
Windows file system would add the subscript if names ended up the same.
|
Quote:
|
Turbo,
for me it's far easier when dealing with many files a day to just change out the special characters of all the files once, then when i need to open whichever file with an application from the cmnd line, tab complete works fine thanks for your help! Todd |
All times are GMT -5. The time now is 02:13 AM. |