LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-31-2005, 01:32 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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!
 
Old 05-31-2005, 01:43 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Try setting the seperator to a newline first:
IFS="
"
 
Old 05-31-2005, 01:50 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Dangit, bit by that IFS again....one of these days I'll actually remember it before posting!

Thanks a lot
 
Old 05-31-2005, 04:55 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
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?
 
Old 06-01-2005, 08:32 AM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

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

Last edited by eddiebaby1023; 06-01-2005 at 08:33 AM.
 
Old 06-01-2005, 09:26 AM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
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

Last edited by rose_bud4201; 06-01-2005 at 09:33 AM.
 
Old 06-01-2005, 12:52 PM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
It looks like you may have an alias or wrapper set up for ls. Try just using the full path to ls.
 
Old 06-01-2005, 01:36 PM   #8
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Works like a charm. Again, thanks
 
Old 06-30-2005, 09:31 PM   #9
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
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
 
Old 07-01-2005, 04:59 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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
 
Old 07-01-2005, 08:28 AM   #11
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Agreed...either that, or something like
Code:
shtuff=$IFS;
IFS="
"

code code code..

IFS=$shtuff
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
filenames with spaces antony.booth Programming 5 11-01-2005 04:49 AM
How to use foreach on filenames with spaces? BrianK Linux - General 3 08-09-2005 12:15 PM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
filenames with spaces - explain t3___ Linux - Newbie 14 02-17-2004 06:13 PM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM


All times are GMT -5. The time now is 03:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration