LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 03:47 AM   #1
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 262
Blog Entries: 2

Rep: Reputation: 49
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 03:52 AM.
 
Old 12-10-2012, 03:52 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 09:56 AM   #3
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 262

Original Poster
Blog Entries: 2

Rep: Reputation: 49
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 03:59 AM
list all files *not* containing pattern code-breaker Linux - General 8 05-03-2007 12:07 PM
What is the Package Search pattern Syntax for dpkg --list? Akhran Debian 3 08-23-2005 05:23 AM
list files NOT matching a pattern smart_sagittari Linux - Newbie 9 05-20-2005 05:32 AM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM

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

All times are GMT -5. The time now is 03:27 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
Open Source Consulting | Domain Registration