Forum Moderators: bakedjake
"find / -name 'tacos'" will return dirs, symlinks, devices, etc. as well as files named 'tacos'. Often you know what type of object you are searching for, so you can add qualifiers to limit your search.
If you want a file, add "-type f" ("-type d" for dirs, "-type l" for symlinks, etc.. see manpage for more details). Also, us oldtimers like to see a trailing "-print", mostly to be pedantic. It's the default action so you can leave it off, but getting in the habit if including it will prepare you for dealing with files that have spaces in the names (more later). So with "-type f" and "-print" added, it becomes:
find . -type f -name 'tacos' -print
To cover some of the "high-tech" features of find, there are also "-a" for "and" and "-o" for "or". The AND "-a" operator is implied and not required -- "find . -type f -name 'foo' -print" is the same as "find . -type f -a -name 'foo' -print" or "find . -a -type f -a -name 'foo' -a -print". (Starting to see a pattern to the logic?)
The OR -o operator is a little tricky. One way to work with it is to give each side of your -o operator its own -print, like this:
find . -type f -name foo -print -o -type d -name bar -print
find $HOME \( -type d -name .mh -prune \) -o -type f ..... -print
On the note of using \( \), it wouldn't hurt to use them with all of your -o operations just to clarify what you want done. The earlier command would then turn into:
find . \( -type f -name foo -print \) -o \( -type d -name bar -print \)
Along with find(1), there's xargs(1). xargs is a way of using find to perform actions on a list of files. For example, if you were looking for a file that contained the word "foo" in it, you could type:
find . -type f -print ¦ xargs egrep "foo"
If you have files with spaces in the name, you'll see some errors about not finding files. In that case, you need to use:
find . -type f -print0 ¦ xargs -0 egrep "foo"
xargs is much better than using find(1)s "-exec <command> {};" feature, since xargs bunches the list of files up into groups of about 256, where "-exec <command> {};" runs your command for every file found. Now that I've talked about it, here's the syntax to use -exec, even though you shouldn't ever use it (I never do):
find . -type f -exec egrep "foo" {};
Hope this helps.
Rob++ (Senior UNIX Sysadmin, at large)
alias fname 'find . -name "\!*" -print'
This establishes a new command "fname", which can be invoked with a pattern as argument:
$> fname *.pl
lists all Perl files in and below the current directory.
Note that you don't even need to escape the * in this call, as the \!* that expands the argument in the alias definition is already enclosed in double quotes there.
I don't know enough bash syntax to tell you how to define a similar alias there... :o
I dabbled around with defining a function in sh/ksh/bash to do the same thing but quickly discovered that it would be a huge hassle to get it to properly handle the * expansion.
It's interesting that tcsh does not expand any * uses before calling the alias. That behavior is unique to (tcsh's handling of) aliases, which would be rather confusing since it doesn't happen anywhere else in shells.
But yes, what you have works well.
: saturn; tcsh
> pwd
/tmp/windsor
> alias fname 'find . -name "\!*" -print'
> fname *test*
./testdata
./eretest
./bar/test
> exit
exit
Rob++