View Full Version : [ubuntu] space character seperates string in container

August 27th, 2011, 01:44 PM
Hi everyone,

I try to use the terminal for multiple renaming. For this I got a script from someone and it goes like this:

for F in $(find *.pdf); do mv ${F} $(echo ${F} | sed s/pdf/txt/); done

it searches for all pdf files and changes them to txt files.
However, it does not work for files containing a space character. Here the container F (is this called a container btw.??) separates the input from the find command into single words. The foldername "hello world" would be separated in "hello" and "world".
My question is: how can this be inhibit?

Thanks in advance

The Cog
August 27th, 2011, 02:52 PM
I think putting this command in front should work:

export IFS=$'\n'
It says the inter-field-separator is newline, rather than the normal newline-or-tab-or-space.

August 27th, 2011, 03:16 PM
Also, always quote your variables.


August 27th, 2011, 03:27 PM
Check out the first (probably the most common pitfall) from the link posted by nothingspecial.

Oh, and http://mywiki.wooledge.org/BashFAQ/020

Also, instead of echo text | sed whatever you can use parameter expansion (http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion).

while IFS= read -d '' file
mv -b -- "$file" "${file%.pdf}.txt"
done < <(find ./ \*.pdf -print0)

August 29th, 2011, 11:21 AM
hey, thanks a lot for your replies, I made it with your help :)

however, its still a bit fishing in the dark with those variables. Sometimes it has to be in " ", sometimes the spaces have to be preceded with a /, sometimes not. And some more stuff which confuses me - I think I need to follow an introduction to shell programming. I found this site http://www.freeos.com/guides/lsst/ or does anyone have a tip?


August 29th, 2011, 11:48 AM
That's a very outdated guide. Check out the BashGuide, BashFAQ and BashPitfalls at http://mywiki.wooledge.org/