Linux get path to bin

How to find the installation path for a software under linux?

Under linux, I launch a software by typing, e.g., fluidplot. How can I find the installation path for this software?

9 Answers 9

to see where it is executing from (if it’s in your $PATH). Or:

find / -name fluidpoint 2> /dev/null 

to look for a file named fluipoint and redirect errors on virtual filesystems.

Usually they are in /sbin , /usr/sbin , /usr/local/bin or ~ as a hidden directory.

NAME which - shows the full path of (shell) commands. SYNOPSIS which [options] [--] programname [. ] 

@Michael excellent to know. Because of your comment, I just discovered that newer versions of bash also do this. +1 to your comment.

The «Usually they are . » line is pretty disingenuous, additional software should be in /opt/* or /usr/local/bin . ~ is your home directory, I’m confused why you call it «hidden».

Sorry to be ambiguous, I mean ~/.dir . The hidden directory is below the home directory. And I completely forgot about /usr/local/bin dop.

If you use an RPM based distribution (CentOS, RHEL, SUSE, openSUSE) you can use rpm -ql

rpm -ql findutils /bin/find /usr/bin/find /usr/bin/xargs /usr/share/doc/packages/findutils /usr/share/doc/packages/findutils/AUTHORS /usr/share/doc/packages/findutils/COPYING /usr/share/doc/packages/findutils/NEWS /usr/share/doc/packages/findutils/README /usr/share/doc/packages/findutils/THANKS /usr/share/doc/packages/findutils/TODO /usr/share/info/find.info.gz /usr/share/man/man1/find.1.gz 

Things aren’t installed to locations in the Linux/UNIX world like they are in the Windows (and even somewhat in the Mac) world. They are more distributed. Binaries are in /bin or /sbin , libraries are in /lib , icons/graphics/docs are in /share, configuration is in /etc and program data is in /var .

The /bin , /lib , /sbin contain the core applications needed for booting and the /usr contains all the other user and system applications.

Just to add some point to @djsumdog’s answer, if you are using DPKG based dist, like Ubuntu, you can use

to check what it is about, and

dpkg --listfiles some_package 

to check what files are included/relevant to this package. It’s for packages that don’t have a binary to run, like libnss3 . And

to find what package includes this file.

For example, dpkg —listfiles libnss3 gives me:

/. /usr /usr/lib /usr/lib/i386-linux-gnu /usr/lib/i386-linux-gnu/libssl3.so /usr/lib/i386-linux-gnu/nss /usr/lib/i386-linux-gnu/nss/libsoftokn3.chk /usr/lib/i386-linux-gnu/nss/libnssckbi.so /usr/lib/i386-linux-gnu/nss/libnsssysinit.so /usr/lib/i386-linux-gnu/nss/libfreebl3.chk /usr/lib/i386-linux-gnu/nss/libnssdbm3.chk /usr/lib/i386-linux-gnu/nss/libnssdbm3.so /usr/lib/i386-linux-gnu/nss/libsoftokn3.so /usr/lib/i386-linux-gnu/nss/libfreebl3.so /usr/lib/i386-linux-gnu/libnssutil3.so /usr/lib/i386-linux-gnu/libsmime3.so /usr/lib/i386-linux-gnu/libnss3.so /usr/share /usr/share/doc /usr/share/doc/libnss3 /usr/share/doc/libnss3/copyright /usr/share/doc/libnss3/changelog.Debian.gz /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/libnss3 

Note that the folders are not only owned by this packages, but by others too. Just check the files.

And reversely, dpkg —search libnss3.so gives me:

firefox: /usr/lib/firefox/libnss3.so thunderbird: /usr/lib/thunderbird/libnss3.so libnss3:i386: /usr/lib/i386-linux-gnu/libnss3.so libnss3-1d:i386: /usr/lib/i386-linux-gnu/libnss3.so.1d 

Источник

Читайте также:  Linux command which ports are in use

Find Path of a Command with whereis

whereis is used to find the path of the Linux binary /executable files, source files and man page files.There are many Linux distributions. Not every Linux distribution keeps the binary/executable files, source files and man page files in the same location. So, to find out the path of these files when needed, the whereis command is used. It is a very useful command for every Linux system administrator out there.

In this article, I am going to show you how to use the whereis command on Linux. So, let’s get started.

Basic Usage of the whereis Command:

You can find the full path of any standard Linux command that is in the PATH of your Linux operating system.

NOTE: The PATH contains the directories where the standard Linux commands and other commands are installed. You can run the following command to print the PATH on your console.

Let’s say, you want to know the full path of the Linux command passwd. To do that, run the whereis command as follows:

As you can see, the full path of the passwd command is /usr/bin/passwd.

The whereis command without any option prints the full path of the command, the full path of the configuration file that matches the query, and the full path of the matched man page files.

Printing the Full Path of Only Binaries/Executables:

If you only want to print the full path of specific Linux commands and configuration files, then you can do so with the -b option of whereis.

Let’s say, you want to print only the full path of the command ls, then run the whereis command as follows:

As you can see, the full path of the command ls is /bin/ls.

If there is a matching configuration file, it should be printed as well. For example, when you try to find the full path of the command passwd, you should also get the full path of the passwd configuration file as you can see in the screenshot below.

Printing the Full Path of Only Man Pages:

If you want to print the full path of only man pages on your computer, then you can use the -m option of the whereis command.

For example, let’s say, you want to find out the full path of all man page files of the ls command. To do that, run the whereis command as follows:

As you can see, the man page of the ls command is in the path /usr/share/man/man1/ls.1.gz.

The same way, you can find the full path of all the man pages of the useradd command.

Finding the Full Path of Linux Headers or Kernel Source Files Only:

You can use the -s option of the whereis command to print the full path of the Linux headers or kernel source files used to develop system software.

Читайте также:  Создать бэкап системы linux

For example, to find the full path of the source file net, you can use the whereis command as follows:

As you can see, there are two locations where the net source header file is kept on my Ubuntu 18.04 LTS, /usr/src/linux-headers-4.15.0-20-generic/net and /usr/src/linux-headers-4.15.0-20/net.

The same way, you can search for the fs source file with the whereis command as follows.

Printing the Directories the whereis Command Searched:

By default, the whereis command searches a lot of system directories. You can use the -l option to tell whereis to print the directories it searched.

As you can see, the whereis command searched a lot of directories by default.

Adding a New Search Directory or Directories:

You saw earlier that the whereis command searches a lot of directories by default. But if you have some directories that you want whereis to search, then you can use the -B, -M or -S option of the whereis command to specify binary/executable, manual or man pages, source or kernel header directories respectively.

Let’s say, you have some binary files in the ~/bin directory that you want to include in the whereis search. To do that, you can run the whereis command as follows:

NOTE: Whenever you use either the -B, -M or the -S option, you must also use the -f option as shown above. Otherwise, you will get an error.

As you can see, the full path of the CreateDirectory command is printed. So, it worked. We’ve successfully added a new binary directory to the whereis search path.

You can also add more than one search directories if you want.

For example, let’s say, you want to add the ~/bin and ~/bin2 binary directories to the whereis search path. To do that, run the whereis command as follows:

The same way, you can add manual or man page directories to the whereis search path as follows:

The same way, for the source or kernel headers, you can use the -S option to add addition search directories as follows:

You can also mix the -B, -M and -S options as required to add the required search path for the binary, manual or source files respectively.

So, that’s how you use the whereis command to find the path of commands/binaries/executables, man pages or manuals, and source files in Linux. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.

Читайте также:  Linux start shell script

Источник

Path to binary in C

How can I get the path where the binary that is executing resides in a C program? I’m looking for something similar to __FILE__ in ruby/perl/PHP (but of course, the __FILE__ macro in C is determined at compile time). dirname(argv[0]) will give me what I want in all cases unless the binary is in the user’s $PATH . then I do not get the information I want at all, but rather «» or «.»

If you just need to cover windows and linux, you could always use ifdefs to deal with the portability problems.

9 Answers 9

Totally non-portable Linux solution:

This uses the «/proc/self» trick, which points to the process that is running. That way it saves faffing about looking up the PID. Error handling left as an exercise to the wary.

The non-portable Windows solution:

WCHAR path[MAX_PATH]; GetModuleFileName(NULL, path, ARRAYSIZE(path)); 

Here’s an example that might be helpful for Linux systems:

/* * getexename - Get the filename of the currently running executable * * The getexename() function copies an absolute filename of the currently * running executable to the array pointed to by buf, which is of length size. * * If the filename would require a buffer longer than size elements, NULL is * returned, and errno is set to ERANGE; an application should check for this * error, and allocate a larger buffer if necessary. * * Return value: * NULL on failure, with errno set accordingly, and buf on success. The * contents of the array pointed to by buf is undefined on error. * * Notes: * This function is tested on Linux only. It relies on information supplied by * the /proc file system. * The returned filename points to the final executable loaded by the execve() * system call. In the case of scripts, the filename points to the script * handler, not to the script. * The filename returned points to the actual exectuable and not a symlink. * */ char* getexename(char* buf, size_t size) < char linkname[64]; /* /proc//exe */ pid_t pid; int ret; /* Get our PID and build the name of the link in /proc */ pid = getpid(); if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0) < /* This should only happen on large word systems. I'm not sure what the proper response is here. Since it really is an assert-like condition, aborting the program seems to be in order. */ abort(); >/* Now read the symbolic link */ ret = readlink(linkname, buf, size); /* In case of an error, leave the handling up to the caller */ if (ret == -1) return NULL; /* Report insufficient buffer size */ if (ret >= size) < errno = ERANGE; return NULL; >/* Ensure proper NUL termination */ buf[ret] = 0; return buf; > 

Essentially, you use getpid() to find your PID, then figure out where the symbolic link at /proc//exe points to.

Источник

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