Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists using a wildcard

Tags:

ruby

In Ruby, how can I check if a file exists using a wildcard?

Apparently this does not seem to work:

File.exists?("/folderOfFile/Filename*.ext") 
like image 419
Michael Küller Avatar asked Nov 19 '12 11:11

Michael Küller


People also ask

How do you use wildcards in Linux?

Option with '*. ' The wildcard '*' means it will match any number of characters or a set of characters. For example, S**n will match anything between S and n.

How to check if a command exists in Bash?

Use the command -v Command to Check if a Command Exists in Bash. The command -v is a built-in function in all POSIX systems and Bash. This function checks if a command exists as it returns the valid path for that command if it does exist and returns NULL if it does not.

What is Shopt?

Shopt is a built-in command in Unix-like operating systems, such as macOS and Linux distributions. The “shopt” command provides control over many settings that are used to tweak the operations in a Bash shell.


1 Answers

Your wildcard would refer to a set of files, not a single file. You could use Dir::glob for this:

!Dir.glob('/folderOfFile/Filename*.ext').empty? 
like image 186
dbenhur Avatar answered Sep 24 '22 11:09

dbenhur