Как узнать путь до исполняемого файла в Linux?
Как в Linux узнать путь до исполняемого файла? В виндовс, например, на ярлыке или в bin/app.exe можно через свойство посмотреть Интересуюсь с целью создавать *.desktop ярлыки для заполнениями ими рабочего стола 🗔, так как способ «добавить в избранное» мне не подходит и получается рабочий стол пустой 😔
Для создания *.desktop нет необходимости указывать полный путь. Посмотрите примеры в /usr/share/applications
3 ответа 3
Я по факту могу найти приложение в диспетчере приложений, можно ли как-то оттуда взять этот путь? Или лучше и правильнее поискать путь к .exe в папках типа как в Windows —> C/Program FIles/app_folder/bin/app.exe ?
Внизу слева есть кнопка «Показать приложения», я про это имел в виду, оттуда бы как-то брать пути, там же ярлыки и их на рабочий стол хочу закидывать : )
Если вы знаете имя исполняемого файла, который выполняется в текущий момент, то полный путь к исполняемому можно узнать вот так:
pgrep | while read pid; do echo -ne "$pid\t"; readlink -f /proc/$pid/exe; done
Например, вывод для исполняемого процесса gopls (сервер go для VS Code):
pgrep gopls | while read pid; do echo -ne "$pid\t"; readlink -f /proc/$pid/exe; done 30880 /mnt/drive2/home2/user/go/bin/gopls
Набрав в командной строке
whereis locates the binary, source and manual files for the specified command names. The supplied names are first stripped of leading pathname components and any (single) trailing extension of the form .ext (for example: .c) Prefixes of s. resulting from use of source code control are also dealt with. whereis then attempts to locate the desired program in the standard Linux places, and in the places specified by $PATH and $MANPATH.
Get path to application?
How can a user determine the path to an application being showed in launcher? This is meant for non-programmers or experts, so terminal commands are not really suited for them. Why: we need to make our users able to drag files/folders on our applications, a standard OS feature entirely missing in Ubuntu.
You ask for answers for non-programmers or experts. Pilot6’s answer is pretty close to what you want — typically .desktop files for each app are located in /usr/share/applications but not always. Would you consider a shell script that I can write and show how to set up , or you do not want to deal with command-line at all ? The script can determine each file’s location and exact location of the executable binary
3 Answers 3
Go to /usr/share/applications or ~/.local/share/applications , right-click the application icon and click on «Properties».
I do not see a reason why a person who can’t run a command in terminal would need a path.
Upvoted, but I agree with Byte Commander — you should mention other possible locations. For instance, Microchip’s MPLAB IDE installs all of its files into /opt directory
thanks, the reason for no terminal: being able to drag files/folders on a application, a standard OS feature totally absent in Ubuntu.
The reason why people need paths is that even when a developer goes through the trouble to make a GUI they will still put a text field in there asking users to provide e.g. «the path to Google Chrome». It sucks, but it’s still infinitely better than a terminal.
In 18.04 and later launch an application by clicking on its icon in the Dash which is accessed by clicking the 9 dots icon in the lower left corner of the dock. Launch the System Monitor and make a note of the exact spelling of the name of the application that you just opened. Open the terminal and type:
type application-name
It takes only a few seconds to get the path to an application in Ubuntu 14.04 and later. Search for the application in the Dash and then drag the application’s icon into the terminal. The application’s full path will be shown in the terminal automatically. Dragging the icon into the terminal will also show the path to any file, folder, archive or anything else that has an icon.
In Ubuntu 20.04 and later drag and drop of files or directories doesn’t work from the desktop, but does work in other locations including dragging from the desktop in Files file manager.
How to find application’s path from command line?
For example, I have git installed on my system. But I don’t remember where I installed it, so which command is fit to find this out?
Just in case, command -v and which worked in Linux Alpine 3.16.2 (Docker image). whereis and locate did not — not installed.
5 Answers 5
If it is in your path, then you can run either type git or which git . The which command has had problems getting the proper path (confusion between environment and dot files). For type , you can get just the path with the -p argument.
If it is not in your path, then it’s best to look for it with locate -b git It will find anything named ‘git’. It’ll be a long list, so might be good to qualify it with locate -b git | fgrep -w bin .
I use locate endlessly (it is very fast), but for those unaware of it, locate is only as up to date as its most recent database update, which is automatically run daily on my Ubuntu. The refresh command is sudo updatedb . Also locate has built-in regex capability, so commands like this works: locate -br «^git$» . -b` means restrict the search to just the basename . or without the -b , it searches the full pathname .. Also, it only searches paths you have configured it to search.. there is no command-line control of this other than your regex filters.
@Gilles, that’s funny for me the behavior is exactly the opposite: type is a shell builtin that tells me aliases and such, and which is an external program that shows me the path to an executable. although if there’s a builtin that gets in the way that executable won’t get called.
@quodlibetor The problems with which are that it doesn’t know about shell built-ins and functions (which is relevant when you’re wondering what typing the command will do), and it uses a different $PATH on some systems.
How do I get the path of a process in Unix / Linux?
In a Windows environment there is an API to obtain the path which is running a process. Is there something similar in Unix / Linux? Or is there some other way to do that in these environments?
11 Answers 11
On Linux, the symlink /proc//exe has the path of the executable. Use the command readlink -f /proc//exe to get the value.
On AIX, this file does not exist. You could compare cksum and cksum /proc//object/a.out .
You can find the exe easily by these ways, just try it yourself.
This is awesome. I knew I ran it from a location which had the symbolic link to the original executable (one of the many versions). pwdx
Last two (pwdx and lsof) may not give you the correct result. The question was about full path to the executable. pwdx and lsof will give you cwd of the process rather than the path to the process. I think the answer of jpalecek is more accurate as the original requestor asked for the path to the executable rather than soft link describing the executable.
This is really useful, however for the last one I seem to need to use lsof -p
All the answers were specific to Linux.
If you also need Unix, then you need this:
char * getExecPath (char * path,size_t dest_len, char * argv0) < char * baseName = NULL; char * systemPath = NULL; char * candidateDir = NULL; /* the easiest case: we are on Linux */ size_t buff_len; if (buff_len = readlink ("/proc/self/exe", path, dest_len - 1) != -1) < path [buff_len] = '\0'; dirname (path); strcat (path, "/"); return path; >/* Ups. not on Linux, no guarantee */ /* check if we have something like execve("foobar", NULL, NULL) */ if (argv0 == NULL) < /* We surrender and give the current path instead */ if (getcwd (path, dest_len) == NULL) return NULL; strcat (path, "/"); return path; >/* argv[0] */ /* if dest_len < PATH_MAX may cause buffer overflow */ if ((realpath (argv0, path)) && (!access (path, F_OK))) < dirname (path); strcat (path, "/"); return path; >/* Current path */ baseName = basename (argv0); if (getcwd (path, dest_len - strlen (baseName) - 1) == NULL) return NULL; strcat (path, "/"); strcat (path, baseName); if (access (path, F_OK) == 0) < dirname (path); strcat (path, "/"); return path; >/* Try the PATH. */ systemPath = getenv ("PATH"); if (systemPath != NULL) < dest_len--; systemPath = strdup (systemPath); for (candidateDir = strtok (systemPath, ":"); candidateDir != NULL; candidateDir = strtok (NULL, ":")) < strncpy (path, candidateDir, dest_len); strncat (path, "/", dest_len); strncat (path, baseName, dest_len); if (access(path, F_OK) == 0) < free (systemPath); dirname (path); strcat (path, "/"); return path; >> free(systemPath); dest_len++; > /* Again, someone has to use execve: we don’t know the executable name; we surrender and instead give the current path */ if (getcwd (path, dest_len - 1) == NULL) return NULL; strcat (path, "/"); return path; >