I have thousands of files named 1.txt 2.txt and so on. Some of those files are missing. Which would be the easiest way to find out which files are missing?
4 Answers
ub=1000 # Replace this with the largest existing file's number.
seq "$ub" | while read -r i; do
[[ -f "$i.txt" ]] || echo "$i.txt is missing"
done
You can easily find the proper value for ub
by doing ls | sort -n
or similar. This relies on the files being in the format output by seq
, notably here without leading zeroes.
-
Thanks! I created a function for this on my .bashrc. You just saved my day :) Oct 14, 2015 at 18:09
$ ls
1.txt 3.txt
$ seq 1 10 | xargs -I {} ls {}.txt >/dev/null
ls: cannot access 2.txt: No such file or directory
ls: cannot access 4.txt: No such file or directory
ls: cannot access 5.txt: No such file or directory
ls: cannot access 6.txt: No such file or directory
ls: cannot access 7.txt: No such file or directory
ls: cannot access 8.txt: No such file or directory
ls: cannot access 9.txt: No such file or directory
ls: cannot access 10.txt: No such file or directory
$
-
5
-
@DennisWilliamson Thats worthy of an answer and probably would be the best one :)– heemaylOct 15, 2015 at 1:23
-
1@heemayl except that with "thousands of files" there's a good chance you'll blow the argument count and/or argument size limit on your system, hence the use of
xargs
.– nneonneoOct 15, 2015 at 1:46 -
@DennisWilliamson: short, clear, simple, should be the best answer! Thanks.– TerDaleJan 5, 2021 at 13:33
This is the function I will be using
missing () {
#ub gets the largest sequential number
ub=$(ls | sort -n | tail -n 1 | xargs basename -s .txt)
seq "$ub" | while read -r i; do
[[ -f "$i.jpg" ]] || echo "$i.txt is missing"
done
}
-
1