Using rm * (wildcard) in the messenger: no such file or directory

I use Python and Envoy. I need to delete all files in a directory. In addition to some files, the directory is empty. In the terminal, it will be:

rm /tmp/my_silly_directory/* 

Common sense dictates that in the messenger this means:

 r = envoy.run('rm /tmp/my_silly_directory/*') 

However:

 r.std_err -> "rm: cannot remove `/tmp/my_silly_directory/*': No such file or directory" 

Naturally, there are alternatives to using the messenger in this case, I just wonder why this does not work.

Any clues?

+6
source share
1 answer

On UNIX, it is pre-shell to interpret wildcards such as * . If you execute the program and pass the argument with * in it directly to the program - this is probably what is done here - then you will get an error, as you see. rm simply assumes that * is the name of the file, and indeed it is possible to create such a file.

One solution might be to execute the shell and let it execute your command on your behalf, something like

 r = envoy.run('sh -c "rm /tmp/my_silly_directory/*"') 

The shell interprets * before calling rm .

+5
source

Source: https://habr.com/ru/post/921674/


All Articles