When a command is executed with 'sudo' the metacharacters except the $ are escaped. This causes that command like the following that we think should work correctly gives unexpected problems:

sudo ls -t /etc/letsencrypt/archive/domain.org/cert*.pem | head -n 1


This command generates the error message:

ls: cannot access '/etc/letsencrypt/archive/domain.org/cert*.pem': No such file or directory


And this is because the * by \* has escaped, and there are no files with the \ character in the specified path.

The solution is to run the embedded command using a 'sh -c', example:

sudo sh -c 'ls -t /etc/letsencrypt/archive/domain.org/cert*.pem | head -n 1'


Which returns the last modified file in that directory named cert * .pem, in this case:

/etc/letsencrypt/archive/domain.org/cert27.pem

Doing it embedded through 'sh -c' does not read from the 'standard input', and read it as a command with its parameters, without escaping it. In this example it works even with a pipe.