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

Re: Unix-ify File Names



Frank Terbeck <ft@bewatermyfriend.org> wrote:
>
> find is just the tool you want to use for recursive actions on files
> (or specialized actions, like sorting). find is an external program,
> but it does not take a file list as argument, which makes it the
> ultimate choice.
>
    I took your advice to heart and started rewriting a portion of a 
script I use. It started out like so:
    for f in `find /etc/ \( -type f -o -type l \) -name "*"`; do
        fsum=$(/usr/bin/md5sum -b $f | cut -c-32) ;
        fls=$(ls -li --full-time $f) ;
        echo $fsum $fls ;
    done
At your instigation I tried this next:
    find /etc/ \( -type f -o -type l \) -name "*" -exec {
        fsum=$(/usr/bin/md5sum -b {} | cut -c-32) ;
        fls=$(ls -li --full-time {} ) ;
        echo $fsum $fls ;
    } /;

Along with screens full of junk that appeared to be the output I
expected except that:
    1) there were no newlines, 
    2) it was data relating to files in my home directory,
    3) it only appeared after I hit ^C thinking the job was hung.
While trying various quoting, escaping and other flails, I also 
recieved messages like the following:
    -bash: }: command not found
    -bash: syntax error near unexpected token `}'
    find: missing argument to `-exec'

Failing that I tried a function:

md5ls () {
    local fsum=$(/usr/bin/md5sum -b $1 | cut -c-32   2> /dev/null ) ;
    local fls=$(ls -li --full-time $1) ;
    echo $fsum $fls ;
}
find /etc/ \( -type f -o -type l \) -name "*" -exec md5ls {} \;
    find: md5ls: No such file or directory

It did finally perform correctly when I converted the function to a 
script but it's slow.

I kept playing and came up with this which is much quicker and returns
the same data though in a different format:

find /etc/ \( -type f -o -type l \) \
    -printf '%8i %y %#m %n %u %g %10s %TY-%Tm-%Td %TT  ' \
    -name "*" -exec /usr/bin/md5sum -b {} \;

The only problem with this one is that it fails when it tries to 
handle a link to a directory. There's no newline in the printf
format because the output of md5sum provides one for files and
links to files but not for links to directories or links to 
missing files.

For some reason find hides md5sum's failure so that 'set -e' at
the top of the script doesn't work. Paste this into a script of
your own and you'll see what I mean.

------- cut here -------
#!/bin/sh
#   test    of find ... -exec

set -e  # exit on error

find /usr/bin/ -name "X11" -print -exec /usr/bin/md5sum {} \;
echo $?;
/usr/bin/md5sum /usr/bin/X11;
echo $?;
------- cut here -------

With the set statement in there the second echo isn't reached
but entered on the CL after the script finishes prints 1.

Whew sorry I got a little long winded, I guess the question I 
have is how to make either the second or third form work.
    find ... -exec {command {}; command {}; command;} \;
    or
    find ... -exec function {} \;

Still learning,
Mike



Reply to: