Perform ls command with * in name
I would like to perform an ls command on a file named like /var/local/tmp/foo*. I have been trying to use
%x[ls "#{path]"]
but this returns:
ls: /var/local/tmp/foo*: No such file or directory
Note that %x[ls '/var/local/tmp/foo*'] also returns above error.
If I do the same command, but as follows: 开发者_如何学JAVA
%x[ls /var/local/tmp/foo*]
it works correctly.
I am guessing that putting the directory path into a string is causing ls to treat it as a file literally named "/var/local/tmp/foo*".
Any ideas how to get around this? I would like to use a variable that holds this string, so just putting in /var/local/tmp/foo* directly is not an option.
Something like "/path/to/*"
is called a glob, and they are actually expanded by the shell, not by commands like ls
. While you could run the shell to have it expand the glob for you, it is easier to just use the built-in Dir
class.
Dir["/path/to/*.txt"]
This returns an array of matching file names.
精彩评论