한 디렉터리에서 다른 디렉터리로 파일 복사

한 디렉터리에서 다른 디렉터리로 파일 복사

한 폴더에서 다른 폴더로 파일 세트를 복사해야 하며 다음 명령을 사용합니다.

cp -rf `head -n 100 /home/tmp/abc.txt` /home/tmp/test/files

파일의 내용은 abc.txt다음과 같습니다.

./folder1/example.tar.gz
./folder2/example.tar.gz
./folder3/example.tar.gz

그러나 위 cp명령을 실행하면 다음과 같은 결과가 나타납니다.

cp: will not overwrite just-created `/home/tmp/test/files/example.tar.gz' with `./folder3 /example.tar.gz'
cp: will not overwrite just-created `/home/tmp/test/files/example.tar.gz' with `./folder1/example.tar.gz'

.gz 파일 이름이 오류에 표시된 내용과 동일하므로 이해가 됩니다. 내가 원하는 것은 /home/tmp/test/files내부적으로 에 나열된 것과 동일한 폴더 구조를 갖는 것입니다 abc.txt. 예를 들면 다음과 같습니다.

/home/tmp/test/files/folder1/example.tar.gz
/home/tmp/test/files/folder2/example.tar.gz
/home/tmp/test/files/folder3/example.tar.gz

example.tar.gz그러나 위 명령을 실행한 후에는 내부 폴더만 얻습니다 cp.

그럼 위에서 언급한 것을 어떻게 얻을 수 있나요?

답변1

CP 아니요

복사는 디렉터리 구조를 복사하지 않고 파일만 복사하므로 오류 메시지가 표시되므로 예제가 작동하지 않습니다. 이러한 전체 복사를 수행하려면 tar명령과 구문을 사용 tar cvf - --files-from=... | (cd /home/tmp/test/files/; tar xvf -)하거나 그냥 rsync.

동기화

내가 당신이라면 이렇게 할 것입니다 rsync.

$ rsync -avz --files-from=abc.txt /src /home/tmp/test/files/.

파일의 처음 100줄만 필요한 경우 abc.txt다음을 수행할 수 있습니다.

$ rsync -avz --files-from=<(head -n 100 abc.txt) /src /home/tmp/test/files/.

샘플 폴더 데이터:

$ tree /home/saml/tmp/folder*
/home/saml/tmp/folder1
`-- example.tar.gz
/home/saml/tmp/folder2
`-- example.tar.gz
/home/saml/tmp/folder3
`-- example.tar.gz

이제 파일을 복사합니다.

$ rsync -avz --files-from=<(head -n 3 /home/saml/tmp/abc.txt) \
         /home/saml/tmp/. /home/saml/tmp/test/files/.
building file list ... done
./
folder1/
folder1/example.tar.gz
folder2/
folder2/example.tar.gz
folder3/
folder3/example.tar.gz

sent 3147093 bytes  received 81 bytes  6294348.00 bytes/sec
total size is 3145728  speedup is 1.00

복사되었는지 확인합니다.

$ tree /home/saml/tmp/test/files
/home/saml/tmp/test/files
|-- folder1
|   `-- example.tar.gz
|-- folder2
|   `-- example.tar.gz
`-- folder3
    `-- example.tar.gz

아스팔트

관심이 있으신 경우 사용 방법은 다음과 같습니다 tar.

$ cd /home/saml/tmp
$ tar cvf - --files-from=<(head -n 3 /home/saml/tmp/abc.txt) | (cd /home/saml/tmp/test/files/; tar xvf -)
./folder1/example.tar.gz
./folder1/example.tar.gz
./folder2/example.tar.gz
./folder2/example.tar.gz
./folder3/example.tar.gz
./folder3/example.tar.gz

복사되었는지 확인합니다.

$ tree /home/saml/tmp/test/files
/home/saml/tmp/test/files
|-- folder1
|   `-- example.tar.gz
|-- folder2
|   `-- example.tar.gz
`-- folder3
    `-- example.tar.gz

답변2

문제는 cp디렉토리 구조를 복사하지 않는 것입니다. 물론입니다. ( cp -r재귀적으로).

cp최소한 POSIX가 수행하지 않는 작업은 cp대상 디렉터리에서 소스 피연산자의 상대 경로를 다시 만드는 것입니다.

그렇다면 당신은 cp a/b/file1 a/b/file2 a/c/file3 dest그것을 얻었습니다 dest/file1 dest/file2 dest/file3.

GNU Coreutils를 사용하는 경우 cp이 옵션이 있을 수 있습니다 --parents. 언제 도입되었는지는 확실하지 않습니다. GNU가 이 경로 재생성 메커니즘을 생성하도록 cp --parents유도합니다 . cp정보 문서에서:

--parents
  Form the name of each destination file by appending to the target
  directory a slash and the specified name of the source file.  The
  last argument given to `cp' must be the name of an existing
  directory.  For example, the command:

      cp --parents a/b/c existing_dir

  copies the file `a/b/c' to `existing_dir/a/b/c', creating any
  missing intermediate directories.

따라서 GNU가 있는 경우 재귀적이지 않으므로 cp제거하고 다음을 사용하여 변경해야 합니다 .-r--parents

cp --parents -f `head -n 100 /home/tmp/abc.txt` /home/tmp/test/files

관련 정보