How to find files dynamically and copy them to a directory in linux

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Trying to find these files and copy them to a directory (test_dir);



abc-1.0.zip
kncb-1.0.zip
jenk-1.1.zip


I would like to find all these files dynamically and copy them to a directory. I have tried the below command but it copies only last file(jenk*.zip) only, need a command which copies all the above files to a directory mentioned (test_dir);



find / -type f -name "*abc*.zip" -o -name "*kncb*.zip" -o -name "*jenk*.zip" ! -path '*/test_dir/*.jar' -exec cp -ar ./test_dir ;









share|improve this question























  • are you guaranteed that there'll only be one of each of those?
    – Jeff Schaller
    Sep 25 at 1:54










  • why are you recursively copying (-r) when you're looking (presumably) for single files?
    – Jeff Schaller
    Sep 25 at 1:55














up vote
0
down vote

favorite












Trying to find these files and copy them to a directory (test_dir);



abc-1.0.zip
kncb-1.0.zip
jenk-1.1.zip


I would like to find all these files dynamically and copy them to a directory. I have tried the below command but it copies only last file(jenk*.zip) only, need a command which copies all the above files to a directory mentioned (test_dir);



find / -type f -name "*abc*.zip" -o -name "*kncb*.zip" -o -name "*jenk*.zip" ! -path '*/test_dir/*.jar' -exec cp -ar ./test_dir ;









share|improve this question























  • are you guaranteed that there'll only be one of each of those?
    – Jeff Schaller
    Sep 25 at 1:54










  • why are you recursively copying (-r) when you're looking (presumably) for single files?
    – Jeff Schaller
    Sep 25 at 1:55












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Trying to find these files and copy them to a directory (test_dir);



abc-1.0.zip
kncb-1.0.zip
jenk-1.1.zip


I would like to find all these files dynamically and copy them to a directory. I have tried the below command but it copies only last file(jenk*.zip) only, need a command which copies all the above files to a directory mentioned (test_dir);



find / -type f -name "*abc*.zip" -o -name "*kncb*.zip" -o -name "*jenk*.zip" ! -path '*/test_dir/*.jar' -exec cp -ar ./test_dir ;









share|improve this question















Trying to find these files and copy them to a directory (test_dir);



abc-1.0.zip
kncb-1.0.zip
jenk-1.1.zip


I would like to find all these files dynamically and copy them to a directory. I have tried the below command but it copies only last file(jenk*.zip) only, need a command which copies all the above files to a directory mentioned (test_dir);



find / -type f -name "*abc*.zip" -o -name "*kncb*.zip" -o -name "*jenk*.zip" ! -path '*/test_dir/*.jar' -exec cp -ar ./test_dir ;






linux find cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 at 1:55









Jeff Schaller

33.3k850112




33.3k850112










asked Sep 25 at 1:40









itgeek

151




151











  • are you guaranteed that there'll only be one of each of those?
    – Jeff Schaller
    Sep 25 at 1:54










  • why are you recursively copying (-r) when you're looking (presumably) for single files?
    – Jeff Schaller
    Sep 25 at 1:55
















  • are you guaranteed that there'll only be one of each of those?
    – Jeff Schaller
    Sep 25 at 1:54










  • why are you recursively copying (-r) when you're looking (presumably) for single files?
    – Jeff Schaller
    Sep 25 at 1:55















are you guaranteed that there'll only be one of each of those?
– Jeff Schaller
Sep 25 at 1:54




are you guaranteed that there'll only be one of each of those?
– Jeff Schaller
Sep 25 at 1:54












why are you recursively copying (-r) when you're looking (presumably) for single files?
– Jeff Schaller
Sep 25 at 1:55




why are you recursively copying (-r) when you're looking (presumably) for single files?
– Jeff Schaller
Sep 25 at 1:55










1 Answer
1






active

oldest

votes

















up vote
2
down vote













You need to apply ( ) around your OR construct.



An explanation can be found in the man page:




Please note that -a, when specified implicitly (for example by two
tests appearing without an explicit operator between them) or
explicitly, has higher precedence than -o. This means that find . -name
afile -o -name bfile -print will never print afile.




Example:



touch afile bfile


Without ( ):



find . -name afile -o -name bfile -print
./bfile


which has an implicit -a between -name bfile and -print:



find . -name afile -o -name bfile -a -print
./bfile


With ( ) the result is as expected:



find . ( -name afile -o -name bfile ) -print
./afile
./bfile


Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.






share|improve this answer




















  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
    – itgeek
    Sep 25 at 13:57











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471215%2fhow-to-find-files-dynamically-and-copy-them-to-a-directory-in-linux%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













You need to apply ( ) around your OR construct.



An explanation can be found in the man page:




Please note that -a, when specified implicitly (for example by two
tests appearing without an explicit operator between them) or
explicitly, has higher precedence than -o. This means that find . -name
afile -o -name bfile -print will never print afile.




Example:



touch afile bfile


Without ( ):



find . -name afile -o -name bfile -print
./bfile


which has an implicit -a between -name bfile and -print:



find . -name afile -o -name bfile -a -print
./bfile


With ( ) the result is as expected:



find . ( -name afile -o -name bfile ) -print
./afile
./bfile


Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.






share|improve this answer




















  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
    – itgeek
    Sep 25 at 13:57















up vote
2
down vote













You need to apply ( ) around your OR construct.



An explanation can be found in the man page:




Please note that -a, when specified implicitly (for example by two
tests appearing without an explicit operator between them) or
explicitly, has higher precedence than -o. This means that find . -name
afile -o -name bfile -print will never print afile.




Example:



touch afile bfile


Without ( ):



find . -name afile -o -name bfile -print
./bfile


which has an implicit -a between -name bfile and -print:



find . -name afile -o -name bfile -a -print
./bfile


With ( ) the result is as expected:



find . ( -name afile -o -name bfile ) -print
./afile
./bfile


Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.






share|improve this answer




















  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
    – itgeek
    Sep 25 at 13:57













up vote
2
down vote










up vote
2
down vote









You need to apply ( ) around your OR construct.



An explanation can be found in the man page:




Please note that -a, when specified implicitly (for example by two
tests appearing without an explicit operator between them) or
explicitly, has higher precedence than -o. This means that find . -name
afile -o -name bfile -print will never print afile.




Example:



touch afile bfile


Without ( ):



find . -name afile -o -name bfile -print
./bfile


which has an implicit -a between -name bfile and -print:



find . -name afile -o -name bfile -a -print
./bfile


With ( ) the result is as expected:



find . ( -name afile -o -name bfile ) -print
./afile
./bfile


Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.






share|improve this answer












You need to apply ( ) around your OR construct.



An explanation can be found in the man page:




Please note that -a, when specified implicitly (for example by two
tests appearing without an explicit operator between them) or
explicitly, has higher precedence than -o. This means that find . -name
afile -o -name bfile -print will never print afile.




Example:



touch afile bfile


Without ( ):



find . -name afile -o -name bfile -print
./bfile


which has an implicit -a between -name bfile and -print:



find . -name afile -o -name bfile -a -print
./bfile


With ( ) the result is as expected:



find . ( -name afile -o -name bfile ) -print
./afile
./bfile


Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 25 at 4:59









nst0022

1062




1062











  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
    – itgeek
    Sep 25 at 13:57

















  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
    – itgeek
    Sep 25 at 13:57
















Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
– itgeek
Sep 25 at 13:57





Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar ./test_dir ; find: invalid expression; you have used a binary operator '-o' with nothing before it.
– itgeek
Sep 25 at 13:57


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471215%2fhow-to-find-files-dynamically-and-copy-them-to-a-directory-in-linux%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos