Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice on changing filenames with embedded spaces 1

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
0
0
US
I am in the process of migrating a zillion files from Windows to Linux (Ubuntu) and of course the file names contain many embedded spaces.

I've been trying for days now to create a script that will properly rename these files by replacing the spaces with underscores. Using a list of fully qualified path/filenames created with find I've attempted to take each line and perform the renames. Here is an example:

rename "sites/staging.xxx.xxxxx.edu/files/2009-10 Financial Declaration Form.doc" "sites/staging.xxx.xxxxx.edu/files/2009-10_Financial_Declaration_Form.doc"

When run I see NO errors at all. Yet, the file(s) are NOT renamed. I've also tried escaping all the special characters (i.e.: "\." for dots, "\ " for spaces) with no better results.

Would someone please suggest a method that might work or something that I am doing incorrectly to resolve this roadblock?

Thanks in advance...

 
It appears that you are expecting the rename command in Ubuntu to work the same way as it does in Windows, with the first argument the current file name and the second argument the name you want to change it to. That's probably not a good assumption. Type "man rename" to get the actual syntax on your platform, but on Suse linux rename takes three arguments - the first is the pattern you want to change, the second is the pattern you want to change it to, and the third is the file or files you want to rename. So if you execute the following command

Code:
rename " " "_" ./*

you will rename any files in the current directory by replacing the first occurrence of a space in the file name with an underscore. Any files that don't contain a space in the name will be unaffected. So you could execute the rename command repeatedly, each time changing one more space to "_", until all files have been renamed as you wish.

Note: the linux command that works similarly to a Windows rename is "mv
 
Hi

That is the syntax of the [tt]rename[/tt] from the util-linux and util-linux-ng packages.

Ubuntu and other Debian-based distributions used to have the [tt]rename[/tt] from the Perl examples.
Code:
[gray]# general syntax[/gray]
rename 's/ /_/g' ./*

[gray]# in this case this is enough[/gray]
rename 'y/ /_/' ./*
But as karluk wrote, see your [tt]rename[/tt]'s man page.


Feherke.
 
Thanks guys. I did review 'man rename' and kind of missed the different format. I like Feherke's approach but I still have a question:

While this rename should work by renaming the file in place, I need to know exactly which file(s) were changed from old to new. The reason is that I then need to perform a sed substitution to change all those old filenames to the new filenames in various other file contents and DB records. For clarity I did not show the variables used in my rename command. For the old filename I use ${OLDFN} and the new I use ${NEWFN}. After doing a sed substitution of "\ " to "\_" on the NEWFN I compare the old and new and if different I then execute the rename AND output a sed command to a sed.script file with the substitution (s|${OLDFN}|${NEWFN}|g).

Finally, why does a simple: mv "${OLDFN}" "${NEWFN}" command not work? It usually states that the target is not a directory.

Awaiting your replies - Thanks
 
Hi

BobMCT said:
Finally, why does a simple: mv "${OLDFN}" "${NEWFN}" command not work? It usually states that the target is not a directory.
The destination has to be a directory only if the source is also a directory. If you collected the file names with [tt]find[/tt], have you restricted it to -type f ? Is it possible that some directory names also contain space ?
BobMCT said:
After doing a sed substitution of "\ " to "\_" on the NEWFN
Just a note : no need for [tt]sed[/tt] :
Code:
NEWFN="${OLDFN// /_}"


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top