Shell – find the set of common files between several directories

shell

I'm looking to compare directories of Drupal themes. A drupal theme is a directory composed of several files, and I'm trying to figure out which ones are essential. For instance, they might all have a file called template.php or page.tpl.php.

How can I find the set of all common files for several directories? In my case, all the 'same' files (those of the same name) are going to be in the same level directory.

Best Answer

List all names (not paths) of files common to all directories.

dirs=( "A dir" "B dir" "C dir" "D dir" )
find "${dirs[@]}" -maxdepth 1 -type f -name "*" -printf '%f\n' |
  sort | uniq -c | sed -n "s/^ *${#dirs[@]} //p"

Or call it as a script-file or function, with the directories as parameters.

find "$@" -maxdepth 1 -type f -name "*" -printf '%f\n' |
  sort | uniq -c | sed -n "s/^ *$# //p"