Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directories with spaces - how to identify

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
I have a number of directories which should all be named with 5 digits - unfortunately some contain spaces (users - who'd have 'em?). This is what some of them are called:

63542
65438
63542
63583 68267 <- this is one directory
63582

I need to perform an action on each directory's contents & intend to use a for loop:

for dir in `ls [0-9]*`
do
... whatever


but the ls command here doesn't identify the directory names with spces - what it does is produce separate names for each part of the name. ie it tries to perform the code in the loop on 63583 AND 68267 as well as the others. I've tried

for dir in `ls &quot;[0-9]*&quot;`
do
... whatever


and

for dir in `ls '[0-9]*'`
do
... whatever


but they both explicitly look for a directory called [0-9]*.

Does anyone have any suggestions?

TIA, Chris
 
Chris:

Why don't you use the find command:

find . -type d -print

It should find directories - even those with spaces.

Regards,

Ed
 
Try running sed on your ls output before using it in the for loop
e.g.
----------------------
Server2 ~$ ls [1-9]*
12:

12 34:
Server2 ~$ ls [1-9]* | sed 's/ //g'
12:

1234:
-----------------------
cheers

amit
crazy_indian@lycos.com

to bug is human to debug devine
 
will something like.....

find . -type d -print | egrep ' '

do what you want? ( there is a single space between the quotes. but the font does not indicate that very well. )


 
The following should get all directories stating with a digit and dirname should contain the full name (with the spaces if any). Just be sure to always use &quot;$dirname&quot; with double quotes (and never $dirname) to keep the dir name with space as a single argument for all the commands you use in 'whatever'.

Code:
ls [0-9]* | while read dirname; do
   # Whatever but always use
   # &quot;$dirname&quot;
   # and never
   # $dirname
done;
 
Many thanks for the replies - esp. to Ed & dchoulette. I thought I was onto something for a while but as I get further into the code I just hit more problems - I've decided to go back to the users & beat them around the head for using spaces.

Hopefully I'll be able to convince them that its not a good idea to change the format of their directory names each time they send me a CD.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top