[PLUG] Bash file test for multiple files

Daniel Hedlund daniel at digitree.org
Fri Feb 11 15:59:56 UTC 2011


On Fri, Feb 11, 2011 at 06:13, Michael Rasmussen <michael at jamhome.us> wrote:
> bash file test -e will tell you if a single file exists but
>    if [ -e ~/Maildir/.aspambin/cur/* ]; then
> will croak with "too many arguments" if there is more than one file

# might be a cleaner way, but this should work...
if [ "$(ls ~/Maildir/.aspambin/cur/* 2>/dev/null)" ]; then ...


> conversly a construct like
>    for S in ~/Maildir/.aspambin/cur/*
> will attempt to act on a file named * if the directory is empty.

How about one of the following?:

# doesn't handle files w/ spaces, possible injection
for S in $(ls ~/Maildir/.aspambin/cur) ...

# handles files with spaces and other problematic characters
find ~/Maildir/.aspambin/cur -print0 | xargs -0 -n1 ...


Cheers,

Daniel Hedlund
daniel at digitree.org



More information about the PLUG mailing list