Find the path of a shell command
jak
nospam at please.ty
Wed Oct 12 17:14:46 EDT 2022
Il 12/10/2022 20:19, MRAB ha scritto:
> On 2022-10-12 06:11, jak wrote:
>> Il 12/10/2022 06:00, Paulo da Silva ha scritto:
>>> Hi!
>>>
>>> The simple question: How do I find the full path of a shell command
>>> (linux), i.e. how do I obtain the corresponding of, for example,
>>> "type rm" in command line?
>>>
>>> The reason:
>>> I have python program that launches a detached rm. It works pretty
>>> well until it is invoked by cron! I suspect that for cron we need to
>>> specify the full path.
>>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
>>> What about other commands?
>>>
>>> Thanks for any comments/responses.
>>> Paulo
>>>
>>
>> I'm afraid you will have to look for the command in every path listed in
>> the PATH environment variable.
>>
> Isn't that what the "whereis" command does?
"whereis" does more than the OP wants. A suitable command could be
"which" but if the OP is in a context where he needs to know the path
of the command, then it will have the same problem with "whereis" or
"which". In any case they would be a few lines of Python:
import os
def which(c):
for p in os.getenv('PATH').split(os.path.pathsep):
if p and os.path.isfile(f := os.path.join(p, c)):
return f
return ''
cmd = 'explorer.exe'
cmdp = which(cmd)
if cmdp:
print("found:", cmdp)
else:
print("not found"
More information about the Python-list
mailing list