Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get files recursively inside directory in ruby [duplicate]

Tags:

ruby

How can I return a list of only the files, not directories, in a specified directory?

I have my_list = Dir.glob(script_path.join("*"))

This returns everything in the directory,including subdirectories. I searched but haven't been able to find the answer.

like image 230
user982570 Avatar asked Mar 08 '26 11:03

user982570


1 Answers

In addition to Mark's answer, Dir.entries will give back directories. If you just want the files, you can test each entry to see if it's a file or a directory, by using file?.

Dir.entries('/home/theiv').select { |f| File.file?(f) }

Replace /home/theiv with whatever directory you want to look for files in.

Also, have a look at File. It provides a bunch of tests and properties you can retrieve about files.

like image 161
theIV Avatar answered Mar 11 '26 01:03

theIV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!