在几个目录之间找到一组通用文件?


9

我正在寻找比较Drupal主题的目录。一个drupal主题是一个由几个文件组成的目录,我试图找出哪些文件是必不可少的。例如,它们可能都有一个名为template.php或的文件page.tpl.php

如何找到几个目录的所有通用文件集?就我而言,所有“相同”文件(相同名称的文件)都将位于同一级别的目录中。


ukuug.org/newsletter/10.4的“ Shell Puzzle”部分中有一些答案(尽管它们是shell-golf-ish)。
James Youngman 2012年

Answers:


7

列出所有目录公用文件的所有名称(而不是路径)。

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"

或将其称为脚本文件或函数,并以目录作为参数。

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

1

您可以显示一个名称列表,这些名称按它们出现在其中的目录数排序。

find */ |              # traverse all the template directories
sort -t / -k 2 |       # sort, ignoring the first field
tr '/' '\t' |          # turn / into tabs
uniq -f 1 -c |         # count duplicates, ignoring the first field
tr '\t' '/' |          # turn tabs back into /
sort -t / -s -k 1n     # sort by the number of occurrences

0

使用Meld,您可以比较两个目录,查看哪个文件存在于一个目录中,而不是另一个目录中存在,反之亦然。它还可以显示公共文件之间的差异。


0

对于3个目录,其中一个是当前目录,两个是“ a”和“ b”,您可以像这样链接测试:

ls a/$(ls b/$(ls *.php) 2>/dev/null) 2>/dev/null

如果文件具有通用模式(如.php)并且文件名中不包含空格。

ls在脚本中使用总是有问题的,我通常不鼓励使用它,但是如果您对搜索开始目录中的所有文件有一个概述,并且它不包含空格,也不包含“ *”或“?”等特殊字符, ”,“ <”或“ |”,请保存后再使用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.