Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.