your programing

내부에 다른 디렉터리를 만드는 대신 'cp'가 디렉터리를 덮어 쓰도록 강제하는 방법은 무엇입니까?

lovepro 2020. 10. 9. 11:33
반응형

내부에 다른 디렉터리를 만드는 대신 'cp'가 디렉터리를 덮어 쓰도록 강제하는 방법은 무엇입니까?


기존 디렉터리를 덮어 쓰는 Bash 스크립트를 작성하려고합니다. 디렉토리가 foo/있고 덮어 쓰려고 bar/합니다. 하지만 이렇게하면 :

cp -Rf foo/ bar/

bar/foo/디렉토리가 생성됩니다. 나는 그것을 원하지 않는다. 에 두 개의 파일이 있습니다 foo/. ab. 같은 이름의 파일 bar/도 있습니다. 내가 원하는 foo/afoo/b교체 bar/abar/b.


-T에서 옵션을 사용하여이 작업을 수행 할 수 있습니다 cp.
에 대한 매뉴얼 페이지를 참조하십시오 cp.

-T, --no-target-directory
    treat DEST as a normal file

따라서 귀하의 예에 따라 다음은 파일 구조입니다.

$ tree test
test
|-- bar
|   |-- a
|   `-- b
`-- foo
    |-- a
    `-- b
2 directories, 4 files

-vVerbose 를 사용하면 분명한 차이점을 알 수 있습니다 .
그냥 -R옵션 을 사용할 때 .

$ cp -Rv foo/ bar/
`foo/' -> `bar/foo'
`foo/b' -> `bar/foo/b'
`foo/a' -> `bar/foo/a'
 $ tree
 |-- bar
 |   |-- a
 |   |-- b
 |   `-- foo
 |       |-- a
 |       `-- b
 `-- foo
     |-- a
     `-- b
3 directories, 6 files

이 옵션을 사용하면 -T내용을 덮어 쓰고 대상을 디렉토리가 아닌 일반 파일처럼 취급합니다 .

$ cp -TRv foo/ bar/
`foo/b' -> `bar/b'
`foo/a' -> `bar/a'

$ tree
|-- bar
|   |-- a
|   `-- b
`-- foo
    |-- a
    `-- b
2 directories, 4 files

이것은 당신의 문제를 해결할 것입니다.


두 단계로 수행하십시오.

rm -r bar/
cp -r foo/ bar/

당신을 보장하려면 bar/동일한까지 끝을 foo/사용 rsync하는 대신 :

rsync -a --delete foo/ bar/

몇 가지만 변경된 경우 전체 디렉토리를 제거하고 다시 복사하는 것보다 훨씬 빠르게 실행됩니다.

  • -a파일을 충실하게 복사하는 '아카이브 모드'입니다 foo/.bar/
  • --deletefoo/에서 없는 추가 파일도 제거 하여 동일한 결과를 bar/보장 bar/합니다.
  • 그것이 무엇을하는지보고 싶다면, -vh장황하고 사람이 읽을 수 있도록 추가하십시오.
  • Note: the slash after foo is required, otherwise rsync will copy foo/ to bar/foo/ rather than overwriting bar/ itself.
    • (Slashes after directories in rsync are confusing; if you're interested, here's the scoop. They tell rsync to refer to the contents of the directory, rather than the directory itself. So to overwrite from the contents of foo/ onto the contents of bar/, we use a slash on both. It's confusing because it won't work as expected with a slash on neither, though; rsync sneakily always interprets the destination path as though it has a slash, even though it honors an absence of a slash on the source path. So we need a slash on the source path to make it match the auto-added slash on the destination path, if we want to copy the contents of foo/ into bar/, rather than the directory foo/ itself landing into bar/ as bar/foo.)

rsync is very powerful and useful, if you're curious look around for what else it can do (such as copying over ssh).


Use this cp command:

cp -Rf foo/* bar/

The following command ensures dotfiles (hidden files) are included in the copy:

$ cp -Rf foo/. bar

Very similar to @Jonathan Wheeler:

If you do not want to remember, but not rewrite bar:

rm -r bar/
cp -r foo/ !$

!$ displays the last argument of your previous command.


The operation you defined is a "merge" and you cannot do that with cp. However, if you are not looking for merging and ok to lose the folder bar then you can simply rm -rf bar to delete the folder and then mv foo bar to rename it. This will not take any time as both operations are done by file pointers, not file contents.


Try to use this composed of two steps command:

rm -rf bar && cp -r foo bar

참고URL : https://stackoverflow.com/questions/23698183/how-to-force-cp-to-overwrite-directory-instead-of-creating-another-one-inside

반응형