[SOLVED] how do i remove ALL special characters in file names with one line command
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
i have written a script that will remove any one of the special characters or white spaces and replace it with an underscore, below is my command that will remove just white spaces
Code:
find . -type f -name "* *" | while read src; do mv "$src" `echo $src | tr " " "_"`; done
and to remove [
Code:
find . -type f -name "*[*" | while read src; do mv "$src" `echo $src | tr "[" "_"`; done
etc. etc.
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.
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:]\.-_' '_'
tr works on what you actually give it and -exec separates the file names with a newline. Again see the manual page for both programs.
For what it's worth, there is a perl script rename on most systems that can process file names with s/// tr and more
Last edited by Turbocapitalist; 02-22-2017 at 01:55 PM.
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.
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.
That means replace all punctuation and space characters to colons.
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.
Last edited by MadeInGermany; 02-22-2017 at 03:25 PM.
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!
Honestly, in shell/commandline always have "quotes" around the filenames, and you don't need to rename them.
atjurhs, perhaps we should ask why you wish to rename the files? If it's that the weird characters are causing difficulties renaming and copying using variables, then you can probably solve that by wrapping the variables in quotes.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.