(Turns out this question was a duplicate, and what I use is the same as the top answer on How to print the name of missing files in a folder?)
For interactive use in bash, quick and easy to type and remember:
I typically use {01..99}
brace expansion to generate the expected series and look for ls
errors:
ls file_0{001..999}.jpeg > /dev/null
The redirect hides the stdout listing of existing files, but stderr remains connected to the terminal. I sometimes leave out the >/dev/null
and just scroll back in the terminal if there are fewer than ~100 files, because error messages show up first, while ls
is still reading through its args before sorting and printing them. That also verifies that I typed it right and my pattern is matching the files I did want (especially if it includes a glob).
For piping / capturing, you could do foo=$(ls ... 2>&1 >/dev/null)
to redirect stderr to the pipe, then redirect stdout to /dev/null while leaving stderr going to the shell's pipe. This would be useful for checking for empty/non-empty error output. But in a script, if you want to obtain the names of the missing files, have look at other answers instead of trying to parse it out of ls
error messages which may be internationalized etc.
If you want / if necessary, you can use quotes on other parts of the filename, like 'foo bar '{01..22}.jpg
. Or even foo\ bar\ {01..22}*.jpg
to expand to 'foo bar '01*.jpg
/ 'foo bar '02*.jpg
etc, so it works even if there's some extra unique name for some or all files somewhere other than the sequence numbers.
This works even if your numbering doesn't use leading zeros, e.g. {1..99}
instead of {01..99}
. If you want to include leading zeros inside the brace range, you can do stuff like {01..09}
which works the way you'd hope, instead of factoring them out of the brace expression like I did in the example.
Note that modern Linux systems support very long arg lists, like 128kiB of text. This method does depend on generating a command line with every file on it. That's 100% fine for interactive use: in the rare case where the list is too huge, the shell will tell you about it.
Another answer used a for
loop over the list, which would be slower (starting a separate ls
for every file), but safe even in the huge, or on a more limited system with much smaller argv limits.
seq -w 0 999
" etc., usediff
to compare files.seq -w
to get number sequence with leading zeros