Using an alias in find -exec

前端 未结 7 677
醉酒成梦
醉酒成梦 2021-01-07 19:11

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

相关标签:
7条回答
  • 2021-01-07 19:53

    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
    
    0 讨论(0)
  • 2021-01-07 19:54

    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.

    0 讨论(0)
  • 2021-01-07 19:57

    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.

    0 讨论(0)
  • 2021-01-07 19:57

    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 {}" \;
    
    0 讨论(0)
  • 2021-01-07 20:00

    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 讨论(0)
  • 2021-01-07 20:03

    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.

    0 讨论(0)
提交回复
热议问题