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

Thread: Simplifying the find command

  1. #1
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Simplifying the find command

    Hi Folks,

    I recently learned how to use:

    Code:
    find / -name *Whatever I'm looking for* -print 2> /dev/null
    to find things on my system.

    My question is, is there a way to set this as a bash alias that will allow me to put whatever I want to search for between the *s before running the command?

    Ideally I'd type the alias in the terminal and then be able to edit the command before it launches. It seems that when whenever I use an alias, the command is executed right then.
    Last edited by GrouchyGaijin; March 10th, 2013 at 11:20 AM.
    Thank you,
    GG -----------

  2. #2
    Join Date
    Mar 2013
    Location
    Czech Republic
    Beans
    188
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Question about simplifying the find command

    I think they are not, this is just final form of the command, it just needs those argument. But maybe, you can create shortcut for running terminal in /dev/null/ and then you could not write it there..

  3. #3
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Question about simplifying the find command

    In most cases locate is faster than find for this sort of thing:

    Code:
    locate "whatever you're looking for"
    NB: locate uses a database of files (update periodically) to speed up access, so if you've just added some new files and you need to look for something, you probably want to update that database:

    Code:
    sudo updatedb

    As for your actual question, I'd use a bash function: http://tldp.org/LDP/abs/html/functions.html

    E.g if you put this in your .bashrc file:

    Code:
    function greet {
        echo "Hello, $1"
    }
    You could then do this:

    Code:
    $ greet michael
    Hello, michael
    $
    I'm sure you can see how you could adapt this.
    Last edited by MG&TL; March 10th, 2013 at 09:28 AM.

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

    Re: Question about simplifying the find command

    Code:
    read -p "Searching for: " search_string; find / -name "$search_string" 2>/dev/null
    Last edited by prodigy_; March 10th, 2013 at 09:42 AM.

  5. #5
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Question about simplifying the find command

    I suggest that you keep the freedom of choice and use the wild card character * in your search pattern. If you add sudo, you will be allowed to search also directories where the normal user has no access. So I suggest the following two aliases, localfind to search the current directory and its subdirectories, and rootfind searching the whole computer.

    Code:
    alias localfind='sudo find \. -name'
    alias rootfind='sudo find / -name'
    Enter these (or something similar) into your .bashrc file at the alias commands that are already there!

    So if you don't want to wait for rootfind, it can be a good idea to change directory to a relevant directory and run localfind.

    examples:

    Code:
    cd
    localfind "*bash*"
    Code:
    cd /etc
    localfind "*host*"
    localfind "*grub*"
    Code:
    rootfind "*grub*"
    Edit: another example shows the advantage with flexible usage of *
    Compare the output of these commands:
    Code:
    cd /usr
    localfind "*.doc*"
    # and
    localfind "*.doc"
    Good luck
    Last edited by sudodus; March 10th, 2013 at 10:26 AM. Reason: flexibility with *

  6. #6
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Simplifying the find command

    Thank you for all of the answers guys, I really appreciate it.

    I have follow up questions so I'll go in order that you replied.

    @MG&TL: When I try locate, even after updating the database, I am unable to find files that I know exist. For example, a song I have stored on the internal hard drive and on an external hard drive. Using the command I posted however, finds both copies of that song.

    I'm afraid I don't understand how a function works (I'll check the link you posted.), basically what I want to do is be able to type a short command followed by the string I am looking for. In your example:

    Code:
    function greet {    echo "Hello, $1"
    }
    where is the search string?


    @Prodigy_

    Your suggestion looks like something I was imagining. Where exactly would I put your code?
    Code:
    read -p "Searching for: " search_string; find / -name "$search_string" 2>/dev/null
    and how would I call it?


    @sudodus

    I tried putting
    Code:
    alias rootfind='sudo find / -name'
    in my list of bash_aliases and then running:
    Code:
    rootfind part-of-the-name-of-file
    The search returned no results.


    However if I run
    Code:
    find / -name *part-of-the-name-of-the-file* -print 2> /dev/null
    I get a list of, using the example of searching for a specific song, two hits, both versions of the song I'm looking for. One on the internal hard drive and one on the external drive.

    So basically, the code I have works and does what I want. I'm just looking for a way to be able to type something shorter in the terminal.
    Thank you,
    GG -----------

  7. #7
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Simplifying the find command

    Quote Originally Posted by GrouchyGaijin View Post

    Code:
    rootfind part-of-the-name-of-file
    The search returned no results.

    You need the wild card characters! I explained why I suggest you use them, and showed with examples

    Code:
    rootfind "*part-of-the-name-of-file*"


    It is also good to enclose in double-quotes ("), otherwise there will be problems when instances are found in the current directory. Try the examples that I supplied. If they don't work for you, I will try hard to improve them.

    I think Prodigy_'s suggestion is fine. Make an alias of it!

  8. #8
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Simplifying the find command

    Prodigy_'s solution works well.

    Thanks guys!
    Last edited by GrouchyGaijin; March 10th, 2013 at 11:20 AM. Reason: Problem solved
    Thank you,
    GG -----------

  9. #9
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Simplifying the find command

    Quote Originally Posted by GrouchyGaijin View Post
    Thank you for all of the answers guys, I really appreciate it.

    I have follow up questions so I'll go in order that you replied.

    @MG&TL: When I try locate, even after updating the database, I am unable to find files that I know exist. For example, a song I have stored on the internal hard drive and on an external hard drive. Using the command I posted however, finds both copies of that song.

    I'm afraid I don't understand how a function works (I'll check the link you posted.), basically what I want to do is be able to type a short command followed by the string I am looking for. In your example:

    Code:
    function greet {    echo "Hello, $1"
    }
    where is the search string?
    Hm. That's weird, maybe locate stays on the same disk if it can.

    Functions allow you to substitute arbitrary commands into sub-commands. Sorry, I should have been a little bit clearer, the example I posted was just an example. You probably want something like (in bashrc):

    Code:
    function search {
        find / -name $1 -print 2> /dev/null
    }
    Then you could do:

    Code:
    search "Whatever you're looking for"

  10. #10
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Simplifying the find command

    Quote Originally Posted by GrouchyGaijin View Post
    ...
    As for "I think Prodigy_'s suggestion is fine. Make an alias of it!"
    Bear with me, how exactly do I make an alias of this?:
    Code:
    alias myfind='read -p "Searching for: " search_string; find / -name "$search_string" 2>/dev/null'
    You can run this in your terminal window and get a temporary alias. Run it with
    Code:
    myfind
    and if you want it permanent, enter it into your .bashrc (or .bash_aliases file).

    The following alias is a modified version of Prodigy_'s suggestion, which includes the wild cards according to the OP. Choose the one that is better for you
    Code:
    alias myfind='read -p "Searching for: " search_string; find / -name "*$search_string*" 2>/dev/null'
    Good luck
    Last edited by sudodus; March 10th, 2013 at 12:32 PM. Reason: final (?) modification

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
  •