check file exists or not using regular expression in ruby - ruby

I have a folder which contains many files. Now I have to check file with specific name exists or not.
For checking file existence I have used File.exists? method. For example
File.exists? ("/home/ubuntu/vga2usb.sql")
=> true
but sometimes filename contains timestamps (e.g. 20140707_vga2usb.sql). I think I have to use regular expression for it.
So I tired with
File.exists?("/home/ubuntu/*_vga2usb")
=> false
Is there any other way for checking file existence.
Thanks in advance.

Try this:
!Dir.glob("/yourpattern/*.sql").empty?

You can use Dir or Dir.glob http://ruby-doc.org/core-2.1.2/Dir.html
Here:
Dir["/home/ubuntu/*_vga2usb"]
Dir.glob("/home/ubuntu/*_vga2usb")

Related

how to check if file exists in all subdirectories?

How to check if the file exists in all subdirectories within chef recipe (not_if guard).
There is no specific answer other than "write some Ruby code to check what you want". You'll probably want to do a Dir glob and then compare the various outputs but maybe you just need an all? and File.exist?.

Pathname with Regex

I would like to know how to use Regex when instantiating a new Pathname.
I am instantiating a Pathname and passing it to FileUtils#rm_rf to delete the file. The problem I am trying to solve is to remove files that have a certain name without regard to extension:
See this contrived example:
target = Pathname.new(["#{#app_name}/#{#file_name}"])
FileUtils.rm_rf(target)
#file_name does not include extensions such as .rb or html.erb, but I would like to match all files with name equal to #file_name no matter what extensions they use.
My initial approach was to use Regex. But how can I use it, or any other suggestions?
You can use Dir.Glob like this:
Dir.glob("#{#app_name}/#{#file_name}.*").each { |f| File.delete(f) }
See more on that at http://ruby-doc.org/core-2.2.1/Dir.html#method-c-glob

Using Ruby to find a file with changing directory

I'm trying to find a file in which the directory will change its name with upcoming versions, so an example could be that it is located under /opt/here/test-1.44/bin/progname and will follow the format same time.
I'm looking to do something like if File.exist?("/opt/here/test-*/bin/progname") but is that the correct format? When searching around I'm also seeing references to using Dir, so would it be something like if Dir['/opt/here/*'.select { |f| f =~ /progname/} then ?
Thanks!
Do
Dir.glob("/opt/here/test-*/bin/progname").empty?
Use any? instead of empty? if you want true when there is such file.

Check if a file exists using a wildcard

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")
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?

Ruby - Search and collect files in all directorys

I'm trying to search for a certain file type within all directories on my unix system using a ruby script. I understand the following code will search all files ending with .pdf within the current directory:
my_pdfs = Dir['*pdf']
As well as:
my_pdfs = Dir.glob('*.pdf').each do |f|
puts f
end
But how about searching all directories and sub-directories for files with the .pdf extension?
Check out the Find module:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/find/rdoc/Find.html
Using Dir.glob is less than ideal since globbing doesn't handle recursion nearly as well as something like find.
Also if you're on a *nix box try using the find command. Its pretty amazingly useful for one liners.
Maybe something like:
pdfs=Dir['/**/*.pdf']
?
Not using Linux right now, so don't know if that will work. The ** syntax implies recursive listing.

Resources