Dir.glob hidden files

Hi everyone, recently I did a test and I saw the new way for Dir.glob don’t return the hidden files, even I tried to set all different options.

I did that test:

puts Dir.glob(Dir["**/*"], File::MatchOptions::DotFiles)

puts Dir.glob(Dir["**/*"], File::MatchOptions::NativeHidden)

puts Dir.glob(Dir["**/*"], File::MatchOptions::OSHidden)

I have the same result in all cases, even there is a hidden file called .fil:

zohran@alienware-m17-r3 ~/Downloads $ ls -la 
total 3411944
drwxr-xr-x 1 zohran zohran        310 Nov  1 21:02  .
drwxr-xr-x 1 zohran zohran        384 Nov  1 10:57  ..
-rw-r--r-- 1 zohran zohran     108075 Oct 25 12:19  CV-Developper-Final.pdf
-r-------- 1 zohran zohran     241621 Oct 29 14:59 'CV Kissaten.pdf'
-rw-r--r-- 1 zohran zohran      91516 Oct 26 17:21  CV-Retail.pdf
-rw-r--r-- 1 zohran zohran 3492741120 Oct 14 08:01  debian-live-12.2.0-amd64-kde.iso
-rw-r--r-- 1 zohran zohran          0 Nov  1 20:57  .fil
-rw-r--r-- 1 zohran zohran      53979 Oct 19 07:16  SC2-Form.pdf
-rw-r--r-- 1 zohran zohran        177 Nov  1 21:02  Test.cr
-r-------- 1 zohran zohran     521066 Oct 19 07:26  Your-National-Insurance-number-letter.pdf
-rw-r--r-- 1 zohran zohran      57222 Oct 27 17:07  Zoom.png
zohran@alienware-m17-r3 ~/Downloads $ crystal Test.cr 
["debian-live-12.2.0-amd64-kde.iso", "SC2-Form.pdf", "Your-National-Insurance-number-letter.pdf", "CV-Developper-Final.pdf", "CV-Retail.pdf", "Zoom.png", "CV Kissaten.pdf", "Test.cr"]
["debian-live-12.2.0-amd64-kde.iso", "SC2-Form.pdf", "Your-National-Insurance-number-letter.pdf", "CV-Developper-Final.pdf", "CV-Retail.pdf", "Zoom.png", "CV Kissaten.pdf", "Test.cr"]
["debian-live-12.2.0-amd64-kde.iso", "SC2-Form.pdf", "Your-National-Insurance-number-letter.pdf", "CV-Developper-Final.pdf", "CV-Retail.pdf", "Zoom.png", "CV Kissaten.pdf", "Test.cr"]

Each of your Dir.glob calls operate on the return value of Dir["**/*"], a method which does not return hidden files by default. So what you’re doing is asking to find all files, including hidden ones, among a list of non-hidden files. By definition, that can’t return hidden files.

Try this instead:

Dir.glob(["**/*"], match: :dot_files)

Note that I removed the second Dir, so it’s just passing the pattern rather than the list of files. Also, since File::MatchOptions is an enum, you can pass a symbol to it — you don’t need a fully qualified name.

4 Likes