一次复制两个文件

Ava*_*eka 6 command-line files cp

如果我想使用命令一次复制两个文件怎么办?假设我有一个名为的ABC 文件夹,文件是

mno.txt
xyz.txt
abcd.txt
qwe.txt and so on (100 no. of files)
Run Code Online (Sandbox Code Playgroud)

现在我想cpmno.txt并且xyz.txt一次。我怎样才能做到这一点 ?

hee*_*ayl 17

假设您想将cp文件放入目录,您可以使用以下常用语法cp

cp mno.txt xyz.txt destination_directory
Run Code Online (Sandbox Code Playgroud)

或者为了简洁起见使用大括号扩展:

cp {mno,xyz}.txt destination_directory
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,最好使用 的-t( --target-directory) 选项cp,这是 GNU-ism:

cp -t destination_directory {mno,xyz}.txt
Run Code Online (Sandbox Code Playgroud)

请注意,如果您想cp一次性访问多个文件的内容cp,则不能。cp将一个文件的内容复制到另一个文件时,一次处理一个文件。


Ser*_*nyy 3

使用cp -t destination_dir/ file1 file2语法。

例子:

bash-4.3$ ls dir1
file1  file2  file3
bash-4.3$ ls dir2/
bash-4.3$ cp -t dir2/  dir1/file1 dir1/file2
bash-4.3$ ls dir2
file1  file2
Run Code Online (Sandbox Code Playgroud)

添加到原来的答案。

喜欢使用 python 的用户可能会对以下脚本感兴趣,该脚本允许复制命令行上指定的任意数量的文件,最后一个参数是目标。

演示:

bash-4.3$ ls dir1
file1  file2  file3
bash-4.3$ ls dir2
bash-4.3$ ./copyfiles.py dir1/file1 dir1/file2 dir2
bash-4.3$ ls dir2
file1  file2
Run Code Online (Sandbox Code Playgroud)

脚本本身:

bash-4.3$ ls dir1
file1  file2  file3
bash-4.3$ ls dir2/
bash-4.3$ cp -t dir2/  dir1/file1 dir1/file2
bash-4.3$ ls dir2
file1  file2
Run Code Online (Sandbox Code Playgroud)

  • 该脚本做了哪些 cp 不做的事情? (4认同)