LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-30-2007, 11:32 AM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
ls with whitespace in filenames


I'm writing a shell script to process some files on an ntfs filesystem drive to get them off and onto an ext3. In the windows spirit, these directories have names like "My Really Cool Dir/", etc. When I write

Code:
for directory in `ls -ad */`; do
...
done
the whitespace is used as a string delimiter, which is no good for my purposes. This is an issue with ls, so
Code:
for directory in `ls -ad */ | sed 's| |\ |g'`; do
isn't fixing it.

I'm sure this is childsplay for someone with more experience! Help?
 
Old 06-30-2007, 11:56 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you're probably better off doing whatever your doing with a find command...

find /var/where/ever -exec rm {} \;

to, in this example, sequentially delete each file found. each file name stays as a known variable within the script, so whitespace never matters.

to stick with ls, try

ls -1ad | while read filename
do
echo file name is $filename
done

as read will read one line at a time, not one word at a time. also you can screw around with the IFS shell variable so for delimits on a newline instead of a space...

Last edited by acid_kewpie; 06-30-2007 at 11:58 AM.
 
Old 06-30-2007, 12:24 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you have filenames with spaces in them in variables, be sure to put double quotes around the variables when using the variables in a script.

For example:
Code:
for texts in /mnt/windows/Documents\ and\ Settings/jhwilliams/My\ Documents/*.[tT][xX][tT]; do
cp "$file" ~/Documents/"$file"
done
for pdfdocs in /mnt/windows/All\ Users/My\ Documents/*.PDF; do
mv "$file" ~/Documents/"${file// /_}"
done
The Second line replaces spaces in the files with the the underscore character.

If you have two files in a director: "file name1.ext" and "another file.ext", if yo u have a command like
for file in *;
do
# something here
done

The first time in the loop, $file contains "file name1.ext", and not "file\ name1.ext". The "\" characters are used when typing such files in the shell interactively to escape the whitespace, preventing the shell from separating the filename into two arguments. Once you press enter, their job is done. They have already done their job.
 
Old 06-30-2007, 08:14 PM   #4
dr34m3r
LQ Newbie
 
Registered: Jun 2007
Distribution: Fedora
Posts: 7

Rep: Reputation: 0
just put this after the shebang (#!)

Code:
IFS=$'\n'
The variable IFS (or inter-field separator) in BASH determines what characters act as delimiters. By default space, tab and newline characters are used as delimiters. The variable can also be used to define custom delimiters like comma, colon etc..

After this you can use your directory variable for referring to your windows folders

Last edited by dr34m3r; 06-30-2007 at 08:16 PM.
 
Old 06-30-2007, 11:00 PM   #5
phidor
Member
 
Registered: Apr 2005
Location: Timaru, New Zealand
Distribution: Manjaro
Posts: 105

Rep: Reputation: 15
On the Command line, with a file or a directory named "Split Name", I refer to it as "Split*" and have no trouble. In my experience a file or directory name with white space in it invites trouble sooner or later. I use data file or folder names, not only free from white space but (at least after the first level where defaults like "Pictures" run wild), with entirely lower case characters. I retain folder names like "Pictures" etc with their uppercase starting character because sometimes software is also twisted that way.

For speed and ease using the Command Line, I've found it handy with file or folder names, to try to have a unique filename character or two at the start of the name. Commands like
"cp P*/k*/p*/f*/g* De*" save a lot of typing!

Phidor.
 
Old 07-01-2007, 03:14 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well "Split*" will match "Split Names", "Split Peas" and "Split Mlik"... that's not going to any use in the long run is it?

seems to me you've probably not got as comfortable with bash tab auto completion as would really suit... no need for crude workarounds whenbash will fill in the rest of the file name for you correctly.
 
Old 07-01-2007, 03:34 AM   #7
phidor
Member
 
Registered: Apr 2005
Location: Timaru, New Zealand
Distribution: Manjaro
Posts: 105

Rep: Reputation: 15
Thanks Chris! Tried it and very pleased to get this lost marble spinning! Phidor.
 
  


Reply



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
whitespace in fstab senyahnoj Linux - General 5 02-26-2007 07:31 AM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
Proper whitespace formatting on stdout R00ts Programming 5 07-30-2005 04:27 PM
Handling whitespace in For loop rvoigt Linux - General 1 04-06-2005 06:57 AM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:35 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration