Page 1 of 3 123 LastLast
Results 1 to 10 of 28

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

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

    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,480

    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,922

    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?

  5. #5
    Join Date
    Dec 2006
    Beans
    6,922

    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?

  6. #6
    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?

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

    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?

  8. #8

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

    Superb guide, its really very helpful for me, Thanks andrew for sharing with us.

  9. #9
    Join Date
    Dec 2006
    Beans
    6,922

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

    Hi paulstephen4,

    Quote Originally Posted by paulstephen4 View Post
    Superb guide, its really very helpful for me, Thanks andrew for sharing with us.
    Thank you very much for your kind words,

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

  10. #10
    Join Date
    Oct 2008
    Location
    Illinois,United States
    Beans
    31
    Distro
    Ubuntu 9.04 Jaunty Jackalope

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

    Thank you for a clear and concise intro guide. I have been weening myself off of gui now that I built a home server and appreciate this type of reference material.

Page 1 of 3 123 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
  •