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'
then echo '$f exists in $2'
else echo '$f is missing in $2' sleep 5 exit

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.


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 %s\n' "$f" "$2"
        printf '%s is missing in %s\n' "$f" "$2"
        exit 1
done < "$1"
  • Thank you, I haven't really used while loops or the read function yet, so I will play around with this. – user29772 Jan 5 '13 at 17:16
  • @user29772 mywiki.wooledge.org/BashFAQ/001 – jordanm Jan 5 '13 at 17:16
  • Is the IFS= part necessary if the files in the list are just separated by white space (the file names contain no spaces :) ) – user29772 Jan 5 '13 at 17:46
  • Putting it in a more sensible way, omitting IFS= is only necessary when you want leading and trailing blanks to be stripped from the beginning and end of the lines being read. It makes no sense to omit it otherwise. – Stéphane Chazelas Jan 5 '13 at 18:04

The shell way, you'd write it:

comm -23 <(sort -u < "$1") <(ls -- "$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).

echo "Inquire if each file of a file list exists in a specific directory"
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...'

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}] "


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.