How to use wildcards with ls to find files that are missing in a numeric sequence

lswildcards

I'm trying to list missing files in a sequence in the terminal. While many answers existed here, they are not generic enough that I managed to adapt it to my situation. So if you can make a generic enough answer that will work for more people, please do it.

I'm doing ls {369..422}.avi >/dev/null to list missing files but I can't use * to match anything that ends with .avi. How can I do this?

The numbers are not in the end of the file but in the middle. So I should need something like *numbers*.avi

Best Answer

ls *{369..422}*.avi >/dev/null

This will first generate patterns like

*369*.avi
*370*.avi
*371*.avi
*372*.avi
*373*.avi
*374*.avi

through the brace expansion, and then ls will be executed with these patterns, which will give you an error message for each pattern that can't be expanded to a name in the current directory.

Alternatively, if you have no files that contain * in their name:

for name in *{369..422}*.avi; do
    case "$name" in 
        '*'*) printf '"%s" not matched\n' "$name" ;;
    esac
done

This relies on the fact that the pattern remains unexpanded if it did not match a name in the current directory. This gives you a way of possibly doing something useful for the missing files, without resorting to parsing the error messages of ls.

Related Question