从Linux shell将多个文件从一个目录复制到另一个目录

时间:2020-12-14 16:02:56

I want to copy several files from one folder to another. How do I do it from the shell command prompt?

我想将几个文件从一个文件夹复制到另一个文件夹。我如何从shell命令提示符执行此操作?

Consider that folder1 contains ten files (e.g. file1, file2, abc, xyz, etc.). I am currently doing the following in order to copy two files from one folder to another:

考虑folder1包含十个文件(例如file1,file2,abc,xyz等)。我目前正在执行以下操作,以便将两个文件从一个文件夹复制到另一个文件夹:

cp /home/ankur/folder/file1 /home/ankur/folder/file2 /home/ankur/dest

Typing the full path into the command line for both the files is annoying.
What comes to my mind is regex, but I don't quite know how to do it.

在两个文件的命令行中键入完整路径很烦人。我想到的是正则表达式,但我不知道该怎么做。

Any help will reduce my RSI ;-)

任何帮助都会降低我的RSI ;-)

4 个解决方案

#1


19  

I guess you are looking for brace expansion:

我想你正在寻找大括号扩展:

cp /home/ankur/folder/{file1,file2} /home/ankur/dest

take a look here, it would be helpful for you if you want to handle multiple files once :

看看这里,如果你想要处理多个文件,它会对你有所帮助:

http://www.tldp.org/LDP/abs/html/globbingref.html

http://www.tldp.org/LDP/abs/html/globbingref.html

tab completion with zsh...

从Linux shell将多个文件从一个目录复制到另一个目录

#2


10  

Use wildcards:

使用通配符:

cp /home/ankur/folder/* /home/ankur/dest

If you don't want to copy all the files, you can use braces to select files:

如果您不想复制所有文件,可以使用大括号选择文件:

cp /home/ankur/folder/{file{1,2},xyz,abc} /home/ankur/dest

This will copy file1, file2, xyz, and abc.

这将复制file1,file2,xyz和abc。

You should read the sections of the bash man page on Brace Expansion and Pathname Expansion for all the ways you can simplify this.

您应该阅读有关Brace Expansion和Pathname Expansion的bash手册页的各个部分,以了解所有可以简化此操作的方法。

Another thing you can do is cd /home/ankur/folder. Then you can type just the filenames rather than the full pathnames, and you can use filename completion by typing Tab.

你可以做的另一件事是cd / home / ankur /文件夹。然后,您只需键入文件名而不是完整路径名,您可以通过键入Tab来使用文件名完成。

#3


3  

You can use brace expansion in bash:

你可以在bash中使用大括号扩展:

cp /home/ankur/folder/{file1,abc,xyz} /path/to/target

#4


2  

Try this simpler one,

试试这个更简单的,

cp /home/ankur/folder/file{1,2} /home/ankur/dest

If you want to copy all the 10 files then run this command,

如果要复制所有10个文件,请运行此命令,

 cp ~/Desktop/{xyz,file{1,2},next,files,which,are,not,similer} foo-bar

#1


19  

I guess you are looking for brace expansion:

我想你正在寻找大括号扩展:

cp /home/ankur/folder/{file1,file2} /home/ankur/dest

take a look here, it would be helpful for you if you want to handle multiple files once :

看看这里,如果你想要处理多个文件,它会对你有所帮助:

http://www.tldp.org/LDP/abs/html/globbingref.html

http://www.tldp.org/LDP/abs/html/globbingref.html

tab completion with zsh...

从Linux shell将多个文件从一个目录复制到另一个目录

#2


10  

Use wildcards:

使用通配符:

cp /home/ankur/folder/* /home/ankur/dest

If you don't want to copy all the files, you can use braces to select files:

如果您不想复制所有文件,可以使用大括号选择文件:

cp /home/ankur/folder/{file{1,2},xyz,abc} /home/ankur/dest

This will copy file1, file2, xyz, and abc.

这将复制file1,file2,xyz和abc。

You should read the sections of the bash man page on Brace Expansion and Pathname Expansion for all the ways you can simplify this.

您应该阅读有关Brace Expansion和Pathname Expansion的bash手册页的各个部分,以了解所有可以简化此操作的方法。

Another thing you can do is cd /home/ankur/folder. Then you can type just the filenames rather than the full pathnames, and you can use filename completion by typing Tab.

你可以做的另一件事是cd / home / ankur /文件夹。然后,您只需键入文件名而不是完整路径名,您可以通过键入Tab来使用文件名完成。

#3


3  

You can use brace expansion in bash:

你可以在bash中使用大括号扩展:

cp /home/ankur/folder/{file1,abc,xyz} /path/to/target

#4


2  

Try this simpler one,

试试这个更简单的,

cp /home/ankur/folder/file{1,2} /home/ankur/dest

If you want to copy all the 10 files then run this command,

如果要复制所有10个文件,请运行此命令,

 cp ~/Desktop/{xyz,file{1,2},next,files,which,are,not,similer} foo-bar