Reputation: 33
In bash, I have a folder containing some subfolders and files as
folder1/
folder2/
script_1.ext
script_2.ext
script_4.ext
...
script_N.ext
, where N is a known large number. I would like to know which numbers are missing in the filenames. I am trying to come up with simple codes that I can extract numbers from the filenames (in the example, it is 1,2,4,...,N) and find missing numbers in 1:N (for example, 3).
I am very new to bash scripts. I tried to find similar questions and answers but I couldn't. Any input will be appreciated!
ps. I have tried
ls -1 | sed 's/script_//' | sed 's/.ext//'
and successfully extracted the numbers, but I am unsure how to save those numbers and compare with 1,...,N to obtain missing numbers.
Upvotes: 1
Views: 66
Reputation: 50775
Basically I want to find numbers in 1,...,N for a known N, that do not exist in the filenames.
Presuming file_
and .ext
are common patterns among your files; loop through 1 to N, build filenames, check their existence and report if they're missing.
N=10 # known N
for ((i=1;i<=N;i++)); do
f=file_$i.ext
if [ ! -f "$f" ]; then
printf '%s is missing\n' "$f"
fi
done
Upvotes: 1
Reputation: 2853
Extracting the numbers is fine. Comparing with 1..N can be done as follows.
Assuming the numbers are in range 1..100:
diff <(ls -1 | sed 's/script_//' | sed 's/.ext//' | sort -n) <(echo "$(seq 1 100)") | sed -n 's/> //p'
Or if the upper bound is below 10:
diff <(ls -1 | sed 's/script_//' | sed 's/.ext//') <(echo "$(seq 1 9)") | sed -n 's/> //p'
Upvotes: 0