Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 06-25-2011, 09:13 PM   #1
Registered: Apr 2004
Location: Costa Rica
Distribution: slack current with (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?
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.
In this case "$source"="This is the file I want to rename"

$ echo "$source"
Any suggestions?
Running Fedorea core 14
Bash version 4.1.7
gnu sed version 4.2.1

Thank you.
Old 06-26-2011, 12:53 AM   #2
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
Try this way:
find | while read _source; do mv "${_source}" "${_source// /_}"; done
Old 06-26-2011, 02:08 AM   #3
Registered: Apr 2004
Location: Costa Rica
Distribution: slack current with (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, 04:30 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
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:
while read _source; do
    mv "${_source}" "${_source// /_}"
done < <(find ./)
See here:

(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).

for source in ./* ; do
     mv "${_source}" "${_source// /_}"
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:

rename -v 's/ /_/g' ./*
Use your google-fu for more.

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:16 PM
Batch/bulk rename with ascending dates? athens101 Linux - Software 3 04-27-2009 07:32 PM
bulk rename files atiman Linux - Newbie 5 03-05-2009 01:42 AM
shell command for bulk 'rename' ashrafulkarim Linux - Newbie 5 12-17-2008 04:46 AM
rename file with "mv" not working for folder? Niceman2005 Linux - Newbie 2 03-13-2005 10:34 PM

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

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