Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

files with same name in different folders

I am looking for a command line that can do some operations on two files that have the same names but in different folders.

for example if

  • folder A contains files 1.txt, 2.txt, 3.txt, …
  • folder B contains files 1.txt, 2.txt, 3.txt, …

I would like to concatenate the two files A/1.txt and B/1.txt, and A/2.txt and B/2.txt, …

I'm looking for a shell command to do that:

if file name in A is equal the file name in B then: 
    cat A/1.txt B/1.txt
end if

for all files in folders A and B, if only names are matched.

like image 798
Bioinfoguy Avatar asked Dec 07 '22 07:12

Bioinfoguy


1 Answers

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

cd dir1
find . -type f | sort > /tmp/dir1.txt
cd dir2
find . -type f | sort > /tmp/dir2.txt
comm -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
like image 69
simonzack Avatar answered Dec 13 '22 09:12

simonzack