开发者

Using an alias in find -exec

I have a very long command in bash, which I do not want to type all the time, so I put an alias in my .profile

alias foo='...'

Now I开发者_JS百科 want to execute this alias using find -exec

find . -exec foo '{}' \;

but find cannot find foo:

find: foo: No such file or directory

Is it possible to use an alias in find?


find itself doesn't know anything about aliases, but your shell does. If you are using a recent enough version of bash (I think 4.0 added this feature), you can use find . -exec ${BASH_ALIASES[foo]} {} \; to insert the literal content of the alias at that point in the command line.


Nope, find doesn't know anything about your aliases. Aliases are not like environment variables in that they aren't "inherited" by child processes.

You can create a shell script with the same commands, set +x permissions and have it in your path. This will work with find.


Another way of calling an alias when processing the results of find is to use something like this answer

so the following should work:

alias ll="ls -al"
find . -type d | while read folder; do ll $folder; done


I am using the ll commonly know alias for this example but you may use your alias instead, just replace ll in the following line with your alias (foo) and it should work:

find . -exec `alias ll | cut -d"'" -f2` {} \;

your case:

find . -exec `alias foo | cut -d"'" -f2` {} \;

Note it assumes your alias is quoted using the following syntax:

alias foo='your-very-long-command'


It's not possible (or difficult / error-prone) to use aliases in the find command. An easier way to achieve the desired result is putting the contents of the alias in a shellscript and run that shellscript:

alias foo | sed "s/alias foo='//;s/'$/ \"\$@\"/" > /tmp/foo
find -exec bash /tmp/foo {} \;

The sed command removes the leading alias foo=' and replaces the trailing ' by "$@" which will contain the arguments passed to the script.


You can use the variable instead.

So instead of:

alias foo="echo test"

use:

foo="echo test"

then execute it either by command substitution or eval, for instance:

find . -type f -exec sh -c "eval $foo" \;

or:

find . -type f -exec sh -c "echo `$foo`" \;

Here is real example which is finding all non-binary files:

IS_BINARY='import sys; sys.exit(not b"\x00" in open(sys.argv[1], "rb").read())'
find . -type f -exec bash -c "python -c '$IS_BINARY' {} || echo {}" \;


I ran into the same thing and pretty much implemented skjaidev's solution.

I created a bash script called findVim.sh with the following contents:

[ roach@sepsis:~ ]$ cat findVim.sh                                                                                                        #!/bin/bash                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
find . -iname $1 -exec vim '{}' \;

Then I added the the .bashrc alias as:

[ roach@sepsis:~ ]$ cat ~/.bashrc | grep fvim                                                                                         
alias fvim='sh ~/findVim.sh'

Finally, I reloaded .bashrc with source ~/.bashrc.

Anyways long story short I can edit arbitrary script files slightly faster with: $ fvim foo.groovy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜