2

I want to create a loop in Linux script that will go thru the folders in one directory and will copy photos to one folder and will overwrite photos that have the same name. Can anyone point me in the write direction?

3 Answers 3

3
find /path/to/source -type f -exec cp -f {} /path/to/destination \;

Would that work? Keep in mind, that will overwrite files without asking.

If you want it to confirm with you before overwriting, use the -i flag (for interactive mode) in the cp command.

Sign up to request clarification or add additional context in comments.

Comments

0
find /path/to/source -type f | xargs -I {} file {} | grep <JPEG or image type> | cut -d ":" -f1 | xargs -I {} cp -rf {} /path/to/destination

With this you can find tune your copy with selecting only the image type.

Comments

0

Actually, you need not to loop through folders for finding photos using script, find command will do that job for you.

Try using find with xargs and cp

find source_dir -type f -iname '*.jpg' -print0 | xargs -0 -I {} cp -f {} dest_dir

Replace *.jpg with format of your photo files. (e.g. *.png etc.)

Note the use of -f option of cp, since you want to overwrite photos with the same name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.