简体   繁体   中英

How can I run a command on all files in a directory and mv to a different the ones that get an output that contains 'Cannot read TIFF header'?

I'd like to remove all bad tiffs from out of a very large directory. The commandline tool "tiffinfo" makes it easy to identify them:

tiffinfo -D *

This will have an ouput like this:

00074000/74986.TIF: Cannot read TIFF header.

if the tiff file is corrupt. If this happens I'd like to take the file and move it to a different dirrectory: bad_images. I tried using awk on this, but it hasn't worked so far... Thanks!

Assuming the "Cannot read TIFF header" error comes on standard error, and assuming tiffinfo outputs other data on standard out which you don't want, then:

cd /path/to/tiffs
for file in `tiffinfo -D * 2>&1 >/dev/null | cut -f1 -d:`
do
    echo mv $file /path/to/bad_images
done

Remove the echo to actually move the files, once satisfied that the script will work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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