我有以下目录结构
folder
└── 01
├── test1
└── abc.bz2
└── asd.bz2
├── test2
└── 546.bz2
└── alsj.bz2
├── test3
└── aewr.bz2
└── hlk.bz2
└── test4
└── oiqw.bz2
└── abc.bz2
└── 02
├── test1
├── test2
├── test3
└── test4
└── 03
├── test1
├── test2
├── test3
└── test4
.
.
└── 31
所有test1..4
目录都包含大量从远程服务器复制的bzip文件。我知道bzip2 -t <filename.bz2>
用于检查文件是否已损坏的命令,但是我需要从上述文件夹结构中检查所有损坏的文件。那么,如何编写shell脚本来获取所有损坏文件的列表呢?我是Shell脚本和Linux的新手,所以可以提供任何帮助。
只要使用find
具有-exec
:
find . -name '*bz2' -exec sh -c 'bzip2 -t "$0" 2>/dev/null || echo "$0 is corrupted"' {} \;
来自man find
:
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command [...]
因此,find
上面的命令将查找所有以结尾的文件bz2
,并sh
在每个文件上启动一个小脚本。在{}
被发现的每个文件名替换。该参数作为第一个参数($0
)传递到sh
将bzip -t
在其上运行的脚本,并在脚本失败时进行投诉。将2>/dev/null
放弃所有其他错误消息,以保持干净的东西。
或者,您可以使用外壳。如果您使用bash
,请启用该globstar
选项以使**
递归进入子目录并检查每个bzip文件:
shopt -s globstar
for file in folder/**/*bz; do bzip2 -t "$file" || echo "$file is corrupted"; done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句