Linux 如何使用cp从不同目录复制多个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9915822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:27:59  来源:igfitidea点击:

How to copy multiple files from a different directory using cp?

linuxbashcp

提问by ziulfer

I want to copy multiple files from a specific directory once I am in another directory. To clarify I want to do the following, at once (one command):

一旦我在另一个目录中,我想从特定目录复制多个文件。为了澄清我想立即执行以下操作(一个命令):

cp ../dir5/dir4/dir3/dir2/file1 .
cp ../dir5/dir4/dir3/dir2/file2 .
cp ../dir5/dir4/dir3/dir2/file3 .
cp ../dir5/dir4/dir3/dir2/file4 .

I can't use cp ../dir5/dir4/dir3/dir2/* .because in dir2there are nfiles (n>4)

我不能使用, cp ../dir5/dir4/dir3/dir2/* .因为dir2里面有n文件(n>4)

By the way, I'm using bash.

顺便说一句,我正在使用bash.

Thanks.

谢谢。

采纳答案by Philipp

cp ../dir5/dir4/dir3/dir2/file[1234] .

or (in Bash)

或(在 Bash 中)

cp ../dir5/dir4/dir3/dir2/file{1..4} .

If the file names are non-contiguous, you can use

如果文件名不连续,您可以使用

cp ../dir5/dir4/dir3/dir2/{march,april,may} .

回答by zbyszek26104

Try this one:

试试这个:

 cp ../dir5/dir4/dir3/dir2/file{1..4}

回答by ghoti

If all the files you want to copy are in the pattern of file{number}{othertext}, you could use something like:

如果您要复制的所有文件都在 的模式中file{number}{othertext},您可以使用以下内容:

cp ../dir5/dir4/dir3/dir2/file[0-9]* .

Note that this will copy file5, but it will also copy file0abc.

请注意,这将复制file5,但它也会复制file0abc

If you would like to copy ONLY those four files (and not the {othertext} ones), you can use:

如果您只想复制这四个文件(而不是 {othertext} 文件),您可以使用:

cp ../dir5/dir4/dir3/dir2/file[1-4] .

Note that while this lookslike part of a regular expression, it is not.

请注意,虽然这看起来像是正则表达式的一部分,但实际上并非如此。