Using BASH to print/manipulate all files in a directory
There are two files in my tmp directory.
xman@sai tmp $ lsYou are probably familiar with simple for-loop in BASH script. But, see the following:
file1.txt file version 2.txt
xman@sai tmp $ for i in `ls`Opssss, it doesnt print out each of the file names properly when there is space characters in the file names. The file names will be broken when there are spaces. Instead, you probably want to do the following:
> do
> echo $i
> done
file1.txt
file
version
2.txt
xman@sai tmp $
xman@sai tmp $ export IFS=$'\n'So now you can manipulate each of the files in a directory properly.
xman@sai tmp $ for i in `ls`
> do
> echo $i
> done
file1.txt
file version 2.txt
xman@sai tmp $
2 Comments:
At 8/24/2006 9:22 PM , Anonymous said...
Perhaps a simpler approach is:
for i in * ; do echo $i ; done
At 8/24/2006 9:52 PM , xman said...
Thanks Chris. Didnt know I can simply do that. Perhaps if you are doing more than an echo e.g. mv, then "$i" is needed instead of simply $i. Alternatively, if we had set IFS=$'\n', then we dont need the "". ;) Correct me if I'm wrong.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home