Check if multiple files exist in directory

39

Solution 1

test -f won't work for multiple files expanded from wildcards. Instead you might well use a shell function with null-redirected ls.

present() {
        ls "$@" >/dev/null 2>&1
}

if [ $# -lt 1 ]; then
    echo "Please enter the path"
    exit
fi
path=$1
if ! present $path/cc*.csv && ! present $path/cc*.rpt && ! present $path/*.xls; then
    echo "All required files are not present\n"
fi

Btw is it fine to use &&? In this case you get not present only when there're no files named cc*.csv or cc*.rpt or cc*.xls in $path.

Solution 2

if [ [ ! f $path/cc*.csv ] && [ ! f $path/cc*.rpt ] && [ ! f $path/*.xls ] ];then
    echo "All required files are not present\n" fi

check[6]: !: unknown test operator

I think you forgot the operand 'f' is an unkown operand --> '-f'

if [ [ ! -f $path/cc*.csv ] && [ ! -f $path/cc*.rpt ] && [ ! -f $path/*.xls ] ];then
    echo "All required files are not present\n"
fi

In your case you all files have to be missing before it will echo your ... it of course depend on your goal.

I'm not able to check it out in ksh on AIX.

Share:
39

Related videos on Youtube

user2171720
Author by

user2171720

Updated on September 18, 2022

Comments

  • user2171720
    user2171720 almost 2 years

    I was wondering if these two ways to write this are interpreted the same by Node/Express:

    app.use(express.bodyParser());
    app.use(express.logger('dev'));
    app.use(express.methodOverride());
    

    vs

    app
      .use(express.bodyParser())
      .use(express.logger('dev'))
      .use(express.methodOverride());
    

    I think the later looks cleaner, but I was wondering if this will still be asynchronous.

    • Stéphane Chazelas
      Stéphane Chazelas almost 9 years
      If they are required, then it seems to me your script should fail if any of them is missing, not if all of them are missing.