files with same name in different folders files with same name in different folders shell shell

files with same name in different folders


For simple things maybe will be enough the next syntax:

cat ./**/1.txt 

or you can simply write

cat ./{A,B,C}/1.txt

e.g.

$ mkdir -p A C B/BB$ touch ./{A,B,B/BB,C}/1.txt$ touch ./{A,B,C}/2.txt

gives

./A/1.txt./A/2.txt./B/1.txt./B/2.txt./B/BB/1.txt./C/1.txt./C/2.txt

and

echo ./**/1.txt

returns

./A/1.txt ./B/1.txt ./B/BB/1.txt ./C/1.txt

so

cat ./**/1.txt

will run the cat with the above arguments... or,

echo ./{A,B,C}/1.txt

will print

./A/1.txt ./B/1.txt ./C/1.txt #now, without the B/BB/1.txt

and so on...


Try this to get the files which have names in common:

cd dir1find . -type f | sort > /tmp/dir1.txtcd dir2find . -type f | sort > /tmp/dir2.txtcomm -12 /tmp/dir1.txt /tmp/dir2.txt

Then use a loop to do whatever you need:

for filename in "$(comm -12 /tmp/dir1.txt /tmp/dir2.txt)"; do    cat "dir1/$filename"    cat "dir2/$filename"done


Will loop through all files in folder A, and if a file in B with same name exists, will cat both:

for fA in A/*; do    fB=B/${f##*/}    [[ -f $fA && -f $fB ]] && cat "$fA" "$fB"done

Pure , except the cat part, of course.