Skip to main content
typo
Source Link

(N.B. There are many similar questions (e.g. here, here, here, and here) but they either assume that the directory structure is one-deep, or the answers are more complex multi-line scripts.)

This is mysituationmy situation:

.
├── to_keep
│   ├── a
│   │   └── duplicate1.txt
│   └── b
│       ├── duplicate2.txt
│       └── unique1.txt
└── to_purge
    ├── c
    │   └── duplicate1.txt
    └── d
        ├── duplicate2.txt
        └── unique2.txt

Is there a simple one line script that will use the basenames found in to_keep (and its sub-directories) and remove files with the same name from to_purge (and its sub-directories)?

The two attempts I have made both fail.

(In both I have used find -print to test the command, with the intention of swapping it to find -delete when it is working.)

The first uses $():

find ./to_purge/ -print -name $(find ./to_keep/ -type f -printf "%f\n")
find: paths must precede expression: `duplicate2.txt'

The second uses xargs:

find ./to_keep/ -type f -printf "%f\n" | xargs --max-args=1 find ./to_purge/ -print -name
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt

Neither attempt works. What have I got wrong?

(N.B. There are many similar questions (e.g. here, here, here, and here) but they either assume that the directory structure is one-deep, or the answers are more complex multi-line scripts.)

This is mysituation:

.
├── to_keep
│   ├── a
│   │   └── duplicate1.txt
│   └── b
│       ├── duplicate2.txt
│       └── unique1.txt
└── to_purge
    ├── c
    │   └── duplicate1.txt
    └── d
        ├── duplicate2.txt
        └── unique2.txt

Is there a simple one line script that will use the basenames found in to_keep (and its sub-directories) and remove files with the same name from to_purge (and its sub-directories)?

The two attempts I have made both fail.

(In both I have used find -print to test the command, with the intention of swapping it to find -delete when it is working.)

The first uses $():

find ./to_purge/ -print -name $(find ./to_keep/ -type f -printf "%f\n")
find: paths must precede expression: `duplicate2.txt'

The second uses xargs:

find ./to_keep/ -type f -printf "%f\n" | xargs --max-args=1 find ./to_purge/ -print -name
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt

Neither attempt works. What have I got wrong?

(N.B. There are many similar questions (e.g. here, here, here, and here) but they either assume that the directory structure is one-deep, or the answers are more complex multi-line scripts.)

This is my situation:

.
├── to_keep
│   ├── a
│   │   └── duplicate1.txt
│   └── b
│       ├── duplicate2.txt
│       └── unique1.txt
└── to_purge
    ├── c
    │   └── duplicate1.txt
    └── d
        ├── duplicate2.txt
        └── unique2.txt

Is there a simple one line script that will use the basenames found in to_keep (and its sub-directories) and remove files with the same name from to_purge (and its sub-directories)?

The two attempts I have made both fail.

(In both I have used find -print to test the command, with the intention of swapping it to find -delete when it is working.)

The first uses $():

find ./to_purge/ -print -name $(find ./to_keep/ -type f -printf "%f\n")
find: paths must precede expression: `duplicate2.txt'

The second uses xargs:

find ./to_keep/ -type f -printf "%f\n" | xargs --max-args=1 find ./to_purge/ -print -name
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt

Neither attempt works. What have I got wrong?

edited tags
Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Source Link

Delete duplicates from another directory recursively

(N.B. There are many similar questions (e.g. here, here, here, and here) but they either assume that the directory structure is one-deep, or the answers are more complex multi-line scripts.)

This is mysituation:

.
├── to_keep
│   ├── a
│   │   └── duplicate1.txt
│   └── b
│       ├── duplicate2.txt
│       └── unique1.txt
└── to_purge
    ├── c
    │   └── duplicate1.txt
    └── d
        ├── duplicate2.txt
        └── unique2.txt

Is there a simple one line script that will use the basenames found in to_keep (and its sub-directories) and remove files with the same name from to_purge (and its sub-directories)?

The two attempts I have made both fail.

(In both I have used find -print to test the command, with the intention of swapping it to find -delete when it is working.)

The first uses $():

find ./to_purge/ -print -name $(find ./to_keep/ -type f -printf "%f\n")
find: paths must precede expression: `duplicate2.txt'

The second uses xargs:

find ./to_keep/ -type f -printf "%f\n" | xargs --max-args=1 find ./to_purge/ -print -name
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt
./to_purge/
./to_purge/c
./to_purge/c/duplicate1.txt
./to_purge/d
./to_purge/d/duplicate2.txt
./to_purge/d/unique2.txt

Neither attempt works. What have I got wrong?