I want to search by file extension and by texts and then copy a binary file inside the same folder. For instance, I am in directory A and finally like to copy all *.gdx files (in B,C,D) to somewhere.
A
|-- B
| |-- file1.out (a text file)
| |-- file1.gdx (a binary file)
|
|-- C
| |-- file2.out (a text file)
| |-- file2.gdx (a binary file)
|
|-- D
| |-- file3.out (a text file)
| |-- file3.gdx (a binary file)
Here is my code:
cd 'find . -maxdepth 2 -name "*.out"|xargs grep "sometext"| awk -F'/' '{print $2}'|sort -u ' && ' find . -maxdepth 2 -name "*.gdx" -print0|xargs -0 cp -t /somewhere'
The problem here, if first find captures multiple folders then copy only one *.gdx file from the first folder, not all *.gdx files from all folders. I believe it has to be done by loop, but don't know how to script.
.outand that the files within those directories have name ending with.gdx? Are the.gdxfiles located at the top level of those folders or may they exist anywhere beneath the.outfolders? – Kusalananda May 04 '18 at 15:36*.outand*.gdxexist in beneath top level directory. The reason first I searched by*.outin firstfindis, the*.gdxis a binary file and cannot begrepby the same text as of*.out. The objective is, find directory which has .out file,cdthere, and thencpfiles (.gdx, etc.) to somewhere. Thanks! – Akand May 04 '18 at 16:47grep? Your question does not mentiongrep. – Kusalananda May 04 '18 at 16:49grepfor .out file like `find . -maxdepth 2 -name ".out" | xargs grep "sometext" `, but not for *.gdx file which is a binary. – Akand May 04 '18 at 18:54grep. One time you said thatgrepdoes not have anything to do with the actual issue of copying the files, … (Cont’d) – Scott - Слава Україні May 05 '18 at 18:00.outfile, (b) existence / location of.gdxfile, … (Cont’d) – Scott - Слава Україні May 05 '18 at 18:00grep— so your example should show a minimum of six cases to cover all the conditions. … … … … … … … … … … … … … … … … … … … … … … … … Please do not respond in comments; [edit] your question to make it clearer and more complete. – Scott - Слава Україні May 05 '18 at 18:00