Re: for i in $(find ...) # with whitespaces in result?

From: --==[bman]==-- (vze58jgq_at_verizon.net)
Date: 08/31/03


Date: Sun, 31 Aug 2003 14:09:03 GMT

Don't be afraid to use quotes. They will protect you from shell
interpreting white spaces as field separators:

for i in "$(find . -type f)"; do
        ls -l "$i"
done

It will do what you want. Be careful, however where you do your 'find'.
If directory contains thousands of files, you might get "argument list
too long" error message. Then, '-name 'something' is your friend as
part of your 'find' command...

Peter Barth wrote:
> Hi,
> How do I use the result of a find command
> in a shell script if the result can contain filenames
> with whitespaces?
> I tried a couple of things enclosing with "" and alike,
> but didn't get there. Any help appreciated.
> First try enclosed.
> ---
> (/tmp)$ ls /tmp/hatesh | more
> a
> b c
> d
> (/tmp)$ lstmp
> This works
> | /tmp/hatesh/a
> | /tmp/hatesh/b c
> | /tmp/hatesh/d
> This doesn't
> : /tmp/hatesh
> : /tmp/hatesh/a
> : /tmp/hatesh/b
> : c
> : /tmp/hatesh/d
> This doesn't either
>
>> /tmp/hatesh /tmp/hatesh/a /tmp/hatesh/b c /tmp/hatesh/d
>
> (/tmp)$ cat lstmp
> #!/bin/sh
> echo "This works"
> for file in /tmp/hatesh/*; do
> echo "| " $file
> done
> echo "This doesn't"
> for file in $( find /tmp/hatesh ); do
> echo ": " $file
> done
> echo "This doesn't either"
> for file in "$( find /tmp/hatesh )"; do
> echo "> " $file
> done
> ---
> Cheers
> - Peter

-- 
    __   _
   / /  (_)__  __ ____ __
  / /__/ / _ \/ // /\ \ /
/____/_/_//_/\_,_//_\_\ Life is what you make of it...


Relevant Pages

  • Bash-4.0 available for FTP
    ... Unlike previous bash distributions, this tar file includes the formatted ... The shell has been changed to be more ... rigorous about parsing commands inside command substitutions, ... Changes have been made to the Readline library being released at ...
    (gnu.announce)
  • Why newbies dont RTFM...
    ... Even though I've used Linux before, I've never had to do any ... BASH BUILTIN COMMANDS ... last command exited within ... unless the shell is not exeâ ...
    (comp.os.linux.misc)
  • Re: Great SWT Program
    ... None of the nasty things that you have said or implied about me are at ... treat the file as input (manually invoking the command interpreter ... script, copy the line into that within the editor, exit, and invoke ... the shell script. ...
    (comp.lang.java.programmer)
  • Re: ksh silently ignores function if mistakenly not autoloaded
    ... Here's the order of execution. ... This really takes place prior to command ... Shell scripts, with all their power, have one major drawback - they ... In the Korn Shell, there are two separate syntaxes for defining ...
    (comp.unix.shell)
  • Re: Shell function - accessing an Access secure db (.MDW file)
    ... > Hi - Does anyone know the correct syntax for the shell function. ... You can use the Shell command. ... To execute internal DOS command (Dir, Copy, ... Private Declare Function OpenProcess _ ...
    (microsoft.public.vb.general.discussion)