Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rm * (wildcard) in envoy: No such file or directory

I'm using Python and Envoy. I need to delete all files in a directory. Apart from some files, the directory is empty. In a terminal this would be:

rm /tmp/my_silly_directory/*

Common sense dictates that in envoy, this translates into:

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 envoy in this case, I am simply wondering why it doesn't work.

Any clues?

like image 561
Jouke Waleson Avatar asked Jul 30 '12 13:07

Jouke Waleson


1 Answers

On UNIX, it's up to the shell to interpret wildcards like *. If you execute a program and pass an argument with * in it directly to the program -- which is presumably what's being done here -- then you'll get an error like you're seeing. rm just assumes the * is a file name, and indeed, it's actually possible to create such a file.

One solution could 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 will interpret the * before invoking rm.

like image 186
Ernest Friedman-Hill Avatar answered Oct 03 '22 18:10

Ernest Friedman-Hill