简体   繁体   中英

bash compare files between folders, and if they don't exit, do something

I have a folder with regular pictures, and another with resized ones.

The goal is to check if a picture is not resized, do the resizing and save in another folder.

I'm using an echo for simplicity, because I don't have the comparison working.

for file in ../regular/*.jpg;
do   
    img=`basename "$file"`

    FILE=./resized/$img

    if [ ! -f "$FILE" ]; then
        echo "$img NOT RESIZED"
    fi  
done

This code just echoes NOT RESIZED for all the pictures in the regular folder ie it doesn't seem to make the comparison at all.

Where is my mistake?

You should try to use diff command to compare directories:

diff -r "$PATH1" "$PATH2"
for file in ../regular/*.jpg;
FILE=./resized/$img
  1. Try to use absolute path, You can also add echo $FILE to see what scripts tries to verify
  2. If this directory contains a huge amount of files, you can exceed command line length limit (usually ~4kb-32kb)
  3. You are using quotas in basename command, why? If your images could contain spaces, you should use quotas also in "if" command, check script below
for file in ../regular/*.jpg;
do   
    img=$(basename "$file")
    if [ ! -f "./resized/$img" ]; then
        echo "$img NOT RESIZED"
    fi  
done

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