1

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?

0

1 Answer 1

1

This will do the job:

$ cd $(dirname $(which ls))

From man dirname:

dirname - strip last component from file name

4
  • Solved. That's exactly what I was looking for. Do you have a concise resource that lists available commands? Thanks! Commented Jul 22, 2015 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. Commented Jul 23, 2015 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. Commented Jul 23, 2015 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. Commented Jul 23, 2015 at 15:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.