gnu cp 명령을 사용하여 파일을 여러 디렉토리에 복사하는 방법
cp 명령을 사용하여 단일 파일을 여러 디렉토리에 복사 할 수 있습니까?
작동하지 않는 다음을 시도했습니다.
cp file1 /foo/ /bar/
cp file1 {/foo/,/bar}
for 루프 또는 찾기를 사용하는 것이 가능하다는 것을 알고 있습니다. 그러나 gnu cp 명령을 사용하는 것이 가능합니까?
아니요, cp
여러 소스를 복사 할 수 있지만 단일 대상으로 만 복사됩니다. cp
원하는 작업에 대해 대상마다 한 번씩 여러 번 호출하도록 준비해야합니다 . 당신이 말하는 것처럼, 루프 또는 다른 도구를 사용합니다.
당신이 할 수 없습니다 cp
만 그러나 당신은 결합 할 수 있습니다 cp
으로 xargs
:
echo dir1 dir2 dir3 | xargs -n 1 cp file1
복사 것인가 file1
에 dir1
, dir2
그리고 dir3
. xargs
이 작업을 cp
3 번 호출 xargs
합니다. 자세한 내용은 맨 페이지 를 참조하십시오.
와일드 카드는 Roberts 코드와도 작동
echo ./fs*/* | xargs -n 1 cp test
내가 볼 수있는 한 다음을 사용할 수 있습니다.
ls | xargs -n 1 cp -i file.dat
-i
의 옵션 cp
명령 수단은 당신이 가진 현재 디렉토리에있는 파일을 덮어 쓸 것인지 묻는 메시지가됩니다 file.dat
. 완전 자동 솔루션은 아니지만 나에게 도움이되었습니다.
내가 사용하는 것이 cat
그리고 tee
내가에서 본 응답에 따라 https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets 대신 cp
.
예를 들면 다음과 같습니다.
cat <inputfile> | tee <outfile1> <outfile2> > /dev/null
ls -db di*/subdir | xargs -n 1 cp File
-b
디렉토리 이름에 공백 이 있는 경우 xargs에 의해 다른 항목으로 분리됩니다. 에코 버전과 관련 하여이 문제가 발생했습니다.
이 답변은 모두 명백한 것보다 더 복잡해 보입니다.
for i in /foo /bar; do cp "$file1" "$i"; done
cp 자체를 사용하지는 않지만 ...
이것은 (느린) SD 카드에서 많은 Gopro 장면을 3 개의 (느린) USB 드라이브로 복사하는 맥락에서 나에게 나타났습니다. 데이터가 영원히 걸리기 때문에 한 번만 데이터를 읽고 싶었습니다. 그리고 나는 재귀를 원했습니다.
$ tar cf - src | tee >( cd dest1 ; tar xf - ) >( cd dest2 ; tar xf - ) | ( cd dest3 ; tar xf - )
(더 많은 출력을 원하면> () 섹션을 더 추가 할 수 있습니다.)
나는 그것을 벤치마킹하지는 않았지만 cp-in-a-loop (또는 많은 병렬 cp 호출)보다 확실히 빠릅니다.
복사를 사용하여 xargs
맥 OS, 나를 위해 일한 유일한 해결책에 와일드 카드를 사용하여 디렉토리 와 디렉토리 이름은 공백 :
find ./fs*/* -type d -print0 | xargs -0 -n 1 cp test
어디에서 test
복사 할 파일
과 ./fs*/*
복사 할 디렉토리는
문제는 xargs가 공백을 새로운 인수로 간주한다는 것 입니다. Mac OS 에서 구분 기호 문자를 변경 -d
하거나 -E
안타깝게도 Mac OS에서 제대로 작동하지 않습니다.
분기 명령없이 수행하려면 다음을 수행하십시오.
tee <inputfile file2 file3 file4 ... >/dev/null
본질적으로 xargs 답변과 동일하지만 병렬 실행을 원할 경우 :
parallel -q cp file1 ::: /foo/ /bar/
예를 들어, file1을 현재 폴더의 모든 하위 디렉토리 (재귀 포함)에 복사하려면 다음을 수행하십시오.
parallel -q cp file1 ::: `find -mindepth 1 -type d`
주의 : 이것은 각각의 대상 디렉토리가 별개의 디스크 인 경우와 같이 매우 특정한 사용 사례에 대해 눈에 띄는 속도 향상만을 전달할 것입니다.
또한 xargs의 '-P'인수와 기능적으로 유사합니다.
bash 스크립트 사용
DESTINATIONPATH[0]="xxx/yyy"
DESTINATIONPATH[1]="aaa/bbb"
..
DESTINATIONPATH[5]="MainLine/USER"
NumberOfDestinations=6
for (( i=0; i<NumberOfDestinations; i++))
do
cp SourcePath/fileName.ext ${DESTINATIONPATH[$i]}
done
exit
아니 당신은 할 수 없습니다.
여러 번이 기능을 사용할 수 있다는 것을 알게 되었기 때문에이를위한 도구를 직접 만들었습니다.
http://github.com/ddavison/branch
꽤 간단합니다-
branch myfile dir1 dir2 dir3
ls -d */ | xargs -iA cp file.txt A
Suppose you want to copy fileName.txt
to all sub-directories within present working directory.
Get all sub-directories names through
ls
and save them to some temporary file say,allFolders.txt
ls > allFolders.txt
Print the list and pass it to command
xargs
.cat allFolders.txt | xargs -n 1 cp fileName.txt
Another way is to use cat and tee as follows:
cat <source file> | tee <destination file 1> | tee <destination file 2> [...] > <last destination file>
I think this would be pretty inefficient though, since the job would be split among several processes (one per destination) and the hard drive would be writing several files at once over different parts of the platter. However if you wanted to write a file out to several different drives, this method would probably be pretty efficient (as all copies could happen concurrently).
If all your target directories match a path expression — like they're all subdirectories of path/to
— then just use find
in combination with cp
like this:
find ./path/to/* -type d -exec cp [file name] {} \;
That's it.
if you want to copy multiple folders to multiple folders one can do something like this:
echo dir1 dir2 dir3 | xargs -n 1 cp -r /path/toyourdir/{subdir1,subdir2,subdir3}
If you need to be specific on into which folders to copy the file you can combine find with one or more greps. For example to replace any occurences of favicon.ico in any subfolder you can use:
find . | grep favicon\.ico | xargs -n 1 cp -f /root/favicon.ico
This will copy to the immediate sub-directories, if you want to go deeper, adjust the -maxdepth
parameter.
find . -mindepth 1 -maxdepth 1 -type d| xargs -n 1 cp -i index.html
If you don't want to copy to all directories, hopefully you can filter the directories you are not interested in. Example copying to all folders starting with a
find . -mindepth 1 -maxdepth 1 -type d| grep \/a |xargs -n 1 cp -i index.html
If copying to a arbitrary/disjoint set of directories you'll need Robert Gamble's suggestion.
I like to copy a file into multiple directories as such: cp file1 /foo/; cp file1 /bar/; cp file1 /foo2/; cp file1 /bar2/
And copying a directory into other directories: cp -r dir1/ /foo/; cp -r dir1/ /bar/; cp -r dir1/ /foo2/; cp -r dir1/ /bar2/
I know it's like issuing several commands, but it works well for me when I want to type 1 line and walk away for a while.
For example if you are in the parent directory of you destination folders you can do:
for i in $(ls); do cp sourcefile $i; done
'Programming' 카테고리의 다른 글
Ajax 쿼리 게시 오류를 어떻게 포착합니까? (0) | 2020.05.08 |
---|---|
비 www에서 www로 아파치 리디렉션 (0) | 2020.05.08 |
작업 표시 줄 아래의 그림자 제거 (0) | 2020.05.08 |
PHP에서 비동기 HTTP 요청을 만드는 방법 (0) | 2020.05.08 |
Java 8 스트림 : 다중 필터 및 복잡한 조건 (0) | 2020.05.08 |