쉘 스크립트를 사용하여 두 개의 다른 디렉토리에서 동일한 이름으로 파일을 비교하는 방법

StackOverflow https://stackoverflow.com/questions/119788

  •  02-07-2019
  •  | 
  •  

문제

SVN을 사용하기 전에 단순히 보관하여 프로젝트를 관리했습니다. /develop/ 디렉토리 및 편집 및 테스트 파일에서 /main/ 예배 규칙서. SVN으로 이사하기로 결정했을 때 디렉토리가 실제로 동기화되어 있는지 확인해야했습니다.

그렇다면 두 개의 다른 디렉토리에서 동일한 이름의 파일을 재귀 적으로 비교하기 위해 쉘 스크립트 [bash]를 작성하는 좋은 방법은 무엇입니까?

참고 : 위에서 사용 된 디렉토리 이름은 샘플에만 해당됩니다. 코드를 최상위로 저장하는 것이 좋습니다 :).

도움이 되었습니까?

해결책

diff 명령에는 디렉토리를 재귀 적으로 비교할 수있는 -r 옵션이 있습니다.

diff -r /develop /main

다른 팁

diff -rqu /develop /main

그것은 당신에게 그런 식으로 변경 사항을 요약 할뿐입니다 :)

만보고 싶다면 신규/누락 파일

diff -rqu /develop /main | grep "^Only

당신이 그들을 맨손으로하고 싶다면 :

diff -rqu /develop /main | sed -rn "/^Only/s/^Only in (.+?): /\1/p"

내가 사용할 수있는 차이는 재귀적인 차이를 허용합니다.

diff -r main develop

그러나 쉘 스크립트로 :

( cd main ; find . -type f -exec diff {} ../develop/{} ';' )

나는 자신의 질문에 대답하는 것이 괜찮은 곳을 읽었으므로 여기에 간다 :)

나는 이것을 시도했고 그것은 꽤 잘 작동했습니다

[/]$ cd /develop/
[/develop/]$ find | while read line; do diff -ruN "/main/$line" $line; done |less

위의 줄을 다음과 같이 편집하여 특정 파일 만 비교할 수 있습니다.

[/]$ cd /develop/
[/develop/]$ find -name "*.php" | while read line; do diff -ruN "/main/$line" $line; done |less

다른 아이디어가 있습니까?

다음은 (다소 지저분한) 내 대본의 예입니다. Dircompare.sh, 그것은 :

  • 두 개의 재귀 패스로 발생하는 디렉토리 (또는 둘 다)에 따라 어레이에 파일과 디렉토리를 정렬합니다.
  • 두 디렉토리에서 발생하는 파일은 IF에 따라 두 배열로 다시 정렬됩니다. diff -q 그들이 다른지 여부를 결정합니다
  • 그 파일을 위해 diff 주장은 동일하고 타임 스탬프를 보여주고 비교합니다

유용 할 수 있기를 바랍니다 - 건배!

edit2 : (실제로 원격 파일에서는 잘 작동합니다. 문제는 로컬 및 원격 파일 사이의 DIFF 작업 중에 처리되지 않은 CTRL -C 신호로 인해 시간이 걸릴 수 있습니다. 스크립트는 이제 트랩으로 업데이트되어이를 처리합니다. 그러나 아래의 이전 편집은 참조를 위해 남겨 둡니다.):

편집 : ... 원격 SSH 디렉토리에 서버가 충돌하는 것 외에 ~/.gvfs) ... 그래서 이것은 아닙니다 bash 더 이상,하지만 대안은 사용하는 것입니다. rsync, 예는 다음과 같습니다.

$ # get example revision 4527 as testdir1
$ svn co https://openbabel.svn.sf.net/svnroot/openbabel/openbabel/trunk/data@4527 testdir1

$ # get earlier example revision 2729 as testdir2
$ svn co https://openbabel.svn.sf.net/svnroot/openbabel/openbabel/trunk/data@2729 testdir2

$ # use rsync to generate a list 
$ rsync -ivr --times --cvs-exclude --dry-run testdir1/ testdir2/
sending incremental file list
.d..t...... ./
>f.st...... CMakeLists.txt
>f.st...... MACCS.txt
>f..t...... SMARTS_InteLigand.txt
...
>f.st...... atomtyp.txt
>f+++++++++ babel_povray3.inc
>f.st...... bin2hex.pl
>f.st...... bondtyp.h
>f..t...... bondtyp.txt
...

주목하십시오 :

  • 위를 얻으려면 후행 슬래시를 잊지 말아야합니다. / 디렉토리 이름의 끝에 rsync
  • --dry-run - 시뮬레이션 만, 파일을 업데이트/전송하지 마십시오
  • -r - 디렉토리로 되풀이됩니다
  • -v - 장점 (그러나 ~ 아니다 파일 변경 정보 관련 정보)
  • --cvs-exclude - 무시하다 .svn 파일
  • -i -"-확인-변경 : 모든 업데이트에 대한 변경 사항을 출력" "

여기에 간단한 발췌가 있습니다 man rsync 그것은 다음에 표시된 정보를 설명합니다 -i (예를 들어, >f.st...... 위의 문자열) :

The  "%i"  escape  has a cryptic output that is 11 letters long.
The general format is like the string YXcstpoguax,  where  Y  is
replaced  by the type of update being done, X is replaced by the
file-type, and the other letters represent attributes  that  may
be output if they are being modified.

The update types that replace the Y are as follows:

o      A  < means that a file is being transferred to the remote
       host (sent).

o      A > means that a file is being transferred to  the  local
       host (received).

o      A  c  means that a local change/creation is occurring for
       the item (such as the creation  of  a  directory  or  the
       changing of a symlink, etc.).

...
The file-types that replace the X are: f for a file, a d  for  a
directory,  an  L for a symlink, a D for a device, and a S for a
special file (e.g. named sockets and fifos).

The other letters in the string above  are  the  actual  letters
that  will be output if the associated attribute for the item is
being updated or a "." for no change.  Three exceptions to  this
are:  (1)  a newly created item replaces each letter with a "+",
(2) an identical item replaces the dots with spaces, and (3)  an
....

약간 비밀스러운, 실제로 - 적어도 그것은 기본 디렉토리 비교를 보여줍니다. ssh. 건배!

클래식 (System V UNIX) 답변은 dircmp dir1 dir2, 이것은 DIR1 또는 DIR2는 아니지만 DIR2에서 찾은 파일을 나열하는 쉘 스크립트였습니다. pr 명령, 따라서 제목으로 페이지를 찍음), 각 공통 파일을 분석과 비교했습니다 (동일, 다른 디렉토리가 가장 일반적인 결과였습니다).

이것은 사라지는 과정에있는 것 같습니다. 필요한 경우 IT를 독립적으로 상환 할 수 있습니다. 로켓 과학이 아닙니다 (cmp 당신의 친구입니다).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top