Check whether files in a file list exist in a certain directory

The runtime arguments are as follows: $1 is the path to the file containing the list of files
$2 is the path to the directory containing the files
What I want to do is check that each file listed in $1 exists in the $2 directory

I’m thinking something like:

for f in 'cat $1'
do
if (FILEEXISTSIN$2DIRECTORY)
then echo '$f exists in $2'
else echo '$f is missing in $2' sleep 5 exit
fi
done

As you can see, I want it so that if any of the files listed in $1 don’t exist in the directory $2, the script states this then closes. The only part I can’t get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ] but I don’t know how you can make sure its checking that it exists in the $2 directory.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The shell way, you’d write it:

comm -23 <(sort -u < "$1") <(ls -A -- "${2%/}/")

(assuming a shell with support for process substitution like ksh, zsh or bash)

comm is the command that reports the common lines between two sorted files. It displays is in 3 tab separated columns:

  1. lines only in the first file
  2. lines only in the second file
  3. lines common to both files

And you can pass -1, -2, and -3 options to remove the corresponding column.

So above, it will only report the first column: the lines that are in the file list, but not in the output of ls (ls does sort the file list by default, we assume file names in there don’t contain newline characters).


In zsh, you’d use its ${A:|B} array subtraction operator:

#! /bin/zsh -
files_in_list=(${(f)"$(<$1)"})
files_in_dir=(${2%/}/*(ND:t))
print -rC1 -- ${files_in_list:|files_in_dir}

Method 2

The best way to iterate over the lines in a file is using the read builtin in a while loop. This is what you are looking for:

while IFS= read -r f; do
    if [[ -e $2/$f ]]; then
        printf '%s exists in %sn' "$f" "$2"
    else
        printf '%s is missing in %sn' "$f" "$2"
        exit 1
    fi
done < "$1"

Method 3

echo "Inquire if each file of a file list exists in a specific directory"
foundc=0
nfoundc=0
fflist=""
nflist=""
DIR_A='./my_directory'  # address directory used as target of searching
FILELIST='./file_list.txt' # file with: list of file names to search

### echo "for file in $FILELIST"
exec 3< $FILELIST  # associa lista_arquivos ao descritor 3
while read file_a <&3; do
    if [[ -s "$DIR_A/${file_a}" ]];then    # file is found and is > 0 bytes.
        foundc=$((foundc + 1)) 
        fflist=" ${fflist} ${file_a}"
        ## echo '...file ' "${file_a}" 'was found...'   
    else                          # file is not found or is 0 bytes
        nfoundc=$((nfoundc + 1)) 
        nflist=" ${nflist} ${file_a}"
       echo '...file ' "${file_a}" 'was not found...'
    fi
done

exec 3<&-  # libera descritor 3
echo "List of found files: "     "${fflist}" "
echo "List of NOT found files: " "${nflist}" "
echo "Number of files in "[$FILELIST]" found     =  [${foundc}]  "
echo "Number of files in "[$FILELIST]" NOT found =  [${nfoundc}] "

exit


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x