The which function returns the full path to some other function, if I'm not mistaken.

$ which ls # => /bin/ls

But if I wanted to the directory the function is in, how would I do that? I'm thinking of doing something like this:

$ cd $(which ls)

Obviously, that won't work because ls isn't a directory. Perhaps I'm barking up the wrong tree with which?


This will do the job:

$ cd $(dirname $(which ls))

From man dirname:

dirname - strip last component from file name

  • Solved. That's exactly what I was looking for. Do you have a concise resource that lists available commands? Thanks! – Mike Lane Jul 22 '15 at 17:48
  • I don't know what do you mean by all commands. There are some commands that are part of the shell called shell builtins, in case of Bash you can read about them in man bash or by typing help <COMMAD>, for example help cd. However, there are many shell and not all of them have the same set of builtin commands. OTOH, there are command line external programs that are not part of an external shell. It depends on your system what these programs are. – Arkadiusz Drabczyk Jul 23 '15 at 9:20
  • My issue is that, for example, I didn't know dirname existed, so I didn't know to even look it up. I'm familiar with the concept of builtins and using man, but it's not terribly helpful unless I know what to look for. So the follow up was, other than going on stackexchange to ask questions, where would one find some listing of builtins in a particular shell? From there, I'm happy to use man (or bro) to learn more about them. – Mike Lane Jul 23 '15 at 14:59
  • But dirname is not a builtin, it's a separate program. In bash you can list all builtins using help. You can use man -k to look for programs for a specified task locally. Apart from that, reading and writing shell scripts is the only way to learn new commands. – Arkadiusz Drabczyk Jul 23 '15 at 15:04

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