bashfunctionaliaswildcard

Bash shell...find command...names with wildcards...alias or function


I've long used the find command for finding files and directories in the current directory and all subdirectories that match a pattern:

find . -name "*.txt" -print
find . -name "Bill*" -print

But what I'd really like is an alias or function that will properly pass the wildcard. (I used to be able to do this in csh, but now I'm using bash.) If an alias or function named "fn" were set properly, I could save some time by just typing:

fn "*.txt"
fn "Bill*"

Ideally, I'd like to lose the quotation marks too, but I'm guessing that might not be possible because the shell will expand them before calling "fn".

Any advice would be greatly appreciated and will postpone carpal tunnel syndrome.... :)

SOLVED: After the discussion below, I put this in my .bashrc file:

fn () {
  find . -name "$1" -print
}

Note the quotes around the argument: "$1". This can then be called with more quotes around the filename expression:

fn "*.txt"

EDIT: must have spaces between the function name and the parentheses, so fn () { ... [works] fn() { ... [doesn't work]


Solution

  • Unfortunately the wildcards must be quoted or the shell will expand them if possible.

    Fortunately there are multiple ways of quoting them.

    fn '*.txt'
    fn Bill\*