for name in dir1/* dir2/*; do
bname=${name##*/}
if [ ! -e "dir1/$bname" ]; then
printf '"%s" not in dir1\n' "$bname"
elif [ ! -e "dir2/$bname" ]; then
printf '"%s" not in dir2\n' "$bname"
fi
done
This will iterate over all names in dir1
and dir2
. $bname
will be the basename of the name ($name
without path).
If the (base)name can not be found in dir1
, then this is reported. Otherwise, if it's not found in dir2
it's reported.
This copes with filenames containing embedded newlines and should run with /bin/sh
or any compatible sh
shell.
This could potentially be expanded to more than two directories. With bash
:
dirs=( dir1 dir2 dir3 dir4 )
for dir in "${dirs[@]}"; do
for name in "$dir"/*; do
bname=${name##*/}
for tdir in "${dirs[@]}"; do
if [ ! -e "$tdir/$bname" ]; then
printf '"%s" not found in %s\n' "$bname" "$tdir"
fi
done
done
done
$ tree
.
|-- dir1
| `-- filename 1
|-- dir2
| |-- filename 1
| |-- filename 3
| `-- hello
world
`-- script.sh
$ sh script.sh
"filename 3" not in dir1
"hello
world" not in dir1