如何查找丢失的具有连续名称的文件?

如何查找丢失的具有连续名称的文件?

我有数千个名为 1.txt 2.txt 等的文件。其中一些文件丢失了。找出哪些文件丢失的最简单方法是什么?

答案1

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

ub您可以通过执行ls | sort -n或类似操作轻松找到适当的值。这依赖于文件的输出格式seq,特别是这里没有前导零。

答案2

$ 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
$

答案3

这是我将要使用的功能

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
}

答案4

其他 (bash):

comm -23 <(printf '%d.txt\n' {1..1000} | sort) <(ls *.txt |sort)

相关内容