LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-25-2011, 08:13 PM   #1
mago
Member
 
Registered: Apr 2004
Location: Costa Rica
Distribution: slack current with 2.6.16.18 (still off the hook)
Posts: 284

Rep: Reputation: 33
Bulk file rename not working as expected


Hello everyone,

Here comes a question that most likely than not is very simple, but I think I'm blocked and I don't see it.

Y want to rename a bunch of files and directories to remove the space on the names, easy enough right?
Code:
for source in $(find ./); do target=$(echo "$source"|sed -e 's/ /_/g'); mv -f "$source" $target; done
Well, I thought that should have work but the problem is that $source comes up broken, when I run it with echo instead of mv I get the echo with broken names.
Code:
In this case "$source"="This is the file I want to rename"

$ echo "$source"
This
is
the
file
I
want
to
rename
$
Any suggestions?
Running Fedorea core 14
Bash version 4.1.7
gnu sed version 4.2.1


Thank you.
 
Old 06-25-2011, 11:53 PM   #2
allez
Member
 
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
Try this way:
Code:
find | while read _source; do mv "${_source}" "${_source// /_}"; done
 
Old 06-26-2011, 01:08 AM   #3
mago
Member
 
Registered: Apr 2004
Location: Costa Rica
Distribution: slack current with 2.6.16.18 (still off the hook)
Posts: 284

Original Poster
Rep: Reputation: 33
Thank you, that did the trick.

Seems to be that echo cannot carried the spaces, it changes them with \n even before the substitution.

Thank you allez!
 
Old 06-26-2011, 03:30 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A for loop simply breaks the input (the part after "in", or the positional parameters) up into separate "words" and processes them individually. If you want to take "per line" input from an external command or file then you should use a while+read loop instead.

Also, while the above suggestion works in this situation, using a pipe to feed the loop is not the best option because the loop will run in a subshell and any variables set inside it will be lost when the loop terminates. This is the recommended way:
Code:
while read _source; do
    mv "${_source}" "${_source// /_}"
done < <(find ./)
See here: http://mywiki.wooledge.org/BashFAQ/024

(Edit: That find command will also include directories in the input, so you may have problems if you're trying to do this recursively, as it will rename the directory before the files inside it, and so subsequent mv commands will fail. You should use the -type f option to limit it to files only, then run it again with -type d to process the directories ).

Incidentally, if all you want to do is grab files from the current directory, then you don't need find at all, just globbing, and you can go back to using a for loop (globbing expansion takes place after word breaking, so filenames won't be broken up).

Code:
for source in ./* ; do
     mv "${_source}" "${_source// /_}"
done
Finally, instead of writing your own scripts, you might consider using one of the many useful bulk renaming programs that are already out there. I like qmv from renameutils, for example, and the perl-based rename utility that comes with some distros. So I just do this to remove spaces from filenames:

Code:
rename -v 's/ /_/g' ./*
Use your google-fu for more.
http://www.google.com/search?&q=linux+bulk+rename

Last edited by David the H.; 06-26-2011 at 03:37 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rename bulk files scofiled83 Programming 11 06-26-2009 10:16 PM
Batch/bulk rename with ascending dates? athens101 Linux - Software 3 04-27-2009 06:32 PM
bulk rename files atiman Linux - Newbie 5 03-05-2009 12:42 AM
shell command for bulk 'rename' anon201 Linux - Newbie 5 12-17-2008 03:46 AM
rename file with "mv" not working for folder? Niceman2005 Linux - Newbie 2 03-13-2005 09:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:08 PM.

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