LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-26-2006, 12:35 AM   #1
smudge|lala
Member
 
Registered: Jan 2004
Location: New Zealand
Distribution: Mint | Sabayon
Posts: 160

Rep: Reputation: 16
Find a string lost in a filesystem..


I have tried a number of commands and mixtures of commands in bash that might do this. I have had no luck yet!

It is a common problem Ihave as I have so many files and large filesystems. I can locate a file, but what if I'm looking for something specific in a file, either a binary signature, or a simple text string?

I have tried locate, find, grep and various mixtures but no luck.

If I am looking for a string "John's cell is 01234" and I know it's lost in a text file on /mnt/sda1/ how do I begin to find it? This must be possible inbash, everything else is! Thanks.
 
Old 03-26-2006, 01:44 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If it is in a text file, you can use grep to find it.
If the text file is in a subdirectory you could use find to locate tiles of type "-f".
Do you know if the file has a certain extension? Or the approximate size?
Do you know the modification date? If there are a lot of files, the more tests you can apply, the fewer files you need search with grep.

example:
find ./ -type f -size -100k -ctime -20 -print0 | xargs -0 grep 'John'\''s cell is 011234'

You need to break up the string so that you can escape the single quote.

You could also search for the substring "is 011234" eliminating the single quote from the search.
 
Old 03-26-2006, 10:19 AM   #3
smudge|lala
Member
 
Registered: Jan 2004
Location: New Zealand
Distribution: Mint | Sabayon
Posts: 160

Original Poster
Rep: Reputation: 16
Finding a string with grep

Thank you jschiwal, this command will prove to be very useful. I am not actually looking for a specific string right now, only concerned with how I can find such information when I need it, as I often do. There are many variables to consider looking at the power of the few commands you used, but once I understand all the options, I will likely use this command a great deal.

Reflecting on the command,

Code:
find ./ -type f -size -100k -ctime -20 -print0 | xargs -0 grep '011234'

find <from root> -type <a file> -size <size of file in B,Kb,Mb> -ctime <time file last changed in multiples of 24 hours - so 20*24> -print0 <eprint the full file name on the standard output if true> | xargs -0 <not sure about this command, manpage is confusing> grep <'string'>
I tried this a few times and it works very nicely. Does it work with -size < or > than?

I understand there is a relation between -print0 and xargs, not sure what it is as I don't really understand xargs.

Is it possible to search a binary content as well? I am not very experienced with hex editors in Linux, but can't live without one! After all it's the power of searching and finding file content I'm interested in, regardless of the content. It is a rare task to search a binary, but can be handy with virus scans, or looking for a bug.

Last edited by smudge|lala; 03-26-2006 at 10:34 AM.
 
Old 03-26-2006, 08:08 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The -print0 option of find has a corresponding -0 option in xargs. A null is used to delineate the arguments. This allows handling files with spaces in the filenames. There may still be a problem with some special characters such as & or !.

The tests like -size or -cmin take arguments in three forms. N, +N, -N, for equal to N, Greater than N, Less than N. Also, the size argument can have a letter at the end.

If you enter "info:find" in the konqueror browser, you may get a html version of the info help files.

I would recommend installing the source of findutils. If it is a .src.rpm that you install, you can apply the patches with "rpmbuild -bp find.spec". Then the source in the BUILD directory has a "make pdf" target. ( Also ps and dvi targets for different document versions ). If you download a tarball source, "./configure && make pdf" will generate the same document; "Finding Files, by David MacKenzie". This is a 60 page print worthy manual for the find, locate and xargs commands.
 
Old 03-29-2006, 04:37 PM   #5
smudge|lala
Member
 
Registered: Jan 2004
Location: New Zealand
Distribution: Mint | Sabayon
Posts: 160

Original Poster
Rep: Reputation: 16
This is a very cool command. I've used it many times since, thanks. Think I'll have to add it to a list of 'greatest short commands' along with du -s * | sort -nr > $HOME/space_report.txt
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
Find a value following a string sgracelin Linux - Newbie 1 07-22-2005 08:41 AM
How to find a string in a directory? iclinux Linux - Newbie 2 07-02-2005 08:20 AM
find a string and write it somewhere eicherlist Programming 11 01-02-2004 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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