jcpunk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have been asked to write a program (I chose perl naturally) to go through a directory tree and learn the file permissions (octal) and ownership(user:group), then dump the knowledge to a file.

I haven't a clue where to begin or how to approach it. Thus I turn to your wisdom.
Don't do my work for me, but please kick me in the right direction. What would you advise for even approaching this task? opendir, modules, or something else?


jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

Replies are listed 'Best First'.
Re: Save permissions on a directory tree
by ptum (Priest) on Mar 15, 2006 at 17:23 UTC

    I've tended to use File::stat for such things, maybe in combination with File::Find?


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Save permissions on a directory tree
by revdiablo (Prior) on Mar 15, 2006 at 17:49 UTC

    I think Perl might not be the first choice here. Personally, I'd use some standard unix utilities:

    cd /directory ls -lR > knowledge
Re: Save permissions on a directory tree
by McDarren (Abbot) on Mar 15, 2006 at 17:30 UTC
    I'd probably use File::Find, stat, and getpwent.

    Although it would probably be quicker and easier to just use ls, grep and awk :p

Re: Save permissions on a directory tree
by rafl (Friar) on Mar 15, 2006 at 17:37 UTC
Re: Save permissions on a directory tree
by roboticus (Chancellor) on Mar 16, 2006 at 02:48 UTC
    jcpunk--

    Assuming you're on a *nix box, I'd second revdiablo's suggestion to use standard utilities, specifically:
    find . -fprintf the_file_name "%p %m %u:%g\n"
    --roboticus
      Assuming you're on a *nix box, I'd second revdiablo's suggestion

      Funnily enough, my suggestion does not really require a unix machine. Most of the good utilities have been ported to many other operating systems. Consider the following from my local Win32 box:

      U:\>find . -printf "%m %u:%g %p\n" | tail 777 ctilden:Domain Users ./SRID 644 ctilden:Domain Users ./trans.csv 755 ctilden:Domain Users ./WINDOWS 644 ctilden:Domain Users ./WINDOWS/ArtGalry.Cag 755 ctilden:Domain Users ./WINDOWS/system 644 ctilden:Domain Users ./WINDOWS/win.ini 644 ctilden:Domain Users ./winscp.RND 777 ctilden:Domain Users ./work 644 ctilden:Domain Users ./zeros.pl 644 ctilden:Domain Users ./_viminfo

      Update: This post is meant to be general in nature, with debates about the usefulness of this to the original topic notwithstanding. =)

Re: Save permissions on a directory tree
by smokemachine (Hermit) on Mar 15, 2006 at 17:52 UTC
    you can use this: perl -e 'printf "%04o $_\n", (stat)[2] while </etc/*>'

    perldoc -f stat