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

為{幸葍}努か 提交于 2019-11-28 17:22:25

问题


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 dir2 there are n files (n>4)

By the way, I'm using bash.

Thanks.


回答1:


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

or (in 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} .



回答2:


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

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

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

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

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

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




回答3:


Try this one:

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


来源:https://stackoverflow.com/questions/9915822/how-to-copy-multiple-files-from-a-different-directory-using-cp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!