Find out whether files exist.

I have the following data stored in a file.
1 /home/file13 /home/file2
2 /home/file41 /home/file654
3 /home/file61 /home/file45
4 /home/file81 /home/file43
...

I want to print the first column provided the files represented by the second and third column exist.
How to do that?

Hi, try with this:

while read linea
do
   fich1=$(echo $linea | cut -d" " -f2)
   fich2=$(echo $linea | cut -d" " -f3)

   if [ -e $fich1 ] && [ -r $fich1 ]
   then
      awk '{print $1}' $fich1
   fi
   if [ -e $fich2 ] && [ -r $fich2 ]
   then
      awk '{print $1}' $fich2
   fi

done < IN_FILE

SORRY!

I understood another thing....I have to improve my english.

Please try:

while read num f1 f2
do
   if [ -s "$f1" ] && [ -s "$f2" ]; then
      echo $num
   fi
done < file

-s will check for non zero size too. if you dont want to do that. just use -f

1 Like

Thank you, anchal_khare.
The code works fine.