Home > Mobile >  Is there an easy way to move several files to a directory if they are not in the directory?
Is there an easy way to move several files to a directory if they are not in the directory?

Time:12-29

I have a lot of jpg files in folder1, and some of those were edited and saved into folder2 with the same name as the original file. Is there an easy way to move all the jpg files from folder1 into folder2 that are not already in folder2?

I was trying to do this using a python script, but now I'm thinking it may be easier with unix commands.

CodePudding user response:

A simple loop in bash that checks if the file exists before moving it.

cd folder2
folder1="/path/to/folder1"
for file in *.jpg
do
    if ! [ -e "$folder1/$file" ]
    then mv "$file" "$folder1"
    fi
done

CodePudding user response:

Assuming folder1 and folder2 share the same parent directory, run from folder1:

diff -q . ../folder2 | awk '{print $4}' | xargs -I '{}' mv {} ../folder2

CodePudding user response:

You ca also do

mv --no-clobber folder1/*.jpg folder2/

or

mv -n folder1/*.jpg folder2/

to not overwrite files in folder2

  • Related