check if two files exists at the same time in one location

Hi friends,

I am trying to check if the two files i am expecting are created in a specific location.

if both files does not exist, do an echo

if [ [ ! -f out1.txt ] -a [ ! -f out2.txt ] ]; then
   echo "files not found"
fi

It gives me the following message:

bash: [: too many arguments

Please help! :slight_smile:

if [ ! -f run ] && [ ! -f run.cpp ]
then
  echo "Files not found!"
fi

--ahamed

In the following, you can see with the -e (exists) option.
1st passes, 2nd fails.

$ ls m*.txt
m12.txt  m45.txt  m45a.txt

$ if [ -e m45.txt -a -e m12.txt ] ; then echo "yes" ; else echo "no"; fi
yes

$ if [ -e m45.txt -a -e m13.txt ] ; then echo "yes" ; else echo "no"; fi
no

I dont think you need the inner [ & ], so:-

if [ ! -f out1.txt -a ! -f out2.txt ]; then
   echo "both files not found"
fi

.....unless BASH is very different to sh, ksh etc. It is one if statement with an and in it. You might want to carefully consider your logic though. Here, you are saying if out1.txt and out2.txt both do not exist, then display the message. If one exists, you would not get the message. Is that what you want?

If you are checking that both should exist, then you could consider:-

  • If out1.txt does not exist or out2.txt does not exist, then error
  • If out1.txt and out2.txt both exist, continue, else error

These might be better. It just depends what you want to acheive.

I hope that this helps,

Robin
Liverpool/Blackburn
UK