0

I currently have a directory with 100 files of 4 types, for a total of 400 files. I would like to find which ones are missing. My current script is:

for((i=1; i<=100; i++)); do name="File_Type_1_${i}.RData"; 
[[ ! -e "$name" ]] && echo "missing $name"; done

for((i=1; i<=100; i++)); do name="File_Type_2_${i}.RData"; 
[[ ! -e "$name" ]] && echo "missing $name"; done

for((i=1; i<=100; i++)); do name="File_Type_3_${i}.RData"; 
[[ ! -e "$name" ]] && echo "missing $name"; done

for((i=1; i<=100; i++)); do name="File_Type_4_${i}.RData"; 
[[ ! -e "$name" ]] && echo "missing $name"; done

which is annoying to have to run 4 times. Is there a way to run everything at once? The

  • From the duplicate, this should work: ls -d File_Type_{1..4}_{1..100}".RData >/dev/null –  Nov 03 '18 at 03:23

1 Answers1

3
ls -l File_type_{1..4}_{1..100}.RData > /dev/null

This command uses brace expansion to generate all 400 filenames, then asks ls to list them, only we immediately redirect the output to /dev/null, dropping it. ls will complain about the missing files to stderr; for example:

ls: cannot access 'File_type_1_99.RData': No such file or directory
ls: cannot access 'File_type_3_42.RData': No such file or directory

Therein lie the missing files!

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255