List missing file names in a pattern

command linefiles

  1. I have a lot of files that start with numbers and are then hyphenated with descriptions.
    For example:

    001 - awesomesauce
    216 - stillawesomesauce
    
  2. They are organized by subdirectory

So, how would I using bash script or some built-in look inside those directories to see if I am missing a number in order? I.e. report back that I am missing 002, 128, etc. in the above example.
I know I can ls {000..216}\ -* and it will list the files and throw an error if it doesn't find it, but is there a better way to get JUST the missing files and do it recursively?

Best Answer

On a gnu setup you could run:

myarr=( $(find . -type f -name '[0-9][0-9][0-9]*' -printf '%f\n' | cut -c1-3 | sort -n) )
join -v1 <(seq -w ${myarr[-1]}) <(printf '%s\n' ${myarr[@]})

Alternatively, with zsh, you could try something like this:

myarr=( **/[0-9][0-9][0-9]*(.one_'REPLY=${${REPLY:t}:0:3}'_) )
mynums=( {001..$myarr[-1]} )
print -l ${mynums:|myarr}

It extracts the numbers (the first three digits) from each file name, sorts them and saves the result in an array - myarr. It then sets another array - mynums containing numbers from 001 up to the value of the last index (i.e. the highest number extracted from the file names) and then uses parameter expansion to remove the values in myarr from the expansion of mynums.

Related Question