예

2개의 디렉토리 a/b/. 여기에 다음 파일이 포함되어 있다고 가정해 보겠습니다.

a/
a/foo/1
a/bar/2
a/baz/3

b/
b/foo/1
b/bar/2

a/foo/1와 는 b/foo/1동일하지만 a/bar/2b/bar/2는 다릅니다.

a/에 병합한 후 b/다음을 얻고 싶습니다.

a/
a/bar/2

b/
b/foo/1
b/bar/2
b/baz/3

설명

  • a/foo/b/foo/(재귀적으로) 동일하므로 제거합니다 a/foo/.
  • a/bar/2그리고 b/bar/2서로 다르기 때문에 우리는 아무것도 하지 않습니다.
  • a/baz/에만 존재 a/하지만 에는 존재하지 않으므로 b/로 옮깁니다 b/baz/.

이에 대한 기성 쉘 명령이 있습니까? rsync효과가 있을 것 같은 느낌이 들지만 rsync.

답변1

이 작업을 수행하는 특정 명령을 내가 알고 있다고 말할 수는 없습니다. 하지만 해싱을 사용하면 이 작업을 수행할 수 있습니다.

아래의 순진한 예:

#!/bin/bash

# ...some stuff to get the files...

# Get hashes for all source paths
for srcFile in "${srcFileList[@]}"
do
  srcHashList+="$(md5sum "$srcFile")"
done

# Get hashes for all destination paths
for dstFile in "${dstFileList[@]}"
do
  dstHashList+="$(md5sum "$dstFile")"
done

# Compare hashes, exclude identical files, regardless of their path.
for srci in "${!srcHashList[@]}"
do
  for dsti in "${!dstHashList[@]}"
  do
    match=0
    if [ "${srcHashList[$srci]}" == "${dstHashList[$dsti]}" ]
    then
      match=1
    fi
    if [ $match != 1 ]
    then
      newSrcList+=${srcFileList[$srci]}
      newDstList+=${dstFileList[$dsti]}
    fi
  done
done
# ...move files after based on the new lists

특히 서로 동일한 경로를 가진 파일에만 관심이 있는 경우에는 확실히 더 깔끔하게 수행될 수 있습니다. 선형 시간으로 수행할 수도 있지만 일반적인 개념은 작동합니다.

관련 정보