Rename all files and directories changing "\ " to "_"
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.
Rename all files and directories changing "\ " to "_"
I have a collection of files that were created on a different system that I need to make use of. Unfortunately, some of the file and directory names contain "\ " (backslash, space).
How can I rename all the files and directories, recursively, from the backslash, space to an underscore?
...and since I'm asking about bulk file handling...
The files I have are compressed (ZIP, LHA, etc.). Is there an easy way to recursively unpack all the files to a different location, keeping the directory structure?
What is the different system? Windows, MacOS etc. Is the forward slash actually in the name or with Windows just the directory character? With Windows and MacOS a forward slash is an illegal character for a filename as far as I know.
Where and how the files are extracted depends on what utility is used (unzip, 7z etc). Check out the options for extracting everything to the same directory. The unzip utility will automatically create the directory structure so no name conversion is required if that is how the files are extracted.
What is the different system? Windows, MacOS etc. Is the forward slash actually in the name or with Windows just the directory character? With Windows and MacOS a forward slash is an illegal character for a filename as far as I know.
Where and how the files are extracted depends on what utility is used (unzip, 7z etc). Check out the options for extracting everything to the same directory. The unzip utility will automatically create the directory structure so no name conversion is required if that is how the files are extracted.
The other system is an Amiga runinning AmigaOS 3.2.
The backslash is actually in the file name. I think that's how an Amiga escapes spaces in file names. I'm not aware of any other characters that need escaping on the Amiga.
The files are zipped and lzx compressed, and I have uncompressors for these installed on my Ubuntu system.
I have a collection of files that were created on a different system that I need to make use of. Unfortunately, some of the file and directory names contain "\ " (backslash, space).
How can I rename all the files and directories, recursively, from the backslash, space to an underscore?
Either of the two versions of rename can do the job. The Perl version is more capable but with such a basic task, either will do.
Just keep in mind that the backslash is an escape character and will itself have to be escaped. Here is a defanged example:
The Perl version will take any Perl expression, but the above is a basic search and replace s/// command. You could use tr/// just as well for that task. See "man rename" and for an excessively deep dive see "man perlre" The Bash reference manual will cover globstar, see "man bash" and search for that string under the section on shopt, however although it is an imposing reference manual it is worth learning to navigate.
The Perl version will take any Perl expression, but the above is a basic search and replace s/// command. You could use tr/// just as well for that task. See "man rename" and for an excessively deep dive see "man perlre" The Bash reference manual will cover globstar, see "man bash" and search for that string under the section on shopt, however although it is an imposing reference manual it is worth learning to navigate.
Thanks!
Unfortunately, I end up with and "Argument list too long" error. There are a LOT of files in these directories (300,000 files in 23,000 directories). I should be able to work something out though with that expression you provided.
Are you sure there is backslash-space and not just a space?
The GNU ls might show an extra backslash, unless you do
ls --literal
or
ls --quoting-style=literal
The suggested prename changes both backslash and space:
a "\ " becomes "__"
A for loop can take many more but not endless arguments.
find has no limit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.