Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a listing of only files using Dir.glob?

Tags:

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 884
user982570 Avatar asked Oct 06 '11 16:10

user982570


People also ask

How do I get a list of directories in python?

listdir() – It is used to list the directory contents. The path of directory is passed as an argument.


2 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 186
theIV Avatar answered Oct 02 '22 10:10

theIV


Dir.glob('*').select { |fn| File.file?(fn) }

like image 31
Zepplock Avatar answered Oct 02 '22 09:10

Zepplock