This question already has an answer here:

I am trying to create an alias which when given some argument will look for the folder with contains the argument as pattern. Let the alias name be gohf.

Now, I will be using this alias like gohf <some-pattern>, for eg. gohf testing. Now this should look for a folder that contains “testing” in its name and take me to that folder in the last step.

Following is what I could think of.

alias gohf='cd `find . -type d | grep testing`';

The above alias works when the pattern is always “testing”. How can I modify so that if the alias is used as say gohf game, then the alias should take me to the folder that contains game in its name.


Shell: ksh

I am assuming that there is only one folder for each pattern I input.

marked as duplicate by Mikel, slm Sep 13 '14 at 1:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    See if you can get an answer from Is it possible to use a parameter within an alias command. Or shorter: no way, that is out of aliases' scope. Use a function instead. – manatwork Jan 23 '13 at 15:18
  • 1
    Any particular reason to not use a function instead? gohf() { cd ´find . -type d | grep $1´ } Excuse me for using the wrong " ` " – Martín Canaval Jan 23 '13 at 15:27
  • piping find to grep is bad practice. the -name or -iname flags do the same thing and can handle regex as well – h3rrmiller Jan 23 '13 at 15:37
  • @MartínCanaval: If I use gohf() { cd ´find . -type d | grep $1´ } ...I copied this function to my .profile...but I get the gohf{: command not found – g4ur4v Jan 23 '13 at 15:39
  • did you make sure the .profile file was sourced? IE, did you start a fresh session on a new TTY or logout and log back in? Or safer to add it to .kshrc and do a . ./kshrc, just to test it. Also, try with what the guy down there suggested in his answer. – Martín Canaval Jan 23 '13 at 15:45

As manatwork said in the comment you should use a function instead to handle arguments better.

  cd $(find . -type d -iname "$1" | sed 1q)

There is no need to pipe the results of find to grep as using the flag -name or -iname does the same thing. Then we pipe the whole thing to sed so that we only cd to the first "hit" if there are multiple.

In the case that you want to be able to handle multiple find "hits" you should do something like this:

  select dir in $(find . -name "$1" -type d)
    cd $dir
  • How do I use this function in my shell.when I copy this to .profile,I get this error gohf{: command not found – g4ur4v Jan 23 '13 at 15:42
  • @g4ur4v because your .profile is read once when the shell is executed you just need to exec ksh to get it to read the new copy by executing a new shell without forking the process – h3rrmiller Jan 23 '13 at 15:43
  • I am getting that error after logging into the account after making changes to .profile.Even if I use exec ksh,I get the same error. – g4ur4v Jan 23 '13 at 15:46
  • Please ignore ...I made some manual mistake. – g4ur4v Jan 23 '13 at 15:48
  • Not "better", aliases don't take arguments at all. – vonbrand Jan 23 '13 at 20:39

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