I am trying to delete files from /Dir1
that don't exist in /Dir2
. The problem is that the first few characters match, but not the last few. For example.
/Dir1
abc_def.png
xyz_xyz.png
/Dir2
abc_ghi.png
So as long as "abc" matches, I don't want to delete the file even though the last few characters are different. The only file I want to delete is xyz.png
because it does not exist in /Dir2
. How would I go about doing this?
Assuming you want to match part of filename before first underscore (as in your example), you can use this code:
cd /Dir1
for f in *_*; do
[[ -f /Dir2/"${f%%_*}"* ]] || echo rm "$f"
done
Once satisfied, remove echo
before rm
.