Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

File::Glob Ignores Dot Files

by dvergin (Monsignor)
on Nov 25, 2003 at 18:19 UTC ( [id://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

Replies are listed 'Best First'.
Re: File::Glob Ignores Dot Files
by Paladin (Vicar) 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 ..