#!/usr/bin/perl # # tidyhome.perl # # Perl script to parse a ~/.tidy file and move files from # user home directory to thier tidied/<category> dir. # # (c) 2000 Billy Allan. # Released under the terms of the GNU General Public License. # See http://www.gnu.org for details. # # Version 0.1 Fri Nov 24 19:43:39 GMT 2000 # Version 0.2 Sun Jan 14 15:16:47 GMT 2001 # Version 0.3 Mon Mar 19 18:48:52 GMT 2001 # # Usage: # tidyhome.perl [username] [username]... # # If no username is supplied, the current UID is assumed (and warne +d) # # Format of ~/.tidy file : # <basedir> # <dir-type> <extension> [<extension> <extension>...] # <dir-type> <extension> ... # ... # # For example : # # tidied # pictures jpg xcf gif png tiff # docs/web html htm # docs/text txt doc # movies avi mpg fli mpeg mov # # The files will be moved into a folder called : # ~/<basedir>/<dir-type> # ****which must exist already!**** # $basedir = 'tidied'; # default $config = '.tidy'; ####################### if ($#ARGV < 0) { warn("No username given - defaulting to current username\n"); @details = getpwuid($<); # get the current user-account @userlist = $details[0]; # get the username } else { @userlist = @ARGV; } ####################### foreach $user (@userlist) { if ($homedir = (getpwnam("$user"))[7]) { if (! tidy($homedir)) { warn("Could not tidy $user homedir...\n"); } } } 1; ####################### sub tidy # tidy($homdir) { my $home = $_[0]; if (open(CONFIG,"<$home/$config")) { $basedir = <CONFIG>; chomp($basedir); while (<CONFIG>) { chomp(); my ($key,@types) = split(/ /); if ($key =~ m/[^0-9a-zA-Z_\-\/]/) # only allow "saf +e" directory names { print "Invalid directory in .tidy file...\n"; next(); } foreach my $type (@types) { chomp($type); my @list = glob("$home/*.$type"); foreach my $file (@list) { my $result = rename($file,"$home/$basedir/$key"); } $type = uc($type); @list = glob("$home/*.$type"); foreach $file (@list) { $result = rename($file,"$home/$basedir/$key"); } } } return 1; } else { return 0; } }

In reply to tidyhome by billysara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.