Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to search for text files only ?

  1. #1
    Join Date
    Jun 2012
    Beans
    310

    How to search for text files only ?

    I've found out this is not as easy as I thought at first,on the other OS we know,all text files usually end with the .txt extension,which makes searching for them quite straightforward-no such thing on Linux,so how can I search for text files in a directory where they may be (and actually are) intermixed with all kinds of other files ? Typically (despite using Tomboy as well) I take lots of unorganized notes as short text files,which then end up somewhere in my home directory,then at some point I'd want to grep for a search term or string among all these files,but there's all sorts of other files in between,so a simple command the likes of find ~ -type f |grep [string] won't cut it,obviously
    Last edited by cogset; January 25th, 2013 at 05:34 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    Georgia USA
    Beans
    225
    Distro
    Xubuntu

    Re: How to search for text files only ?

    Catfish is a great GUI tool for searching local files.
    It's in the repos. Search Synaptic or Software Center or "sudo apt-get install catfish".

    Set search folder to /home/<Your_User_Name_Here>
    Search for *.foo
    All file types "Foo" will show up in Catfish's search results.

    It also has a filter for file types. If you want just text files for a string, say, "Resume." All related text type files will appear in the field.

    Also, create a directory for notes! /home/USER/Documents/NOTES would solve a lot of your headache and help narrow what you're searching for.
    You can change Tomboy's default save directory under Preferences > Synchronization.

    Unless you're asking for a way to search a text string within the body of a set of text files. It's unclear what you're really asking for.
    And, isn't sanity really just a one-trick pony anyway? I mean, all you get is one trick: "rational thinking."
    But, when you're good and crazy? Oooh! Oooh! Oooh! The sky is the limit!

  3. #3
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How to search for text files only ?

    Code:
    find . -exec sh -c "file -b --mime-type {} | grep -q '^text/'" \; -print

  4. #4
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: How to search for text files only ?

    Quote Originally Posted by cogset View Post
    on the other OS we know,all text files end with the .txt extension
    Unfortunately that's not true. Extension is a convenience tool, it doesn't magically define file type. It can be deliberately misleading or even completely absent.

  5. #5
    Join Date
    Jun 2012
    Beans
    310

    Re: How to search for text files only ?

    Thanks folks for all the insights,not least the simple advice of creating a dedicated directory just for notes:I'm now giving Catfish a try,although the string suggested above
    Code:
    find . -exec sh -c "file -b --mime-type {} | grep -q '^text/'" \; -print
    is probably what I was after,if only I could understand what it actually does in detail .
    I also probably didn't explain myself very well,so I'll try to do better this time-the typical scenario would be like that:I have an habit of taking lots of short notes as text files scattered around my home directory (/home itself,Documents,Desktop and some subdirectories-I know,that's not very tidy,paradoxically I do this in the effort of avoiding to clutter Tomboy with possibly unworthy stuff),then at some point remember that one of those notes was about,say,"firefox bookmarks",now I'd like to grep for this exact string amongst *all* text files in my entire home directory,and that's the hard part,as there's no extension identifying them as text files.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to search for text files only ?

    Code:
    find . -exec sh -c "file -b --mime-type {} | grep -q '^text/'" \; -print
    =
    Code:
    find <current_dir> (get mime of the file and check if 'text/' is in it) -print file
    bold part is responsible for checking mimetype
    -exec is used to embed external commands in find, sh -c opens a subshell where file -b --mimetype {} is executed ({} is a placeholder for the currently processed filename)
    Code:
    $ file -b --mime-type sample.txt
    text/plain
    $ file -b --mime-type test.c
    text/x-c
    that output is passed to grep to figure out if the phrase text/ appears in it at the very beginning. Grep ends with exit code of 0 (success) or 1 (failure). -print part that prints out the current {} depends on that exit code: success = execute, failure = skip. In short the whole -exec sh -c acts here as a condition controlling the -print part
    Last edited by Vaphell; January 25th, 2013 at 06:44 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: How to search for text files only ?

    now I'd like to grep for this exact string amongst *all* text files in my entire home directory,and that's the hard part
    If it is string matching you want throughout *all* text files then I would recommend trying Recoll package which is found in Ubuntu Software Centre.

    First it has to index all your files.

  8. #8
    Join Date
    Jan 2009
    Location
    Nelspruit, South Africa
    Beans
    1,415
    Distro
    Ubuntu Development Release

    Re: How to search for text files only ?

    I just do
    Code:
    locate *.txt
    Monitor: LG flatron w1954s 22", mobo: G31-m7 series, CPU: Intel LGA 775 Intel core duo 2 2,66GHZ, RAM: 3GB, GPU: ATI Radeom HD 2400, Storage: 500GB SATA and 80GB IDE, Windows 7 ultimate and Ubuntu

  9. #9
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: How to search for text files only ?

    that doesn't find strings within *.txt

  10. #10
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How to search for text files only ?

    Quote Originally Posted by Vaphell View Post
    Code:
    find . -exec sh -c "file -b --mime-type {} | grep -q '^text/'" \; -print
    =
    Code:
    find <current_dir> (get mime of the file and check if 'text/' is in it) -print file
    bold part is responsible for checking mimetype
    -exec is used to embed external commands in find, sh -c opens a subshell where file -b --mimetype {} is executed ({} is a placeholder for the currently processed filename)
    Code:
    $ file -b --mime-type sample.txt
    text/plain
    $ file -b --mime-type test.c
    text/x-c
    that output is passed to grep to figure out if the phrase text/ appears in it at the very beginning. Grep ends with exit code of 0 (success) or 1 (failure). -print part that prints out the current {} depends on that exit code: success = execute, failure = skip. In short the whole -exec sh -c acts here as a condition controlling the -print part
    Congratulations on a clear, succinct and perfect explanation.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •