Problem :
What I need is a ‘subtract’ operation on sets of files in different directories. Assuming this file system hierarchy:
A1.txt
A2.txt
A3.txt
B2.txt
B4.txt
… I’d like to remove all files under A
that also exist under B
(no recursion, and I only need to compare file names).
The result should be:
A1.txt
A3.txt
(nothing changed in B
)
The target OS is Windows – either command line or a GUI tool. I’m also OK with a UNIX command-line approach – I have GnuWin32 installed.
Solution :
Create a list of files in B, replace B by A and remove them.
/bin/ls -1 B/ | xargs -I {} echo rm A/{}
remove the echo once you have it. For example:
$ ls A/
1 2 3
$ ls B/
1 2
$ /bin/ls -1 B/ | xargs -I {} echo rm A/{}
rm A/1
rm A/2
If you have many files, I suggest doing something akin to
#!/bin/sh
for f in `ls -1 B/*`
do rm A/$f
done
Making sure that files with spaces and control character work is left as an exercise to the reader ^_-