Find the path of a shell command

jak nospam at please.ty
Wed Oct 12 08:46:56 EDT 2022


Il 12/10/2022 14:20, Chris Green ha scritto:
> jak <nospam at please.ty> wrote:
>> Il 12/10/2022 09:40, jkn ha scritto:
>>> On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, 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.
>>>
>>> erm, or try 'which rm' ?
>>
>> You might but if you don't know where the 'rm' command is, you will have
>> the same difficulty in using 'which' command. Do not you think?
>>
>  From a command prompt use the bash built-in 'command' :-
> 
>      command -v rm
> 
> ... and rm will just about always be in /usr/bin.
> 

ok but I didn't suggest a very complicated thing:

for p in os.getenv('PATH').split(os.path.pathsep):
     if p:
         if os.path.isfile(os.path.join(p, 'rm')):
             print(f)



More information about the Python-list mailing list