Are there two single directories or there are sub-directories in the directories and you want common files of directories and sub-directories?
If there are two directories only and no sub directories in these two directories. Now you can do this.
To list the common files between two directories, if there are no sub-directories in these two directories :
Code:
ls /path/of/first/directory /path/of/second/directory | awk '{ c[$1]++; } END { for( x in c ) if( c[x] == 2 ) print x; }'
These are the common files between two directories.
Now save this list into any file.
Code:
ls /path/of/first/directory /path/of/second/directory | awk '{ c[$1]++; } END { for( x in c ) if( c[x] == 2 ) print x; }' > list.txt
This is little tricky part,Actually the list is not with the absolute path of the files, it is just file name so when you will try to copy using list, it will not be copied, it will not be able to find the file and give error.
So add absolute path in the file.
Code:
ls /path/of/directory1 /path/of/directory2 | awk '{ c[$1]++; } END { for( x in c ) if( c[x] == 2 ) print x; }' > list.txt | sed -i 's/^/\/path\/of\/directory1\//g' list.txt
Now copy the files into another directory.
Code:
cp `cat list.txt` /path/of/destination/directory
So bottom of the line, you have to run these two commands
Code:
$ ls /path/of/directory1 /path/of/directory2 | awk '{ c[$1]++; } END { for( x in c ) if( c[x] == 2 ) print x; }' > list.txt | sed -i 's/^/\/path\/of\/directory1\//g' list.txt
$ cp `cat list.txt` /path/of/destination/directory
and your common files will be copied into your destination directory.