LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 12-10-2012, 04:47 AM   #1
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian
Posts: 154
Blog Entries: 1

Rep: Reputation: 12
Search a list of files for a pattern


I've tried this:
find / -type f -iname "*.txt" 2> /dev/null | xargs grep --ignore-case mypattern
Then I tried first redirecting the find results into a file and running:
xargs -a /tmp/findtext.txt grep --ignore-case mypattern

It constantly gives me: (excuse the Dutch)
Code:
xargs: Ongepaard enkel aanhalingsteken; aanhalingstekens worden door 'xargs'
speciaal behandeld tenzij u optie '-0' gebruikt.
grep: /home/user/[: Bestand of map bestaat niet
grep: www.Speed.Cd: Bestand of map bestaat niet
grep: ]: Bestand of map bestaat niet
grep: 'lseek' is mislukt: Ongeldige 'seek'-opdracht
grep: Mr.Freedom.1969.ECLiPSE.DVDRiP.XViD.iNT-DeBTXViD/www.Speed.Cd.txt: Bestand of map bestaat niet
grep: /home/user/Bureaublad/JonasMap/verslag/Castles: Bestand of map bestaat niet
[etc. etc.]
("Bestand bestaat niet" = "File doesn't exist"
Start says: "unpaired single quote; quotes are treated special, unless you pass the '-0' option"
)
The start of the search results look like this:
Code:
/tmp/findtext.txt
/etc/X11/rgb.txt
/home/user/[ www.Speed.Cd ] - Mr.Freedom.1969.ECLiPSE.DVDRiP.XViD.iNT-DeBTXViD/www.Speed.Cd.txt
/home/user/.TeXworks/configuration/auto-indent-patterns.txt
/home/user/.TeXworks/configuration/smart-quotes-modes.txt
/home/user/.TeXworks/configuration/syntax-patterns.txt
/home/user/.TeXworks/configuration/tag-patterns.txt
/home/user/.TeXworks/configuration/texworks-config.txt
/home/user/.TeXworks/configuration/delimiter-pairs.txt
/home/user/.TeXworks/completion/tw-basic.txt
What is the correct command line to use? Google isn't very helpful. Now that I take a closer look at it, it actually seems to process some files.

Last edited by Weapon S; 12-10-2012 at 04:52 AM.
 
Old 12-10-2012, 04:52 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
To avoid problems with blank spaces you can try the -print0 action of find and the related -0 option of xargs. In this way the list of files will be separated by a null character and the spaces are considered as part of the file name, as it should be. Example:
Code:
find / -type f -iname "*.txt" -print0 2> /dev/null | xargs -0 grep --ignore-case mypattern
Hope this helps.
 
1 members found this post helpful.
Old 12-11-2012, 10:56 AM   #3
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian
Posts: 154
Blog Entries: 1

Original Poster
Rep: Reputation: 12
Thank you. Works like a charm! I've also discovered there is a -exec option on find, which works.
 
  


Reply


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
Need advice on a script to search many files for list of terms, append hits to list jimmy the saint Programming 1 07-11-2010 04:59 AM
list all files *not* containing pattern code-breaker Linux - General 8 05-03-2007 01:07 PM
What is the Package Search pattern Syntax for dpkg --list? Akhran Debian 3 08-23-2005 06:23 AM
list files NOT matching a pattern smart_sagittari Linux - Newbie 9 05-20-2005 06:32 AM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 12:57 PM


All times are GMT -5. The time now is 06:44 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration