Reputation: 2470
I have two folders that contain images. (Say folder 1
and 2
)
The images in folder 2
are a subset of the images of in folder 1
.I want to delete the images from folder 1
that are in folder 2
. How do I do this ?
EDIT
The files names are exactly same in both folders
OS - Ubuntu 16.04 LTS
Please ask for any further information that is required.
Upvotes: 0
Views: 370
Reputation: 26471
Option 1: files are pure copies with identical filenames
for file1 in dir1/*; do
[[ -e "dir2/$(basename "$file1")" ]] && echo "$file1"
done
Option 2: files are pure copies with identical filenames, but modifications could have occurred after the copy.
The following checks the content too
for file1 in dir1/*; do
file2="dir2/$(basename "$file1")"
[[ -e "$file2" ]] && cmp --silent "$file1" "$file2" && echo "$file1"
done
Option 3: files are copies, but filenames could have changed.
Have a look at fdupes
or you can do the following:
checksums=$(md5sum dir2/* | awk '{printf substr($1,length($1)-32+1,32) OFS}')
for file1 in dir1/*; do
md5sum=$(md5sum "$file1"| awk '{printf substr($1,length($1)-32+1,32)}')
[[ "$checksums" =~ $md5sum ]] && echo "$file1"
done
In the last example we make it a bit cumbersome because filenames with newlines or funny characters can introduce a <backslash>-character in the checksum (cfr. md5sum prepends '\' to the checksum)
note: replace echo "$file1"
with rm "$file1"
after inspection.
Upvotes: 2
Reputation: 19982
Use find
with \0
for parsing filenames with spaces or newlines.
Find all files in folder 1. Remove them from folder 2 (ignore errors).
dir1=1
dir2=2
find "${dir1}" -type f -printf '%f\0' | xargs -0 -i rm "${dir2}"/{} 2>/dev/null
Upvotes: 2
Reputation: 457
Here's one of the possible ways:
1.
find /path/to/folder1 -type f -printf '%f\n'|sort
find /path/to/folder2 -type f -printf '%f\n'|sort
2.
join <(find /path/to/folder1 -type f -printf '%f\n'|sort ) <(find /path/to/folder2 -type f -printf '%f\n'|sort))
3.Putting 1. and 2. altogheter:
for f in $(join <(find /path/to/folder1 -type f -printf '%f\n'|sort ) <(find /path/to/folder2 -type f -printf '%f\n'|sort))
do
echo removing $f
# uncomment below once you are satisfied with the list printed
# rm "/path/to/folder1/$f"
done
Upvotes: 2