Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an alias in find -exec

Tags:

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 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?

like image 695
Jesse van Bekkum Avatar asked Oct 04 '11 20:10

Jesse van Bekkum


People also ask

How do I find an alias in Linux?

To see a list of aliases set up on your linux box, just type alias at the prompt. You can see there are a few already set up on a default Redhat 9 installation. To remove an alias, use the unalias command.

How do I check the value of an alias?

To view the alias for a particular name, enter the command alias followed by the name of the alias. Most Linux distributions define at least some aliases. Enter an alias command to see which aliases are in effect. You can delete the aliases you do not want from the appropriate startup file.

Where is alias defined?

Definition of alias : otherwise called : otherwise known as —used to indicate an additional name that a person (such as a criminal) sometimes uses John Smith alias Richard Jones was identified as the suspect.


2 Answers

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.

like image 101
Jonathan Callen Avatar answered Sep 16 '22 20:09

Jonathan Callen


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.

like image 37
jman Avatar answered Sep 16 '22 20:09

jman