1

I want to find all files in dir1 having corresponding same file names in dir2, and delete them from dir1.

For example:

dir1: first.txt second.txt
dir2: third.txt first.txt

So I want to remove from dir1 first.txt file.

How to achieve this using Bash terminal? (not script with for loops etc. or 3'rd party program like "fdupes")

2
  • 3
    What's wrong with using for? Being able to easily use it in a one-line command is one of the best features of Unix shells. Nov 3, 2015 at 11:55
  • 1
    Sometimes the things you want to do will require loops or sequences of commands. You had good luck on the previous question. The nature of Unix is that you can do anything, but sometimes you'll need to put a few pieces together and build it yourself. When you do you can use them just like any other command. So as you find more techniques on here you can write and collect your own set of commands that you use for your special purposes. Also see:unix.stackexchange.com/questions/239986/…
    – RobertL
    Nov 3, 2015 at 11:58

4 Answers 4

4

To handle filenames with spaces:

#!/bin/bash
OPWD=$(pwd)
cd "$1"
for MYFILE in "$2"/*
do
    if [ -f "${MYFILE##/*/}" ]
    then
        echo "removing ${MYFILE##/*/}"
        rm "${MYFILE##/*/}"
    fi
done
cd "$OPWD"
4
  • 1
    Can you show how to put this in a shell script? So it could be called as finddel dir1 dir2?
    – RobertL
    Nov 3, 2015 at 11:52
  • hence the 'quick and dirty'...
    – Lambert
    Nov 3, 2015 at 12:04
  • hence the 'quick and dirty' :-) changed one of the versions to handle with filenames containing spaces...
    – Lambert
    Nov 3, 2015 at 12:26
  • This will fail unless $2 is an absolute path. Also, if the cd "$1" fails for some reason (e.g. $1 doesn't exist) you could end up removing the wrong files. Nov 3, 2015 at 21:48
4

Another quickie, also without an explicit loop. Don't forget, you can prefix the rm -f with echo to test this out.

( cd dir2 && find . -maxdepth 1 -type f -print0 ) | ( cd dir1 && xargs -0 rm -f )

You can put this into a script, replacing dir1 with "$1" and dir2 with "$2"

0

Quick answer...

#!/bin/bash
#finddel dir1 dir2

for i in $(ls $1)
    do
    [ -f $2/$i ] && echo "Deleting $2/$i" && rm -f $2/$i
done
2
  • 2
    And this one removes the files from dir2 instead of dir1....
    – Lambert
    Nov 3, 2015 at 12:43
  • 2
    Need to quote all your variables, and don't loop over ls output. Nov 3, 2015 at 16:46
-1

Use rsync :

rsync --verbose --remove-source-files xyz/* .
pop3-2.dump
pop3-3.dump
pop3.dump
popcorn-build.log

sent 852,069,995 bytes  received 124 bytes  113,609,349.20 bytes/sec
total size is 851,861,745  speedup is 1.00
2
  • 2
    This will break horrendously if the files in the two directories weren't intended to be the same. Nov 3, 2015 at 13:44
  • 2
    You're right @roaima... He said "same file name". I was thinking in remove files that was accidentally copied to another directory Nov 3, 2015 at 15:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .