내부에 다른 디렉터리를 만드는 대신 'cp'가 디렉터리를 덮어 쓰도록 강제하는 방법은 무엇입니까?
기존 디렉터리를 덮어 쓰는 Bash 스크립트를 작성하려고합니다. 디렉토리가 foo/
있고 덮어 쓰려고 bar/
합니다. 하지만 이렇게하면 :
cp -Rf foo/ bar/
새 bar/foo/
디렉토리가 생성됩니다. 나는 그것을 원하지 않는다. 에 두 개의 파일이 있습니다 foo/
. a
및 b
. 같은 이름의 파일 bar/
도 있습니다. 내가 원하는 foo/a
및 foo/b
교체 bar/a
와 bar/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
-v
Verbose 를 사용하면 분명한 차이점을 알 수 있습니다 .
그냥 -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/
--delete
foo/
에서 없는 추가 파일도 제거 하여 동일한 결과를bar/
보장bar/
합니다.- 그것이 무엇을하는지보고 싶다면,
-vh
장황하고 사람이 읽을 수 있도록 추가하십시오. - Note: the slash after
foo
is required, otherwisersync
will copyfoo/
tobar/foo/
rather than overwritingbar/
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 ofbar/
, 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 offoo/
intobar/
, rather than the directoryfoo/
itself landing intobar/
asbar/foo
.)
- (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
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
'your programing' 카테고리의 다른 글
2D 배열을 1D 배열에 매핑 (0) | 2020.10.09 |
---|---|
파이썬에서 문자열의 처음 몇 글자를 표시하는 방법은 무엇입니까? (0) | 2020.10.09 |
중첩 된 'for'루프의 수에 제한이 있습니까? (0) | 2020.10.09 |
누군가 잘 구성된 crossdomain.xml 샘플을 게시 할 수 있습니까? (0) | 2020.10.09 |
Ruby Koans : 기호 목록을 문자열로 변환하는 이유 (0) | 2020.10.09 |