LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   ls and filenames with spaces (http://www.linuxquestions.org/questions/programming-9/ls-and-filenames-with-spaces-328942/)

rose_bud4201 05-31-2005 01:32 PM

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
Once the script spits out what I want it to, the "echo" will be replaced with a "mv $oldname $newname". However, it doesn't yet. It's choking on all the spaces in the filenames, and instead of giving me the expected

RHPS\ Schedule.txt --> RHPS_Schedule.txt

it's finding

RHPS\ --> RHPS\
Schedule.txt --> Schedule.txt

Can anyone help me out here?

Thanks!

david_ross 05-31-2005 01:43 PM

Try setting the seperator to a newline first:
IFS="
"

rose_bud4201 05-31-2005 01:50 PM

Dangit, bit by that IFS again....one of these days I'll actually remember it before posting!

Thanks a lot :)

rose_bud4201 05-31-2005 04:55 PM

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?

eddiebaby1023 06-01-2005 08:32 AM

Yeah, just type it into the shell! You may have to export IFS to get make it available in subprocesses.

rose_bud4201 06-01-2005 09:26 AM

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 :)

david_ross 06-01-2005 12:52 PM

It looks like you may have an alias or wrapper set up for ls. Try just using the full path to ls.

rose_bud4201 06-01-2005 01:36 PM

Works like a charm. Again, thanks :)

tonyfreeman 06-30-2005 09:31 PM

David Ross said:
Quote:

Try setting the seperator to a newline first:
IFS="
"
This is a new one for me ... and it worked like a charm! Thanks! This IFS thingy is really going to save my bacon when dealing with spaces in filenames :-)

Here's how I'm using it:

Code:

IFS="
"

for i in $(ls -1 *.part[0]1.rar)
do
        echo -e "\n $i \n"
        PAR=${i%%.part*}.par2
        if [ -e "$PAR" ]
        then
                /usr/bin/par2repair "$PAR" && /usr/local/bin/rar e "$i" && mv *.avi *.mpg *.iso ../
        fi
done


bigearsbilly 07-01-2005 04:59 AM

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

rose_bud4201 07-01-2005 08:28 AM

Agreed...either that, or something like
Code:

shtuff=$IFS;
IFS="
"

code code code..

IFS=$shtuff



All times are GMT -5. The time now is 07:05 AM.