Spaces in file names

Spaces in file names

Post by Christia » Thu, 16 Dec 2004 00:16:26



Hello guys,

I have the following structure :

root : h235 - /tmp/test> ls -l
drwxr-xr-x    2 root     root         1024 dc 14 12:28 a dir
drwxr-xr-x    2 root     root         1024 dc 14 15:04 another one
drwxr-xr-x    2 root     root         1024 dc 14 12:28 the last one

That is, directories with spaces in their names. And I have trouble using
the following for loop :

for i in `ls`; do echo $i; done

Since the default separator is space, i get 6 arguments.

I've tried playing with IFS the following way :

root : h235 - /tmp/test> liste=`ls -F`
root : h235 - /tmp/test> echo $liste
a dir/ another one/ the last one/
root : h235 - /tmp/test> IFS=/
root : h235 - /tmp/test> for i in $liste; do echo $i; done
a dir

another one

the last one
root : h235 - /tmp/test>

Why do I get those empty lines ? And has anyone a solution ?

Thanks

Christian

 
 
 

Spaces in file names

Post by Stephane CHAZELA » Thu, 16 Dec 2004 00:25:45


2004-12-14, 16:16(+01), Christian:
[...]

Quote:> for i in `ls`; do echo $i; done

The output of ls is not meant to be post-processed.

xargs touch << \EOF
a\ \ b\ \
c \ a\ b
EOF
for i in *; do printf '<%s>\n' "$i"; done

Have a look at the FAQ.

--
Stephane

 
 
 

Spaces in file names

Post by Bruce Barnet » Thu, 16 Dec 2004 00:30:54



> for i in `ls`; do echo $i; done

  for i in "`ls`"; do echo "$i"; done
or
  ls |while read filename; do echo "$filename"; done

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

 
 
 

Spaces in file names

Post by Ed Morto » Thu, 16 Dec 2004 00:26:48



> Hello guys,

> I have the following structure :

> root : h235 - /tmp/test> ls -l
> drwxr-xr-x    2 root     root         1024 dc 14 12:28 a dir
> drwxr-xr-x    2 root     root         1024 dc 14 15:04 another one
> drwxr-xr-x    2 root     root         1024 dc 14 12:28 the last one

> That is, directories with spaces in their names. And I have trouble using
> the following for loop :

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

        Ed.

 
 
 

Spaces in file names

Post by Chris F.A. Johnso » Thu, 16 Dec 2004 00:35:21



> Hello guys,

> I have the following structure :

> root : h235 - /tmp/test> ls -l
> drwxr-xr-x    2 root     root         1024 dc 14 12:28 a dir
> drwxr-xr-x    2 root     root         1024 dc 14 15:04 another one
> drwxr-xr-x    2 root     root         1024 dc 14 12:28 the last one

> That is, directories with spaces in their names. And I have trouble using
> the following for loop :

> for i in `ls`; do echo $i; done

    There is no need for ls:

for i in *; do echo $i; done

    If you just want directories:

for i in */; do echo $i; done   ## In a Bourne shell, use */.

Quote:> Since the default separator is space, i get 6 arguments.

> I've tried playing with IFS the following way :

> root : h235 - /tmp/test> liste=`ls -F`
> root : h235 - /tmp/test> echo $liste
> a dir/ another one/ the last one/
> root : h235 - /tmp/test> IFS=/
> root : h235 - /tmp/test> for i in $liste; do echo $i; done
> a dir

> another one

> the last one
> root : h235 - /tmp/test>

> Why do I get those empty lines ? And has anyone a solution ?

> Thanks

> Christian

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

Spaces in file names

Post by Stephane CHAZELA » Thu, 16 Dec 2004 00:41:31


2004-12-14, 15:35(+00), Chris F.A. Johnson:
[...]
Quote:>     There is no need for ls:

> for i in *; do echo $i; done

[...]

But there is a need for quotes:

for i in *; do echo "$i"; done

--
Stephane

 
 
 

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

Hello all,

I am trying to tar a bunch of files that are listed in a text file, one file
name per line.
Some file names have space in the name as they are Windows users' files.

Using sed I have changed the file names in the list to have '\ ' instead of
white space, so "some file.doc" would be "some\ file.doc"

If I issue a tar command in a script

FILES=$(cat file_list.txt)
tar -cf something.tar $FILES

it craps out when reaching files with "\ " in the name prompting;
tar: some\\: Cannot stat: No such file or directory

Yet at a command prompt issuing

tar -cf something.tar some\ file.doc

works just fine.

Any ideas how I can overcome this?

TIA.

2. PD version of PPP running on SolarisX86 ?

3. archiving files with spaces in their name

4. X pauses!!!

5. How to trim off spaces from the file name

6. alternative module path

7. One problem, two solutions (spaces in file names)

8. C libraries

9. pkgmk and spaces in file names

10. File names with spaces & env vars

11. Backing up files with spaces in name

12. Space in file name

13. replace spaces with underscores in file names