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

Thread: How can I limit the number of results with "find" command?

  1. #1
    Join Date
    Oct 2006
    Beans
    584
    Distro
    Ubuntu

    How can I limit the number of results with "find" command?

    Well thats the question... I want it to stop at the first match.

  2. #2
    Join Date
    Oct 2005
    Location
    IN, USA
    Beans
    274

    Re: How can I limit the number of results with "find" command?

    one simple way is to pipe it through "head" for example:

    find . -name "whatever" | head -n 1

  3. #3
    Join Date
    Dec 2005
    Location
    Copenhagen, Denmark
    Beans
    386
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: How can I limit the number of results with "find" command?

    I always use grep together with find. E.g if i look for a conf to X11 i would do

    find / -name *conf | grep X11

    This will search for conf and X11 at the same time.

    Output is
    /media/sda6/etc/X11/xorg.conf
    /media/sda6/etc/X11/gdm/gdm.conf
    /media/sda6/etc/X11/imwheel/startup.conf
    /media/sda6/etc/X11/dm.d/30xdm.conf
    /media/sda6/etc/X11/dm.d/20gdm.conf
    /media/sda6/etc/X11/dm.d/10kdm.conf
    /etc/X11/xorg.conf


    (I just cannot find a good example right now)
    Last edited by tkjacobsen; May 31st, 2007 at 08:26 PM.
    Troels Kofoed Jacobsen
    Consult the wiki before posting: wiki.ubuntu.com

  4. #4
    Join Date
    Oct 2006
    Beans
    584
    Distro
    Ubuntu

    Re: How can I limit the number of results with "find" command?

    Thanks for the answers.
    Here is the problem:
    I want to make a script to automatically create .desktop files from a lot of folders with pictures inside. Each desktop file will link to a folder having a picture for the icon.

    The problem with the suggested solution is that it will do a lot of extra work.

    "locate" has an option to limit the amount of results (-n)
    Can "locate" search in an specified path? How?

  5. #5
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: How can I limit the number of results with "find" command?

    Quote Originally Posted by Yuzem View Post
    Well thats the question... I want it to stop at the first match.
    Just use the -quit action.

    Code:
    $ find . -type f -print -quit
    This finds the first regular file, prints it, and stops. Also look at the -depth and -prune options for limiting the search space.

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: How can I limit the number of results with "find" command?

    well if its just to find the first regular file, another way
    Code:
    ls -l /dir | awk '/^-/{print;exit}'

  7. #7
    Join Date
    Oct 2006
    Beans
    584
    Distro
    Ubuntu

    Re: How can I limit the number of results with "find" command?

    The -quit action works perfectly.
    Thanks.

  8. #8
    Join Date
    Oct 2006
    Beans
    584
    Distro
    Ubuntu

    Re: How can I limit the number of results with "find" command?

    One more problem...
    I need the full path to the file. The .desktop file does not work with relative paths (or I don't know how to make it work)

    Code:
    ~/Desktop/test$ pwd
    /home/azd/Desktop/test
    ~/Desktop/test$ find . -type f -print -quit
    ./12/test.jpg
    ~/Desktop/test$ echo $(pwd)$(find . -type f -print -quit)
    /home/azd/Desktop/test./12/test.jpg
    I can't get rid of that dot there.
    I am sure this is a very basic thing but I am a complete beginner.

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: How can I limit the number of results with "find" command?

    Code:
    find $(pwd) -type f -print -quit
    or define the path in a variable and use it in find.

  10. #10
    Join Date
    Oct 2006
    Beans
    584
    Distro
    Ubuntu

    Re: How can I limit the number of results with "find" command?

    So simple...
    Thanks!

Page 1 of 2 12 LastLast

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
  •