geekgugi
geekgugi

Reputation: 389

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/

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). I have tried the following command without success. I do not want to move my.war out of the folder even temporarily.

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

Please help me with suitable regular expression or any other tool.

Upvotes: 1

Views: 1134

Answers (4)

Michał Politowski
Michał Politowski

Reputation: 4385

For completeness: a zsh solution. In zsh with the EXTENDED_GLOB option set you can do (see documentation):

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:

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

Upvotes: 0

BN0LD
BN0LD

Reputation: 52

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.

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

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225052

Assuming you're using bash, you can enable the extglob option and use:

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

Use

shopt -s extglob

to enable that option.

Here's a link to the relevant documentation.

Upvotes: 2

Shobit
Shobit

Reputation: 794

Well, since it's the only file that starts with an 'm', you could do cp -r ~/certificate/[a-ln-z]* ~/cert

Upvotes: 1

Related Questions