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.
.out
and that the files within those directories have name ending with.gdx
? Are the.gdx
files located at the top level of those folders or may they exist anywhere beneath the.out
folders? – Kusalananda May 04 '18 at 15:36*.out
and*.gdx
exist in beneath top level directory. The reason first I searched by*.out
in firstfind
is, the*.gdx
is a binary file and cannot begrep
by the same text as of*.out
. The objective is, find directory which has .out file,cd
there, and thencp
files (.gdx, etc.) to somewhere. Thanks! – Akand May 04 '18 at 16:47grep
? Your question does not mentiongrep
. – Kusalananda May 04 '18 at 16:49grep
for .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 thatgrep
does not have anything to do with the actual issue of copying the files, … (Cont’d) – Scott - Слава Україні May 05 '18 at 18:00.out
file, (b) existence / location of.gdx
file, … (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