ls and filenames with spaces
I'm trying to do a mass rename of files & folders in a particular directory, translating all spaces to underscores.
What I've got so far is this: Code:
for i in `ls | grep ' '`; do newname=`echo $i | tr " " "_"`; oldname=$i; echo -e "$oldname --> $newname"; done RHPS\ Schedule.txt --> RHPS_Schedule.txt it's finding RHPS\ --> RHPS\ Schedule.txt --> Schedule.txt Can anyone help me out here? Thanks! |
Try setting the seperator to a newline first:
IFS=" " |
Dangit, bit by that IFS again....one of these days I'll actually remember it before posting!
Thanks a lot :) |
On second thought, one more question: Is it possible to set that via commandline? So I could do this sort of thing without having to use a script?
|
Yeah, just type it into the shell! You may have to export IFS to get make it available in subprocesses.
|
Yes, but this is something that I noticed yesterday and confirmed just now - if I try to change IFS in a terminal, it screws up ls for that particular instance of the terminal.
[me@localhost~/shell_scripts]$ ls -l total 8 -rwxr-xr-x 1 me users 1480 2004-06-27 19:52 tolower.sh* -rwxr-xr-x 1 me users 155 2005-05-31 15:12 translate_spaces_to_underscores.sh* [me@localhost~/shell_scripts]$ export IFS=" > " [me@localhost~/shell_scripts]$ for i in `ls | grep ' '`; do newname=`echo $i | tr " " "_"`; oldname=$i; echo -e "$oldname --> $newname"; done /usr/bin/ls: --color=auto -F -b -T 0: No such file or directory [me@localhost~/shell_scripts]$ ls /usr/bin/ls: --color=auto -F -b -T 0: No such file or directory [me@localhost~/shell_scripts]$ export IFS=" " [me@localhost~/shell_scripts]$ ls -l total 8 -rwxr-xr-x 1 me users 1480 2004-06-27 19:52 tolower.sh* -rwxr-xr-x 1 me users 155 2005-05-31 15:12 translate_spaces_to_underscores.sh* Needless to say, ls works at any other time :) |
It looks like you may have an alias or wrapper set up for ls. Try just using the full path to ls.
|
Works like a charm. Again, thanks :)
|
David Ross said:
Quote:
Here's how I'm using it: Code:
IFS=" |
I always go into a subshell first when you play with IFS
then when you finish playing exit from it to get IFS back to normal |
Agreed...either that, or something like
Code:
shtuff=$IFS; |
All times are GMT -5. The time now is 03:23 PM. |