简体   繁体   English

如何在linux中复制特定文件?

[英]how to copy specific files in linux?

I am trying to copy some of the files in a source directory to destination directory. 我试图将源目录中的一些文件复制到目标目录。 The source directory contains following files. 源目录包含以下文件。

source directory path is ~/certificate/ 源目录路径是〜/ certificate /

drwxrwxr-x 2 ubuntu ubuntu     4096 Oct 16 11:58 apache
-rw-rw-r-- 1 ubuntu ubuntu     5812 Oct 16 11:20 apache.keystore
-rw-rw-r-- 1 ubuntu ubuntu     1079 Oct 16 08:31 csr.txt
-rwxr-xr-x 1 ubuntu ubuntu 36626564 Oct 16 10:08 my.war
drwxrwxr-x 2 ubuntu ubuntu     4096 Oct 16 09:39 tomcat
-rw-rw-r-- 1 ubuntu ubuntu     6164 Oct 16 09:31 tomcat.keystore

I want to copy all files to ~/certs/ except my.war (certs is the destination directory). 我想将所有文件复制到〜/ certs /,除了my.war(certs是目标目录)。 I have tried the following command without success. 我没有成功尝试以下命令。 I do not want to move my.war out of the folder even temporarily. 我甚至不想暂时将my.war移出文件夹。

cp -r ~/certificate/(?!m)* ~/cert/. 

Please help me with suitable regular expression or any other tool. 请帮助我使用合适的正则表达式或任何其他工具。

Assuming you're using bash , you can enable the extglob option and use: 假设您正在使用bash ,您可以启用extglob选项并使用:

cp -r ~/certificate/!(my.war) ~/certs/

Use 采用

shopt -s extglob

to enable that option. 启用该选项。

Here's a link to the relevant documentation. 这是相关文档的链接

好吧,因为它是唯一一个以'm'开头的文件,你可以做cp -r ~/certificate/[a-ln-z]* ~/cert

You can try to use "find" command. 您可以尝试使用“查找”命令。 It find all files in directory, except "my.war" files and execute "cp" command for all found files. 它查找目录中的所有文件,“my.war”文件除外,并对所有找到的文件执行“cp”命令。

find . -type f \( -iname "*" ! -iname "my.war" \) -exec cp {} ./cert/ \;

For completeness: a zsh solution. 为了完整性: zsh解决方案。 In zsh with the EXTENDED_GLOB option set you can do (see documentation ): 在zsh中设置了EXTENDED_GLOB选项,你可以这样做(参见文档 ):

cp -r ~/certificate/^my.war ~/certs

or with KSH_GLOB (and NO_BARE_GLOB_QUAL ) options set you can use the same ksh syntax as in bash: 或者使用KSH_GLOB (和NO_BARE_GLOB_QUAL )选项设置,您可以使用与bash中相同的ksh语法:

cp -r ~/certificate/!(my.war) ~/certs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM