Results 1 to 10 of 28

Thread: Linux Basics: A gentle introduction to 'find'

Hybrid View

  1. #1
    Join Date
    Dec 2006
    Beans
    6,979

    Linux Basics: A gentle introduction to 'find'

    This guide has been converted to a wiki:

    https://help.ubuntu.com/community/find

    Support is still available in this thread so feel free to ask any questions .
    Last edited by andrew.46; April 9th, 2012 at 10:20 AM.
    You think that's air you're breathing now?

  2. #2
    Join Date
    Sep 2006
    Beans
    3,500

    Re: Linux Basics: A gentle introduction to 'find'

    Yet another excellent guide. Good job, Andrew.

  3. #3
    Join Date
    Dec 2006
    Location
    /dev/null
    Beans
    347

    Re: Linux Basics: A gentle introduction to 'find'

    Nice guide... can you add searching within files? For example, there are a number of documents in a given directory. You want to find a keyword within them.

  4. #4
    Join Date
    Dec 2006
    Beans
    6,979

    Re: Linux Basics: A gentle introduction to 'find'

    Hi graysky,

    Quote Originally Posted by graysky View Post
    Nice guide... can you add searching within files? For example, there are a number of documents in a given directory. You want to find a keyword within them.
    There is an immense amount of material omitted in this guide, in part because this is intended to be an introduction only. But definitely I shall consider adding something like this in the near future. A 'real world' example of the usage of grep that you mentioned could be in my case finding all of the html files that mention 'slrn' in my offline website:

    Code:
    $ find $HOME/html/andrews-corner -exec grep -q 'slrn' '{}' \; -print
    /home/andrew/html/andrews-corner/leafnode.html
    /home/andrew/html/andrews-corner/index.html
    /home/andrew/html/andrews-corner/slrn.html
    /home/andrew/html/andrews-corner/custom_os.patch
    /home/andrew/html/andrews-corner/mutt.html
    Fantastic program!!

    Andrew
    You think that's air you're breathing now?

  5. #5
    Join Date
    Dec 2006
    Location
    /dev/null
    Beans
    347

    Re: Linux Basics: A gentle introduction to 'find'

    Very cool.

    How can I add an alias to my ~/.bashrc that will allow me to do this on the shell just by typing the alias and word or phrase?

    For example, the alias might be called lookfor and what comes after that could be what you wanna search for inside the files in the current directory.

    Example:

    Code:
    $ lookfor lsrn
    That would then do this:
    Code:
    find ./ -exec grep -q 'slrn' '{}' \; -print
    How can I do that?

    Also, how would you handle a group of words?

  6. #6
    Join Date
    Dec 2006
    Beans
    6,979

    Re: Linux Basics: A gentle introduction to 'find'

    Hi graysky,

    Quote Originally Posted by graysky View Post

    How can I add an alias to my ~/.bashrc that will allow me to do this on the shell just by typing the alias and word or phrase?
    An interesting question, and one that I had to do a little research on before I could answer properly . An actual alias would not do as bash aliases are not really meant to contain variables. What you ask for can be set as a bash function instead:

    Code:
    lookfor() {
      find . -exec grep -q "$1" '{}' \; -print 
    }
    This would search the working directory and subdirectories with the syntax:

    Code:
    lookfor search_term
    which is what you specified. However a better idea would be to use two variables, the first being the path to search and the second being the search term:

    Code:
    lookfor() {
      find "$1" -exec grep -q "$2" '{}' \; -print 
    }
    and the syntax would then be:

    Code:
    lookfor path search_term
    Place this in ~/.bashrc and have a play with it. Mind you this starts moving towards a shell script which would of course be much more flexible.

    Also, how would you handle a group of words?
    I am not entirely sure what you mean there, although I sense that your needs are slipping further away from 'find' and leaning more towards grep . In fact perhaps a simpler search with 'find' piped to grep would be better. Can you provide a specific example of what you need?

    All the best,

    Andrew
    You think that's air you're breathing now?

  7. #7
    Join Date
    Dec 2006
    Beans
    6,979

    Re: Linux Basics: A gentle introduction to 'find'

    Hi FakeOutdoorsman,

    Quote Originally Posted by FakeOutdoorsman View Post
    Yet another excellent guide. Good job, Andrew.
    Thanks very much! I plan about a dozen of these in a series, partly because it fills a need and partly because with vdpau, FFmpeg-mt, PPA archives, impending rc3 release etc I will be slipping out of the MPlayer business soon .

    Andrew
    You think that's air you're breathing now?

  8. #8
    Join Date
    Feb 2010
    Location
    Cobourg
    Beans
    163

    Re: Linux Basics: A gentle introduction to 'find'

    I'm looking to be able to find a bunch of pdfs, move those pdfs to another directory, keep the directory structure, and remove the previous structures.
    So far I have been able to use the find command and copy the files while keeping the structure:

    find /home/john/books1/ -regex ".*\(pdf\|epub\|mobi\)$" -type f -exec cp '{}' /home/john/temp/ ';'

    but if I use mv I get all the files in one directory...

    find /home/john/books1/ -regex ".*\(pdf\|epub\|mobi\)$" -type f -exec mv -b '{}' /home/john/temp/ ';'


    Am I stuck using 'cp'? Which is fine, I just need to find a way to then delete them...

    find /home/john/books1/ -regex ".*\(pdf\|epub\|mobi\)$" -type f -exec rm '{}' /home/john/temp/ ';'

    I plan to also have this in a cron job but that shouldn't be a problem once I have figured out the script

    Thanks for any help


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
  •