简体   繁体   中英

how to " If files with same name and extensions exists in other folders" keep them Bash Script

1. folder1  
hello.zip
world.apk

 2. folder2
hello.zip
world.apk
testing.apk
---------------------------------

check the folder1 for the file with their name and extension, and keep the same files in folder2 and delete the rest of files in folder2 .

**Thanks in advance.** 

Does it make sense to flip the logic around and do something equivalent? That is, what if you look in folder 2 and then delete any file that doesn't appear in folder 1. The advantage here is that the loop is much simpler.

# loop over files in folder2
for path2 in folder2/*; do

    # convert path2/foo.ext to path1/foo.ext
    path1=folder1/"$(basename "$path2")"

    # test if path1/foo.ext exists, and remove path2/foo.ext if it doesn't
    [[ -e "$path1" ]] || rm -v "$path2"
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