Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 28

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

  1. #11
    Join Date
    Dec 2006
    Beans
    6,831

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

    Hi Slowspeed,

    Quote Originally Posted by Slowspeed View Post
    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.
    It is totally my pleasure . I am in the process of rewriting and expanding this material for the Ubuntu Community Wiki at the moment:

    find - Community Documentation
    https://help.ubuntu.com/community/find

    About another week of work to go on that one yet...

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

  2. #12
    Join Date
    Sep 2006
    Beans
    3,442

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

    I think this is an understated guide. Passing the results of find to other utilities just creates so many possibilities.

  3. #13
    Join Date
    Dec 2006
    Beans
    6,831

    Re: 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 .
    You think that's air you're breathing now?

  4. #14
    Join Date
    Apr 2012
    Beans
    1

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

    Heh, thanks for this one I've never used find for anything other than searching by file names! (until now)

  5. #15
    Join Date
    Dec 2006
    Beans
    6,831

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

    Quote Originally Posted by rainbowcats View Post
    Heh, thanks for this one I've never used find for anything other than searching by file names! (until now)
    Glad to hear the wiki has been useful to you .
    You think that's air you're breathing now?

  6. #16
    Join Date
    Mar 2007
    Location
    Denver, CO
    Beans
    7,552
    Distro
    Ubuntu 12.04 Precise Pangolin

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

    Off topic, but I'm up for an awk primer. Fantastic find guide. I'm glad its being promoted again.

  7. #17
    Join Date
    Dec 2006
    Beans
    6,831

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

    Quote Originally Posted by kevdog View Post
    Off topic, but I'm up for an awk primer. Fantastic find guide. I'm glad its being promoted again.
    Well, actually this guide was turned into a wiki a while back but I have just now altered the guide in line with the new way of doing things . As for an awk primer, I wish I had this knowledge!!
    You think that's air you're breathing now?

  8. #18
    Join Date
    Aug 2009
    Beans
    78

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

    Support is still available in this thread so feel free to ask any questions .
    Just noticed this thread when reading through the changes to Tutorials and Tips.

    Maybe this is more of a shell-scripting question, but here goes: I have a script "newest.sh" in ~/bin that looks like

    Code:
    format='%T@\t%s\t%Tx %TH:%TM:%TS\t%p\n';
    find $* -printf "$format" | sort -k1n|cut -f2-
    Basiically, it uses "find -printf" to get the time in seconds since the epoch, sorts by this field, then clips it off. The result is a list of all the files in the selected location, sorted from oldest to newest. Works like a charm, EXCEPT if I am trying to match a specific part of a file name, and there is a match in the top-level directory.

    For example: typing
    Code:
    newest.sh . -iname \*.pdf
    in a directory which happens to contain a file named "file.pdf" results in the error

    Code:
    find: paths must precede expression: file.pdf
    How can I insulate the argument to "-iname" from being interpreted by the shell?


    Thanks for any help.

  9. #19
    Join Date
    Dec 2006
    Beans
    6,831

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

    Oddly enough I cannot duplicate this error:

    Code:
    andrew@skamandros~/media/YouTube$ newest.sh 
    17981740	05/28/2009 01:42:49.0000000000	./Armistice_Day_1920_Homecoming_Of_An_Unknown_Warrior2.flv
    44465312	09/04/2009 09:45:32.0000000000	./Ruttie_Jinnah.flv
    19841820	03/23/2010 11:15:28.0000000000	./Cold_Chisel_Bow_River.mp4
    20235169	04/28/2010 17:55:55.0000000000	./ashtanga_yoga_demo.mp4
    438626221	12/02/2010 12:00:58.0000000000	./The_Art_Of_Piano_Great_Pianists_Of_The_20Th_Century.flv
    475711968	12/23/2010 23:59:27.0000000000	./Gary_Yourofsky.flv
    12730971	02/23/2011 14:17:30.0000000000	./Zenga_Zenga.flv
    93765772	08/03/2011 07:06:54.0000000000	./Dr_Swami_Shankardev_Rotation_Through_Chakras_Guided_Meditation.mp4
    79	12/13/2011 18:42:44.0000000000	./template
    0	04/18/2012 08:42:19.3180826960	./file.pdf
    4096	04/18/2012 08:42:19.3180826960	.
    andrew@skamandros~/media/YouTube$ newest.sh . -iname \*.pdf
    0	04/18/2012 08:42:19.3180826960	./file.pdf
    So I have to admit that I am not entirely sure what is happening here...
    You think that's air you're breathing now?

  10. #20
    Join Date
    Dec 2009
    Beans
    157

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

    Quote Originally Posted by George Heine View Post
    [...]

    For example: typing
    Code:
    newest.sh . -iname \*.pdf
    in a directory which happens to contain a file named "file.pdf" results in the error

    Code:
    find: paths must precede expression: file.pdf
    How can I insulate the argument to "-iname" from being interpreted by the shell?


    Thanks for any help.
    The problem here is not '-iname', but the stripping of the backslash from the shell, causing the unwanted expansion of '*.pdf' after find's '-iname' option, and the command line would have looked like:

    Code:
    find . -iname file.pdf other_file.pdf ...
    which is a syntax error, (but it'd have worked fine if it were only one pdf file in the search path). One way to avoid shell quoting issues is using "$@":

    Code:
    #!/bin/bash
    set -x
    
    format="%T@\t%s\t%Tx %TH:%TM:%TS\t%p\n"
    
    find "$@" -printf "$format" | sort -k1n | cut -f2-
    Or using arrays:

    Code:
    ...
    arr=("$@")
    
    find "${arr[@]}" -printf "$format" | sort -k1n | cut -f2-
    And running it:

    Code:
    ./newest.sh . -iname "*.pdf"
    P.S. For more convenience, I'd use quotes instead of backslashes when escaping special characters in the command line, for example:

    Code:
    ./newest.sh "dir with space/" -iname "*.pdf" -type f
    Last edited by erind; April 18th, 2012 at 03:07 AM.

Page 2 of 3 FirstFirst 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
  •