Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What I need is a 'subtract' operation on sets of files in different directories. Assuming this file system hierarchy:

A\1.txt
A\2.txt
A\3.txt

B\2.txt
B\4.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:

A\1.txt
A\3.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.

share|improve this question

1 Answer 1

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 ^_-

share|improve this answer
    
That's brilliant! Works for few files. I have a problem though: for > ~100 files I get the error "ls: write error: Invalid argument" and only a part of the output is printed (i.e. the last printed line is cut short). ls -1 by itself works just fine. Looks like xargs is reading just a chunk of what ls outputs. –  Cristi Diaconescu Aug 31 '11 at 12:19
    
Ah, updated answer. –  Sardathrion Aug 31 '11 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.