Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Grep command to find files containing text string and move them

Status
Not open for further replies.

jikwle

Newbie
Newbie level 1
Joined
Jan 25, 2022
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
8
I am able to locate files in a folder containing a specific text string using this command:
grep -lir 'string' ~/directory/*
How do I move the files that appear in the above result to another location?
 

KlausST

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,269
Helped
4,741
Reputation
9,503
Reaction score
5,127
Trophy points
1,393
Activity points
154,156
Hi,

I´m really not experienced with Linux ... thus I just have a quick and dirty solution:

* first do the GREP and write it´s results in a file
* then do the MOVE according the file contents

But I guess there is a more elegant solution....

Klaus
 

dick_freebird

Advanced Member level 7
Advanced Member level 7
Joined
Mar 4, 2008
Messages
8,244
Helped
2,293
Reputation
4,596
Reaction score
2,334
Trophy points
1,393
Location
USA
Activity points
65,957
Hi,

I´m really not experienced with Linux ... thus I just have a quick and dirty solution:

* first do the GREP and write it´s results in a file
* then do the MOVE according the file contents

But I guess there is a more elegant solution....

Klaus
That's what I do, grep into a file of targets and then vi the
file to make clean 'mv' commands. Though if you understood
the output of the grep you might pipe the stream to 'sed -e'
with similar sytnax, I prefer having the chance to correct any
fat-fingering along the way.
 

c_mitra

Advanced Member level 6
Advanced Member level 6
Joined
Nov 13, 2012
Messages
3,815
Helped
929
Reputation
1,860
Reaction score
921
Trophy points
1,393
Activity points
30,128
* first do the GREP and write it´s results in a file
* then do the MOVE according the file contents
I guess this is the only solution but you can use pipes and /or sed to give a professional look.

But if the numbers are small and you need to do it only once, it is better to do as KlausST says.

Do check visually after every step just to make sure you know what you are doing and what you want.
 

Aussie Susan

Advanced Member level 4
Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,489
Helped
406
Reputation
812
Reaction score
445
Trophy points
1,363
Activity points
17,516
If you use a file as the intermediate medium and visually check before doing the 'move' then that will not encounter this problem.
However if you use as pipe (which means that the processes might run in parallel), make sure that the move destination is either not scanned by the 'grep' or scanned before the 'move' into that destination from somewhere else.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top