14

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]

5
  • You'll need a function, not an alias, because the argument needs to go in the middle of the expansion. fn() { find . -name "$1" -print; } Commented Oct 14, 2013 at 2:54
  • I tried this. But the "$1" in the function gets expanded in the current directory. So, if I try fn *.txt in a directory that has the file a.txt, then the command becomes fn a.txt instead of fn *.txt
    – Rudi
    Commented Oct 14, 2013 at 3:03
  • 1
    Aha... The answer below solved this. I needed quotes both when calling and in the function description. Thanks for the very quick replies!
    – Rudi
    Commented Oct 14, 2013 at 3:21
  • You will always need to quote the argument to the function if it contains wildcards…but the function as written seems to work for me when invoked as fn "*.c", for example. You can't avoid quoting the wildcard expression in the invocation of fn. Commented Oct 14, 2013 at 3:55
  • I might have improved your solution slightly. By making my function fn() { find . -iname "*$1*" -print} I can just type fn word and I find all files with word, Word, WORD, WoRd or similar anywhere in the filename. That's what I was after, maybe you were after something different.
    – suprjami
    Commented Mar 9, 2014 at 12:05

3 Answers 3

15

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\*
4
  • Yes, I expected that. :( But I can live with that. What I don't know how to do is to write an alias or function that I can include in my .bashrc file so that all I have to type is: fn Bill* or similar.
    – Rudi
    Commented Oct 14, 2013 at 2:53
  • Sorry. My reply was vague. I don't know how to write an alias or function such that all I have to type is: fn "Bill*" (I don't mind typing the quotes; I do mind having to type f i n d - n a m e - p r i n t so frequently. Thanks for the quick replies, btw!
    – Rudi
    Commented Oct 14, 2013 at 3:00
  • I may be missing something, but my attempt to apply that straightforwardly didn't work. I've edited the original post to show the function I tried and how it fails.
    – Rudi
    Commented Oct 14, 2013 at 3:10
  • If you're quoting it in the function and you're quoting it in the command line then you should not be getting the results you're seeing. Commented Oct 14, 2013 at 3:14
8
find ./ -name "*pbs.e*"  -type f -size +10c
0

I'm an inexperienced bash user moving from csh. In my .cshrc I have the alias:

alias f  'find . -name "\!*"'

which allows searching for files and directories using unquoted wildcards, including a simple *. For example:

  • f *.cpp => lists all *.cpp files in directory/subdirectories
  • f * => lists all files in directory/subdirectories
  • f *x* => lists all files containing an 'x' in their name

To achieve the same in bash I've added the following function to my .bashrc:

f() {
find . -name "$(history 1 | awk -F ' ' '{print $NF}')" -print
} 

which seems to be working for me, without requiring quotes.

The shell will need to have:

set -o history && set -o histexpand

which I think is true for an interactive shell (at least it is for me).

I did try using history expansion, but it didn't seem to work within a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.