Find the path of a shell command

Jon Ribbens jon+usenet at unequivocal.eu
Thu Oct 13 07:26:14 EDT 2022


On 2022-10-12, Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
> Às 22:38 de 12/10/22, Jon Ribbens escreveu:
>> On 2022-10-12, Jon Ribbens <jon+usenet at unequivocal.eu> wrote:
>>> On 2022-10-12, Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>>>> Às 19:14 de 12/10/22, Jon Ribbens escreveu:
>>>>> On 2022-10-12, Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>>>>>> Às 05:00 de 12/10/22, Paulo da Silva escreveu:
>>>>>>> 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?
>>>>>>>
>>>>>> Thank you all who have responded so far.
>>>>>> I think that the the suggestion of searching the PATH env seems the best.
>>>>>> Another thing that I thought of is that of the 'which', but, to avoid
>>>>>> the mentioned recurrent problem of not knowing where 'which' is I would
>>>>>> use 'type' instead. 'type' is a bash (sh?) command.
>>>>>
>>>>> If you're using subprocess.run / subprocess.Popen then the computer is
>>>>> *already* searching PATH for you.
>>>> Yes, and it works out of cron.
>>>>> Your problem must be that your cron
>>>>> job is being run without PATH being set, perhaps you just need to edit
>>>>> your crontab to set PATH to something sensible.
>>>> I could do that, but I am using /etc/cron.* for convenience.
>>>>
>>>>> Or just hard-code your
>>>>> program to run '/bin/rm' explicitly, which should always work (unless
>>>>> you're on Windows, of course!)
>>>> It can also be in /bin, at least.
>>>
>>> I assume you mean /usr/bin. But it doesn't matter. As already
>>> discussed, even if 'rm' is in /usr/bin, it will be in /bin as well
>>> (or /usr/bin and /bin will be symlinks to the same place).
>>>
>>>> A short idea is to just check /bin/rm and /usr/bin/rm, but I prefer
>>>> searching thru PATH env. It only needs to do that once.
>>>
>>> I cannot think of any situation in which that will help you. But if for
>>> some reason you really want to do that, you can use the shutil.which()
>>> function from the standard library:
>>>
>>>      >>> import shutil
>>>      >>> shutil.which('rm')
>>>      '/usr/bin/rm'
>> 
>> Actually if I'm mentioning shutil I should probably mention
>> shutil.rmtree() as well, which does the same as 'rm -r', without
>> needing to find or run any other executables.
> Except that you can't have parallel tasks, at least in an easy way.
> Using Popen I just launch rm's and end the script.

    [threading.Thread(target=shutil.rmtree, args=(item,)).start()
        for item in items_to_delete]



More information about the Python-list mailing list