Check if files inside a text file are found under a directory

Hi all,

Please somebody help me with this:

I want to check if the files listed in a text file, are found under a directory or not.

For example: the file is list_of_files.txt, which contains inside this rows:

[root@localhost ~]# cat list_of_files
logs
errors
paths
debug
[root@localhost ~]#

I want to check if these 4 files, are found under directory, for example /tmp

If so, everything is OK, if not, print the files which are missing.

Thank you!

Hello arrals_vl,

Could you please try following script.

echo "please enter the complete path where you want to search for files."
read path
while read line
do
    NAME=`find "$path" -type f -name "$line" 2>/dev/null`
    if [[ -n "$NAME" ]]
    then
        echo "$line" file has been found.
    else
        echo "$line" file NOT found.
    fi
done < "files"

In my case output is as follows.

please enter the complete path where you want to search for files.
/tmp
chumma file NOT found.
tets100 file has been found.
test98 file has been found.
test8 file has been found.
test7 file has been found.
test6 file has been found.
test5 file has been found.
test4 file has been found.
test3 file has been found.
test2 file has been found.
test1 file has been found.

Where file named files is the input file with all file names you want to search for.

Thanks,
R. Singh

If you mean the specific directory and not searching all sub-folders, you can simplify this with:-

while read file
do
   [ -f /tmp/$file ]
   then
      print "Found file $file"
   else
      print "File $file not found."
   fi
done < list_of_files

I hope that this helps,
Robin

Depending on your version of grep , this might produce the missing files:

ls -1 /tmp | grep -vxf - list