LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-24-2024, 04:04 PM   #1
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Rep: Reputation: 2
Question 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?
 
Old 01-24-2024, 04:12 PM   #2
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Original Poster
Rep: Reputation: 2
...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?
 
Old 01-24-2024, 06:05 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
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.
 
Old 01-24-2024, 07:07 PM   #4
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by michaelk View Post
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.

This is how the files are stored...
Code:
Maindir
   file1.zip
   dir1
      dir2
         file2.lha
         file3.zip
      dir3
         file4.lha
         another\ file.zip
   dir4
      file5.zip
...and how I would like the result to look...
Code:
Altdir
   file1
   file1.icon
   dir1
      dir2
         file2
         file2.icon
         file2.readme
         file3
      dir3
         file4
         file4.info
         file4.dir
            file4.readme
         another_file
   dir4
      file5
 
Old 01-24-2024, 07:23 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,311
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by Calab View Post
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:

Code:
shopt -s globstar

rename -n -v 's/[\\\s]/_/g;' **

shopt -u globstar
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.
 
Old 01-24-2024, 08:12 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
Are files individually zipped and then archived in a single file with path information?
 
Old 01-24-2024, 09:08 PM   #7
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by Turbocapitalist View Post
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:

Code:
shopt -s globstar

rename -n -v 's/[\\\s]/_/g;' **

shopt -u globstar
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.

I had not heard of shopt before. Interesting.

Last edited by Calab; 01-24-2024 at 09:29 PM.
 
Old 01-24-2024, 09:09 PM   #8
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by michaelk View Post
Are files individually zipped and then archived in a single file with path information?
No... there is a directory tree with zip files throughout. I want to keep the tree and expand the compressed files in their location on the tree.
 
Old 01-24-2024, 10:34 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
Then you need to use the find command.
 
Old 01-24-2024, 10:51 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Wouldn't a simple "for fle in **; ... " suffice (with globstar active of course) ?.
The unpacking could be done as a later step.
 
Old 01-24-2024, 11:43 PM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.
Code:
find . -type f -exec rename -n -v 's/[\\\s]/_/g' {} +
-n is "no action". Remove it for real action.
 
  


Reply

Tags
filename



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Rename Files in Linux (mv and rename Commands) LXer Syndicated Linux News 0 10-03-2021 06:15 AM
LXer: How to Bulk Rename Files in Linux with Thunar’s Bulk Rename Tool LXer Syndicated Linux News 0 11-27-2019 07:03 PM
LXer: GUI To Batch Rename Files On Linux With Exif And Music Tags Support: Inviska Rename LXer Syndicated Linux News 0 05-25-2019 12:07 PM
[SOLVED] How to remove all hidden directories and folders, and only hidden directories and folders rm_-rf_windows Linux - General 5 04-12-2016 07:28 AM
Rename all ".JPG" files to ".jpg" under all subfolders... jiapei100 Programming 4 04-25-2010 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration