LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-10-2018, 12:19 AM   #1
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 83

Rep: Reputation: Disabled
Cp all files whose contents include the word "XSym" to a directory


I know how to locate files that contain XSym ..

Code:
grep -lrI XSym .
Then I came up with this brilliant snippet to copy it to subdirectory x2, but unfortunately, it hangs, without returning to the command-prompt & without copying the file.

Code:
for i in $(grep -lrI XSym) do cp -i ./x2/ done
Also, I'd like to do this recursively, so maybe I need to use FIND instead of FOR?

Any ideas much appreciated.
 
Old 01-10-2018, 03:55 AM   #2
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
In this special case you absolutely need Grep to end before beginning any copy, otherwise you'll be in an infinite loop. So send the results of the Grep in a file. For this you should have complete path of the files and no spaces. It should be something like:
cd /
p=/pathfromwherewearesearching
[ ! -d $p/x2 ] && mkdir $p/x2
grep -lrI XSym $p > /tmp/f

for i in `cat $f` do cp -i $i $p/x2 done
 
Old 01-10-2018, 05:43 AM   #3
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware, OpenBSD
Posts: 3,924
Blog Entries: 11

Rep: Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173Reputation: 2173
Why not use find with xargs grep?
 
Old 01-10-2018, 05:58 AM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,511

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
You could also try
Code:
$ grep -r string directory/* | awk '{print $1}' | xargs cp $1 directory
 
Old 01-10-2018, 05:59 AM   #5
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post Why not use find with xargs grep?

Quote:
Originally Posted by hazel View Post
Why not use find with xargs grep?
Because this would be taking the process in the wrong order.
 
Old 01-11-2018, 11:55 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,697

Rep: Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494
Code:
$(grep -lrI XSym)
Where's the dir to search ie "." ?
 
Old 01-12-2018, 01:45 AM   #7
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,643

Rep: Reputation: 278Reputation: 278Reputation: 278
Will pipe serves the purpose?

grep -lrI XSym | cp -i ./x2/
 
Old 01-12-2018, 04:48 AM   #8
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post Loop

Quote:
Originally Posted by AwesomeMachine View Post
You could also try
Code:
$ grep -r string directory/* | awk '{print $1}' | xargs cp $1 directory
This could lead to an infinite loop too
 
  


Reply

Tags
grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FireFox SlackBuild removes the "/usr/include" directory khronosschoty Slackware 4 12-13-2016 07:51 AM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM
Where "standard system include directory" is defined? (gcc) kaz2100 Programming 8 07-23-2006 09:22 AM
Nautilus - "automatic updating" of directory contents Locura Linux - Software 0 12-03-2003 05:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration