Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

File::Glob Ignores Dot Files

by dvergin (Monsignor)
on Nov 25, 2003 at 18:19 UTC ( #309995=perlquestion: print w/ replies, xml ) Need Help??
dvergin has asked for the wisdom of the Perl Monks concerning the following question:

Following advice here I am using File::Glob's bsd_glob() to grab directory listings. But I now discover that on my Linux 7.3 system both glob() and bsd_glob() ignore dot files (like .bashrc .vimrc etc.). Here's a demo:
#!/usr/bin/perl use warnings; use strict; use File::Glob ':glob'; my @ary = bsd_glob( "/home/dvergin/*" ); #my @ary = glob( "/home/dvergin/*" ); # same result print "[$_]\n" for @ary;
But the files beginning with a dot are not listed. I need them. I'm using the File::Glob module for its handling of spaces in directory names and I am using bsd_glob() following the advice in response to my previous post mentioned above.

How do I get graceful handling of spaces in dir names and glob-ish lists of directory contents that include the dot files?

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall

Comment on File::Glob Ignores Dot Files
Download Code
Replies are listed 'Best First'.
Re: File::Glob Ignores Dot Files
by Paladin (Priest) on Nov 25, 2003 at 18:45 UTC
    Just like your shell, * ignores files starting with .. You need to explicitly state it:
    my @ary = bsd_glob( "/home/dvergin/{.*,*}" )
      That will include '.' and '..' in the list. I have seen a pattern that will leave those out but it is messy. It is simpler to filter them out.
      my @files = grep { /^\.\.?$/ } bsd_glob( "{.*,*}" );
        System directories '.' and '..' can be ignored just by using file test flag '-f': my @files = grep { -f } bsd_glob( "{.*,*}" );
Re: File::Glob Ignores Dot Files
by Anonymous Monk on Nov 25, 2003 at 18:43 UTC
Re: File::Glob Ignores Dot Files
by Aristotle (Chancellor) on Nov 26, 2003 at 11:10 UTC
    This is known, expected and desired behaviour. The proper glob pattern to include all files other than . and .. is {[^.],.[^.],.??*,*}

    Makeshifts last the longest.

      The correct form of this glob pattern is really {.[!.],.??*,*}. Unfortunately, that has bugs in perl 5.8.8, although it works on 5.14 and 5.16. So, the actual best practice may be to avoid glob altogether and open a dirhandle yourself and read/filter the file names you get back.

        Nowadays Id just use Path::Tiny and do path($to_dir)->children. It excludes . and .. automatically, which is a great default. It also spits out Path::Tiny objects full of useful convenience methods, rather than just strings, which help make code much less grubby.

        Makeshifts last the longest.

Re: File::Glob Ignores Dot Files
by Zed_Lopez (Chaplain) on Nov 25, 2003 at 18:45 UTC

    They take regexes as arguments and * doesn't work the same way as in your shell. Try '/.*' at the end.

    Updated: Oops. Listen to the above monks. It's not a regexp. Note also that their code will return . and .. and you might want a grep to exclude them.

      In shell commands I use .[!.]* It works if you don't have files with two leading ..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://309995]
Approved by davido
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2015-07-08 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls