简体   繁体   English

查找不在数字列表中的文件

[英]Find files not in numerical list

I have a giant list of files that are all currently numbered in sequential order with different file extensions. 我有一个庞大的文件列表,这些文件当前都以不同的文件扩展名按顺序编号。

3400.PDF
3401.xls
3402.doc

There are roughly 1400 of these files in a directory. 目录中大约有1400个这些文件。 What I would like to know is how to find numbers that do not exist in the sequence. 我想知道的是如何找到序列中不存在的数字。

I've tried to write a bash script for this but my bash-fu is weak. 我试图为此编写一个bash脚本,但是我的bash-fu很弱。

I can get a list of the files without their extensions by using 我可以通过使用以下文件获取不含扩展名的文件列表

FILES=$(ls -1 | sed -e 's/\\..*$//')

but a few places I've seen say to not use ls in this manner. 但是我见过的一些地方说不要以这种方式使用ls (15 days after asking, I couldn't relocate where I read this, if it existed at all...) (问了15天后,如果有的话,我无法将其移到我阅读的地方...)

I can also get the first file via ls | head -n 1 我也可以通过ls | head -n 1获得第一个文件ls | head -n 1 ls | head -n 1 but Im pretty sure I'm making this a whole lot more complicated that I need to. ls | head -n 1但我很确定我正在使这变得更加复杂,我需要这样做。

Sounds like you want to do something like this: 听起来您想做这样的事情:

shopt -s nullglob
for i in {1..1400}; do 
    files=($i.*)
    (( ${#files[@]} > 0 )) || echo "no files beginning with $i"; 
done

This uses a glob to make an array of all files 1.* , 2.* etc. It then compares the length of the array to 0. If there are no files matching the pattern, the message is printed. 这将使用一个glob来创建所有文件1.*2.*等的数组。然后将数组的长度与0进行比较。如果没有与该模式匹配的文件,则会显示该消息。

Enabling nullglob is important as otherwise, when there are no files matching the array will contain one element: the literal value '1.*' . 启用nullglob非常重要,否则,当没有文件匹配时,数组将包含一个元素:文字值'1.*'

根据已删除的答案,基本上是正确的:

for i in $(seq 1 1400); do ls $i.* > /dev/null 2>&1 || echo $i; done
ls [0-9]* \
| awk -F. '  !seen[$1]++ { ++N }
             END         { for (n=1; N ; ++n) if (!seen[n]) print n; else --N }
'

Will stop when it's filled the last gap, sub in N>0 || n < 3000 当它填补了最后一个空白时停止, N>0 || n < 3000 N>0 || n < 3000 to go at least that far. N>0 || n < 3000至少可以走那么远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM