-1

I'm having trouble creating an alias so I can run find in "silent mode" (without all the Permission denied messages). my bash skills are pretty bad so I'm not sure how to fix this

here's my alias in ~/.bashrc:

alias find='function _find(){find / -name "$1" 2>&1 | grep -v "Permission denied"}'

so if I run source ~/.bashrc and then sudo find "Exercism", I get an output of find: ‘Exercism’: No such file or directory

but running find / -name "Exercism" 2>&1 | grep -v "Permission denied" yields the correct result.

I'm not sure I'm passing the argument right, or at all, into the aliased find function.

4
  • Drop the alias and just put the function into your .bashrc or .bash_aliases. Call _find from the command line. It is not a good idea to always search /.
    – markgraf
    Oct 9 '19 at 14:57
  • 1
    On a sidenote: when your alias is the same as the command itself, you want to alias somecommand='command somecommand --with-options whatever'.
    – markgraf
    Oct 9 '19 at 15:00
  • If you want to drop error message find [...] 2&>/dev/null. That's what i use in general.
    – Julien B.
    Oct 9 '19 at 15:18
  • I didn't realize that I was defining my function wrong, and for using this command with sudo, it would need to be in a script anyway. Oct 9 '19 at 18:35
4

Think of aliases as substituting a word with some other text in shell code before it is interpreted.

With a find alias defined as:

alias find='function _find(){find / -name "$1" 2>&1 | grep -v "Permission denied"}'

When you type

find

at the prompt, that gets substituted with:

function _find(){find / -name "$1" 2>&1 | grep -v "Permission denied"}

If the shell was zsh, that code would define a _find function whose body is find / -name "$1" 2>&1 | grep -v "Permission denied". In other shells including bash, you would get a syntax error either because function f() is not the proper variable declaration syntax or (like in bash or yash) because that {find... would be treated as a simple command and bash and yash happen to be the only shells that don't support simple commands as function body.

Here, you want do define a function, not an alias, and since that function would have a different API from the find command's, you'd want to call it my another name like:

myfind() {
  find / -name "$@" 2>&1 | grep -v "Permission denied"
}

In any case, whether that's an alias or a function, as both are internal features of the shell, you won't be able to use them from sudo. For sudo to be able to call it, you'd have to make it a real command like a script:

#! /bin/sh -
find / -name "$@" 2>&1 | grep -v "Permission denied"

You'd need make that script executable and store it in a directory that is in sudo's secure_path if defined.

1
  • thanks for this. looks like it's probably not necessary. Oct 9 '19 at 18:34
1
alias find='function _find() { find / -name "$1" 2>&1 | grep -v ": Permission denied$"; } ; _find'

Your alias defines a function. You want it do define a function and call it.

4
  • Why is the alias needed at all though?
    – jesse_b
    Oct 9 '19 at 14:25
  • 1
    The alias is not needed as such. But the original poster wanted to work this with an alias, for example because they wanted to understand how argument passing works. Ours not to question why.
    – AlexP
    Oct 9 '19 at 14:41
  • The trouble comes when they try to sudo call it...
    – Jeff Schaller
    Oct 9 '19 at 15:19
  • 2
    @JeffSchaller: Shell functions have the same problem...
    – AlexP
    Oct 9 '19 at 15:21

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