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
A
contains files 1.txt
, 2.txt
, 3.txt
, …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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With