Question: Using the bash language with the following architecture

```
 - directory1
        - entries
 - directory2
        - entries

```

I want to delete the duplicate entries, and just keep one of them.
I am using the following script :

```
for dir in *
do 
    [ -d "$dir" ] && for file in "$dir"/*
    do 
        filename=$(basename "$file")
        find . -name "$filename" -not -path "*$dir*" -exec rm "{}" \;
    done
done

```

The problem I have been facing is that when a directory gets emptied, `filename="*"`. Therefore find will return all entries and delete everything.

I can't use any tools since the machine I am using cannot access the internet.

Answer: 
Adding the following test did the trick:

```
for dir in *
do 
    [ -d "$dir" ] && [ "$(ls -A $dir)" ] && for file in "$dir"/*
    do 
        filename=$(basename "$file")
        find . -name "$filename" -not -path "*$dir*" -exec rm "{}" \;
    done
done

```
With your solution, I don't see how you *keep one*. If file 'f' is in directories 'A' and 'B': 

* when your outer for loop is on 'A', the find will print B/f.
* -------------------------------'B', ------------------- A/f.

However, you don't do anything, you just print.

I propose this, without nesting a find in a for in a for:

there\_can\_be\_only\_one.sh:

```
#!/bin/sh
tmp=$(mktemp -d)
dir=$1

for d in "$dir"/*; do
  if [ -d "$d" ]; then
    for f in "$d"/*; do
      bf=$(basename "$f")
      if [ -a "${tmp}/$bf" ]; then
        rm "$f"
      else
        touch "${tmp}/$bf"
      fi
    done
  fi
done

rm -rf "$tmp"

```

Basically, a (n empty) new temporary directory, $tmp, is created. Then, for each file $f, if $tmp contains a file with the same filename, then remove that file $f else create a file with that filename in $tmp.

```
yarl@badaboum:~/work$ ls
foo  there_can_be_only_one.sh
yarl@badaboum:~/work$ ls -R foo/
foo/:
dir0  dir1  dir2

foo/dir0:
de  hello  mammouth

foo/dir1:
arg  de  heart  hello

foo/dir2:
arg  blarg  de

yarl@badaboum:~/work$ ./there_can_be_only_one.sh foo/
yarl@badaboum:~/work$ ls -R foo/
foo/:
dir0  dir1  dir2

foo/dir0:
de  hello  mammouth

foo/dir1:
arg  heart

foo/dir2:
blarg

```