Linux get command path

How to get full path name of a Linux command

I want to find out the file path of commands in Linux e.g., ls has file path as /bin/ls . How can I find out the exact path of some commands?

5 Answers 5

As pointed out, which would do it. You could also try:

This will list all the paths that contains progName . I.e whereis -b gcc on my machine returns:

gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc 

On my machine I have /bin/gcc and /usr/bin/gcc (both of which are links which after many links point to the actual program which is /usr/bin/x86_64-linux-gnu-gcc-9 ), but the former isn’t listed in whereis gcc .

you can use which , it give you path of command:

Well done with the use of type which is generally a builtin, and if not provided by POSIX. type -p (where that option is available) will return a pruned result which can be assigned and used as a command. e.g. myprog=$(type -p someexename) .

You can use the which command. In case of a command in your $PATH it will show you the full path:

mureinik@computer ~ $ which cp /usr/bin/cp 

And it will also show details about aliases:

mureinik@computer ~ $ which ls alias ls='ls --color=auto' /usr/bin/ls 

Yes you can find it with which command

You did not specify, which shell you are going to use, but I strongly recommend using which , as it does not necessarily do, what you expect. Here two examples, where the result possibly is not what you expect:

(1) Example with bash, and the command echo :

would output /usr/bin/echo , but if you use the echo command in your bash script, /usr/bin/echo is not executed. Instead, the builtin command echo is executed, which is similar, but not identical in behaviour.

Читайте также:  Линукс пинг большими пакетами

(2) Example with zsh, and the command which :

would output the message which: shell built-in command (which is correct, but certainly not a file path, as you requested), while

would output the file path /usr/bin/which , but (as in the bash example) this is not what’s getting executed when you just type which .

There are cases, when you know for sure (because you know your application), that which will produce the right result, but beware that as soon as builtin-commands, aliases and shell functions are involved, you need first to decide how you want to handle those cases, and then choose the appropriate tools depending on the kind of shell which you are using.

Источник

How to Get the Path of a Linux Command?

A Linux command that you run from a terminal window can be built-in (a part of the system), a function (an application that runs when you enter certain commands), an alias (another name for a command), or an external executable (a program that you download). You can use the which, command, whereis, whatis, and type tools to determine what each one is and where they are located.

We will look at the which, command, types, and whereis commands as they are usually found in most Linux-Based OSs.

So let’s explore how to get the path of a Linux command in this article.

PATH Environment Variable

Before we get into the details of the utilities, let us first understand that the applications, such as our shell (the program that runs when we type commands), searches for the commands in a list of folders that are stored in an environmental variable called PATH. Each folder is separated by a colon «:» symbol.

We can see what is inside this variable using the echo function.

Command

Output

When we install an executable file on our computer, so that we can run it from anywhere, we must ensure that the PATH environment variables include the location of the executable.

We can temporarily change the PATH variable by typing this command

Command

$ export PATH=$PATH:/newdir/path $ echo $PATH

Output

/sbin:/bin:/usr/sbin:/usr/bin:/newdir/path

The above created PATH will be reset when you restart your computer.

We can change the PATH environment variables permanently by editing the.bashrc file.

export PATH=$PATH:/newdir/path

which Command

Most Linux-powered operating system distributions include the which utility. We can use this tool to determine the location of a Linux program.

Читайте также:  Linux crt1 o undefined reference to main

Command

Output

When we type the echo command into our terminal window, it will execute the echo executable file located at /bin/ directory.

Furthermore, the which command has an option -a which will print out all matching paths −

Command

Output

/usr/bin/php /usr/bin/php /bin/php

We have two executables in two different directories. One is located in the /usr/bin/, and the other is located in the /opt/php8/. The former is used by default because its location appears in the PATH environment variable, and the latter has the correct permissions.

whereis Command

Let’s take a quick look at the whereis (where is) utility. This tool finds out where a program is located.

If we call the utilities directly, they show us all the locations for the binary, source code, and man pages −

Command

Output

php: /usr/bin/php /usr/lib64/php /etc/php.ini /etc/php.d /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

We can use the -b parameter to display just the binaries.

Command

Output

php: /usr/bin/php /usr/lib64/php /etc/php.ini /etc/php.d /usr/include/php /usr/share/php

If we want to display just the manual use -m parameter −

Command

Output

php: /usr/share/man/man1/php.1.gz

type Command

Type commands can not only show the paths of Linux commands, but they can also determine whether the targets are internal, functions, aliases, or external executables.

Let’s use type command with -p parameter to see the path of supplied Linux Command −

Command

Output

If you don’t include the parameter, it will display the command definitions.

Command

Output

If we use the -a option, it shows the command description, executable type, and the full file name −

Command

Output

echo is a shell builtin echo is /usr/bin/echo echo is /usr/bin/echo echo is /bin/echo

We can also use the command type -t to show the executable type.

Command

Output

The above command has output builitin, as echo is type of builtin file.

Command

Output

Similarly above, the ls command is an alias file, and below command shows php command is just a file (executable file).

Читайте также:  Gigabyte uefi bios linux

Command

Output

command Command

Another useful tool for finding the location of a Linux command is the command command.

This tool lets us know whether we’re dealing with an executable file, or an alias command.

The command having two option “-v” and “-V”, where “-v” gives output in just as a result and “-V” option provides output in sentence format.

Command

Output

Command

Источник

In linux, how to get the full path of a command I am using?

How to get the full path of a command I am using? for example, I have installed jdk into /opt/Oracle/jdk1.7.0_25 , and my PATH looks like this:

/opt/Oracle/jdk1.7.0_25/bin/:/usr/lib/x86_64-linux-gnu/qt4/bin:/home/unimous/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games. 

4 Answers 4

should give you the complete path.

This also works for any other command in Linux, just use which command_name .

It depends on your distribution, in debian and debian-based distros you have update-alternatives

And for your java you do for instance:

update-alternatives —install «/usr/bin/java» «java» «/opt/Oracle/jdk1.7.0_25/bin/java» 3

Help section on update-alternatives update-alternatives: --install needs    Usage: update-alternatives [ . ] Commands: --install    [--slave  ] . add a group of alternatives to the system. --remove  remove from the group alternative. --remove-all remove group from the alternatives system. --auto switch the master link to automatic mode. --display display information about the group. --query machine parseable version of --display . --list display all targets of the group. --get-selections list master alternative names and their status. --set-selections read alternative status from standard input. --config show alternatives for the group and ask the user to select which one to use. --set  set as alternative for . --all call --config on all alternatives. is the symlink pointing to /etc/alternatives/. (e.g. /usr/bin/pager) is the master name for this link group. (e.g. pager) is the location of one of the alternative target files. (e.g. /usr/bin/less) is an integer; options with higher numbers have higher priority in automatic mode. Options: --altdir change the alternatives directory. --admindir change the administrative directory. --log change the log file. --force allow replacing files with alternative links. --skip-auto skip prompt for alternatives correctly configured in automatic mode (relevant for --config only) --verbose verbose operation, more output. --quiet quiet operation, minimal output. --help show this help message. --version show the version. 

Источник

Оцените статью
Adblock
detector