get directory listing with files that have spaces.

get directory listing with files that have spaces.

Post by triump.. » Thu, 21 Oct 2004 22:23:54



How can I get a directory listing of a directory that has files with
spaces in the names.

I'm looking at something like
================================
#!/bin/sh
for i in $(ls -R /home/user1/Test)
do
echo $i
done
================================
The problem with this is that it puts each seperate word on its own
line.  I cannot do anything with it like 'tail $i'.  

T.

 
 
 

get directory listing with files that have spaces.

Post by Ed Morto » Thu, 21 Oct 2004 22:47:47



> How can I get a directory listing of a directory that has files with
> spaces in the names.

> I'm looking at something like
> ================================
> #!/bin/sh
> for i in $(ls -R /home/user1/Test)
> do
> echo $i
> done
> ================================
> The problem with this is that it puts each seperate word on its own
> line.  I cannot do anything with it like 'tail $i'.  

See question 16 in the FAQ, http://home.comcast.net/~j.p.h/cus-faq.html#P

        Ed.

Quote:> T.


 
 
 

get directory listing with files that have spaces.

Post by Birger Blix » Tue, 16 Nov 2004 21:51:08



Quote:> How can I get a directory listing of a directory that has files with
> spaces in the names.

> I'm looking at something like
> ================================
> #!/bin/sh
> for i in $(ls -R /home/user1/Test)
> do
> echo $i
> done
> ================================
> The problem with this is that it puts each seperate word on its own
> line.  I cannot do anything with it like 'tail $i'.  

> T.

Your shebang is /bin/sh, but $(command) is a bash thing, that works for you since /bin/sh is a link
to /bin/bash on your machine.  `command` is the sh syntax.

Try:

ls -R /home/user1/Test | while read line
do
        echo "$line"
done

Remember , it's "$line" NOT $line

$ touch "foo bar"
$ ls foo* | while read line; do wc $line;done
wc: cannot open foo
wc: cannot open bar
        0       0       0 total

$ ls foo* | while read line; do wc "$line";done
        0       0       0 foo bar
$

a=x
b="y z"

ls $a $b

the shell expand this to ls x y z   , but ls can't know if y z is one word or two.
but ls "$a" "$b" will work, since the command will be ls "x" "y z"

/bb

 
 
 

get directory listing with files that have spaces.

Post by Stephane CHAZELA » Tue, 16 Nov 2004 22:07:12


2004-11-15, 13:51(+01), Birger Blixt:
[...]

Quote:>> ================================
>> #!/bin/sh
>> for i in $(ls -R /home/user1/Test)
>> do
>> echo $i
>> done
>> ================================
>> The problem with this is that it puts each seperate word on its own
>> line.  I cannot do anything with it like 'tail $i'.  

>> T.

> Your shebang is /bin/sh, but $(command) is a bash thing, that works for you since /bin/sh is a link
> to /bin/bash on your machine.  `command` is the sh syntax.

No, $(...) is a POSIX shell thing. /bin/sh nowadays is no more a
Bourne shell but a POSIX conformant shell (except in one or two
old-fashioned systems such as Solaris or Tru64).

Quote:> ls -R /home/user1/Test | while read line
> do
>    echo "$line"
> done

> Remember , it's "$line" NOT $line

> $ touch "foo bar"
> $ ls foo* | while read line; do wc $line;done
> wc: cannot open foo
> wc: cannot open bar
>         0       0       0 total

> $ ls foo* | while read line; do wc "$line";done
>         0       0       0 foo bar
> $

Now, you can try the same after:

touch ./-c './foo\ bar' './foo '

It should be:

ls foo* | while IFS= read -r line; do wc -- "$line"; done

(read -r is not Bourne but is POSIX).

(Still, that doesn't work for filenames with newlines)

--
Stephane

 
 
 

1. listing files in a directory which' name has a space

Hi

Fellows, either I am working with a hard one here (which is difficult
to believe considering the simpleness of the problem), or I am just
plain stupid. Either case, I have a problem.

A bourne shell file script has to go through files in directories. For
each diretory it lists all files (non-recursively) which have .ext
extension, and does some processing with them -> the filename with the
directory have to be in a shell variable inside a loop, of course the
directory is in another variable.

I tried the obvious:

FILES=`echo ${DIR}/*.ext`
for FILE in "${FILES}"
do
  blablabla
done

and few other ways, but so far, not yet a good option which would work
with directory name with spaces in it. I don't feel good about
replacing echo with ls since it completely messes up everything if
there should be directory ${DIR}/something.ext. Ok, unprobable, but
still...

Help, anyone? I will propose the one who gives most elegant solution
to be awarded with nobel prize in bourne shell scripts.

2. dosemu & compaq

3. Getting a list of new file from several directories

4. Sony SuperStation/Aiwa Bolt

5. tar, list of files and files with spaces in the name

6. Urgent: Anybody got DIPLOGIN working??

7. Directory List of files excluding files without a suffix

8. Boot hangs at Ethernet probe... Help!

9. list of files in directory and all subdirectories > files.csv

10. how to Delete files with having SPACE in their names?

11. Getting a directory listing. (BPFPH)

12. getting a directory listing without removing index.html

13. Getting a directory listing in C