0

Is there any way to alias the options of a command? For instance, I often use the options -mindepth and -maxdepth with find, but these are quite long at 9 characters apiece, and don't have corresponding short forms. I would like to alias them to something faster to type like -mn and -mx. Of course, one can always build a custom binary with new option names, but I am interested in shell built-ins/utilities that provide this behavior (analogously to how alias allows commands to be aliased).

  • A function wrapper that parses options (carefully), I suppose. Though I'd warn you away from this, just like I don't rely on `alias rm="rm -i". – Jeff Schaller Aug 14 at 18:52
0

Write a shell function or script. The only issue is how much you care about corner cases. If you don't care at all then

 #!/bin/bash
 args=()
 while (($# > 0))
 do
      case "$1" in
      ("-mn") args=("${args[@]}" "-mindepth") ;;
      ("-mx") args=("${args[@]}" "-maxdepth") ;;
      (*) args=("${args[@]}" "$1") ;;
      esac
      shift
 done
 exec find "${args[@]}"

If you care a little more then use

      ("-mn") args=("${args[@]}" "-mindepth" "$2") ; shift ;;

and add similar cases for all the other find options which take a parameter so you don't convert

  find / -name -mn -print

into

  find / -name -mindepth -print

Don't call this script find, perhaps fnd as it takes shorter options than find, or myfind. It is possible to make it work if you call it find, but as mentioned in the comments you don't what to alter the behavior of standard programs.

  • 1
    To append to an array, you can use args+=('-mindepth'). – Benjamin W. Aug 14 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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