Xman Blog Inside

Thursday, August 24, 2006

Using BASH to manipulate all files in a directory - Cont

Chris pointed out I can simply do
for i in *; do echo $i ; done
I can recall I struggled on the problem file names broken into parts. I spent much time to solve it. There is nothing much to argue if all other programs except your current script were written correctly. If you forgot to " " your $i, see what will happen below.
xman@sai tmp $ ls -Q
"text1.txt" "text version 2.txt"
xman@sai tmp $ for i in * ; do mv $i $i.txt ; done
mv: target `2.txt.txt' is not a directory
xman@sai tmp $
Now we have two solutions.

Solution I:
for i in *; do mv "$i" "$i.txt" ; done
But do remember to double quote your file names. ;)

Solution II:
export IFS=$'\n'
for i in * ; do $i $i.txt ; done
There is nothing wrong if you use "$i".

Currently, I'm not sure what will break what will not if I set IFS=$'\n'. Certainly, if you are using some broken scripts that forgot to " ", with IFS=$'\n', you are still fine. Then, I'll rather locally use IFS=$'\n'. ;) If IFS=$'\n' does break other things, please tell me!

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home