MAPK MAPK - 4 months ago 9
Linux Question

Check if the full path to multiple files are valid

Say I have 1000 files and full path to them (example,

mylist
). I may have some of the files (i.e. full paths) invalid. Is there any unix/linux method to check whether a list of files exist in the directories?
for example, I have these three files where file3.txt doesn't exist in mypath, but it exist in
mypath/mypath2/file3.txt
. I want to identify that this file doesn't exist.

mylist:

mypath/mypath2/file1.txt
mypath/mypath2/file2.txt
mypath/file3.txt

Answer

Given a list of files in a file called mylist:

while read -r filepath; do
  if [[ -f "$filepath" ]]; then
    echo "File '$filepath' exists"
  else
    echo "File '$filepath' does not exist"
  fi
done <mylist

In the above, "exists" means "is a regular file".