Glob does not recognize file name with square brackets

I have a folder, its name:

drwxr-xr-x. 2 user user 4096 Aug 2 18:30 folder name [www.website.com] 

but when i use glob as follows:

 my @files = glob ("/home/user/Downloads/folder name [www.website.com]/*"); print "@files/n"; 

it does not list files in the directory, the result is as follows:

 /home/user/Downloads/folder name 

I tried to avoid spaces and square brackets as follows:

 /home/user/Downloads/folder\ name\ \[www.website.com\] 

But the result is the same, which could be my mistake, or what can be done to improve my code?

Thanks in advance

+7
perl
source share
2 answers

The built-in glob function separates patterns in spaces. So you are doing something equivalent

 my @files = ( glob("/home/user/Downloads/folder"), glob("name"), glob("[www.website.com]/*"), ); 

Exiting spaces and brackets would be one option. But it is better to use the core File File::Glob module for finer control:

 use File::Glob ':bsd_glob'; my @files = bsd_glob("/home/user/Downloads/folder name \\[www.website.com\\]/*"); 

Now you need to copy only the brackets. It also cancels the built-in globe, so in fact you do not need to change your code except for one import (and, of course, a template).

+9
source share

Spaces and square brackets are special in globe patterns. To match

 /home/user/Downloads/folder name [www.website.com] 

you need

 /home/user/Downloads/folder\ name\ \[www.website.com\] 

So the full template

 /home/user/Downloads/folder\ name\ \[www.website.com\]/* 

To build this string, you can use a string literal

 "/home/user/Downloads/folder\\ name\\ \\[www.website.com\\]/*" 

Test:

 $ perl -E'say for glob "/home/eric/Downloads/folder\\ name\\ \\[www.website.com\\]/*"' /home/eric/Downloads/folder name [www.website.com]/foo 

Please note that spaces cannot be removed in Windows. You will need to use File :: Glob bsd_glob . (It calls the same function as glob , but with different flags.)

 use File::Glob qw( bsd_glob ); say for bsd_glob("C:\\Program Files\\*"); 
+1
source share

All Articles