[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: shell scripts and spaces in file/folder names

Joris Huizer <joris_huizer@yahoo.com> writes:

> I'm trying to learn to write shell scripts... but I
> have a problem: I've got some files which have spaces
> in their names (yeah I know it's ugly... but it'll be
> some searching to find those)
> for i in `ls .`
> do
>   #...
> done

You need to take some care in doing this, as I'm sure you've noticed.
:-)  I tried:

  touch 'foo bar'
  for i in *; do echo "$i"; done

from the command line, and it did what I expected.  You need to be
careful that, everywhere you use a filename variable, it's in double
quotes, so that the shell turns it into a single "word" for
command-line parsing purposes.  Using shell globbing is almost
certainly better than calling ls in a subshell.

("find . -name '* *' -print" will print the names of all of the files
in and under the current directory with spaces in their names, if that
helps you clean things at all.  If you're using find and xargs in a
spaceful world, consider using find ... -print0 | xargs -0 ....)

David Maze         dmaze@debian.org      http://people.debian.org/~dmaze/
"Theoretical politics is interesting.  Politicking should be illegal."
	-- Abra Mitchell

Reply to: