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

jikwle

Newbie
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
Joined
Apr 17, 2014
Messages
21,809
Helped
4,580
Reputation
9,174
Reaction score
4,765
Trophy points
1,393
Activity points
143,900
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 5
Joined
Mar 4, 2008
Messages
7,740
Helped
2,215
Reputation
4,436
Reaction score
2,137
Trophy points
1,393
Location
USA
Activity points
61,845
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 5
Joined
Nov 13, 2012
Messages
3,800
Helped
929
Reputation
1,860
Reaction score
919
Trophy points
1,393
Activity points
29,962
* 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
Joined
Jan 5, 2015
Messages
1,396
Helped
392
Reputation
784
Reaction score
410
Trophy points
1,363
Activity points
16,348
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
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top