列出目录1中但不在目录2中的文件,反之亦然? [英] List files that are in directory1 but NOT in directory2 and vice versa?

查看:55
本文介绍了列出目录1中但不在目录2中的文件,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我开始进行bash shell脚本编写,我正在尝试为分配的脚本编写脚本,当您输入两个目录时,它将检查它们是否存在并根据错误消息显示,并且两个目录是否都存在,它将列出当前目录之间的差异.

Hey, I started bash shell scripting and I'm trying to make a script for an assignment that when you enter two directories, it will check if they exist and display according error message and if both directories DO exist, it will list the differences between the current directories.

$ cd dir-1
$ myshellscript . dir-2 (comparing . aka dir-1 against dir-2) 

输出:

Files that are in . but not in dir-2
-rw------- 1 ddddd users   1 2011-03-1 01:26 123123123

Files that are in dir-2 but not in .
-rw------- 1 ddddd users   1 2011-03-1 01:26 zzzzzzzzzzzz

到目前为止,我似乎无法检测目录是否存在,也无法列出差异:

What I have so far that does not seem to detect whether a directory exists nor list differences:

dir-1=$1
dir-2=$2

if [ $# > 2  ]
   then
      echo "Usage: compdir dir-name1 dir-name 2"
      exit 1
   elif [ $# < 2 ]
      then
         echo "Usage: comdir dir-name1 dir-name 2"
   elif [ ! -d "$@" ]
      then
         echo "/$@ is not a valid existing directory"
   else
      exit 0
fi

echo $dir-1
echo $dir-2

我必须使用的命令列表,否则我会使用comm -32 <(ls -la dir-1) <(ls -la dir-2)

List of commands I have to work with, otherwise I would have used comm -32 <(ls -la dir-1) <(ls -la dir-2)

http://dl.dropbox.com/u/20930447/index.html

推荐答案

有点粗糙-但我一直使用的最简单方法是(可以使用diff params,我通常使用不同的grep

a bit crude - but the easiest way I always use is (can play with the diff params, I typically use different grep

diff -rcw DIR1 DIR2| grep ^Only

然后您可以根据需要进行排序和格式化

then you can sort and format as you like

修改为格式(效率较低,因为我们在这里两次运行diff ...很容易解决)

Revised to format (less efficient as we are running diff twice here ... easily solved)

echo files only in $dir1
LST=$(diff ${dir1} ${dir2}| grep "^Only in ${dir1}"| sed 's@^.*: @@')
(cd ${dir1}; ls -l ${LST})

echo files only in $dir2
LST=$(diff ${dir1} ${dir2}| grep "^Only in ${dir2}"| sed 's@^.*: @@')
(cd ${dir2}; ls -l ${LST})

扩展上面的sed表达式:
s =搜索并替换
三个'@'分隔表达式(这通常是用'/'完成的)
^匹配行的开头(强制其余部分不匹配) .表示任何字符
*表示前一个表达式(.==与任何字符匹配)0-N次 :"是我从差异输出仅在X:"

Expanding on the sed expression above:
s=search and replace
the three '@' are separating the expressions (this is TRADITIONALLY done with '/')
^ matches the beginning of a line (forces the rest not to match elsewhere) . means any character
* means the previous expression (.==match any char) 0-N times ": " is what I matched on from the diff output "Only in X: "

看妈咪,别动手-现在没有"sed",它的开始就越来越粗糙了

Look Mommy, no hands - now without 'sed' its beginning to be less and less crude

XIFS="${IFS}"
IFS=$'\n\r'
for DIFFLINE in $(diff ${dir1} ${dir2}|grep ^Only); do
  case "${DIFFLINE}" in
   "Only in ${dir1}"*)  
    LST1="${LST1} ${DIFFLINE#*:}"
    ;;
   "Only in ${dir2}"*)  
    LST2+="${DIFFLINE#*:}"
    ;;
  esac
done
IFS="${XIFS}"

echo files only in $dir1
(cd ${dir1}; ls -l ${LST1})

echo files only in $dir2
(cd ${dir2}; ls -l ${LST2})

您可能想了解IFS ...它需要在bash手册中进行一些阅读,但基本上是字段分隔符...默认情况下,它们包含空格,并且我不希望循环被填充行的分数,只是完整的行-因此,在循环期间,我将默认的IFS覆盖为换行符和回车符.

You will probably want to know about IFS ... it needs some reading in the bash manual, but its basically the field separator characters ... by default they include spaces and I don't want the loop to be fed with fractions of lines, just complete lines - so for the duration of the loop I override the default IFS to just newlines and carriage returns.

顺便说一句,也许您的教授正在阅读stackoverflow,也许接下来您将不被允许使用分号;-) ...(回到'man bash'... BTW,如果您在emacs中使用'man bash'做,更加容易阅读IMO)

BTW maybe your professor is reading stackoverflow, maybe next you wont be allowed to use semicolons ;-) ... (back to 'man bash' ... BTW if you do 'man bash' do it in emacs, makes much easier to read IMO)

这篇关于列出目录1中但不在目录2中的文件,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆