Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

like image 589
Rudi Avatar asked Oct 14 '13 02:10

Rudi


People also ask

What is the Find command in bash?

The Bash find Command 101 The find command allows you to define those criteria to narrow down the exact file(s) you'd like to find. The find command finds or searches also for symbolic links (symlink). A symbolic link is a Linux shortcut file that points to another file or a folder on your computer.

What is wildcards in shell script?

A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches. Wildcards are commonly used in shell commands in Linux and other Unix-like operating systems.


1 Answers

find ./ -name "*pbs.e*"  -type f -size +10c
like image 126
Shicheng Guo Avatar answered Oct 22 '22 18:10

Shicheng Guo