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

Hi monks,

I have a drive space script (not mine,actually, but we're using it---we don't deserve any credit for it :)) that is running to try to determine user directory usage.

When it runs, it dies on a user's subdir with an apostrophe. I wonder if there is an "if" loop i can put into the following script to either escape the character(s) or to just skip the folder/dir.

here is the script, the error follows:

#!/bin/perl -w # # find big directories in a particular file system # ---- prompt user ---- print "directory> "; $partName="/home/users/"; chomp $partName; # ---- prompt user ---- print "size (in KB)> "; $dirSize="50000"; chomp $dirSize; # ---- find directories ---- @dirs=`find $partName -mount -type d | xargs du -sk`; # ---- print details on large directories ---- for $_ ( @dirs ) { chomp; my($size,$dirname) = split; if ( $size > $dirSize ) { if ( $size > 1000000 ) { # convert to GB $GB=$size/1000000; printf "%8.2f %s %s", $GB, "GB", "$dirname\n"; } elsif ( $size > 1000 ) { # convert to MB $MB=$size/1000; printf "%8.2f %s %s", $MB, "MB", "$dirname\n"; } else { printf "%8.2f %s %s", $size, "KB", "$dirname\n"; } } }

after /home/users is /home/users/0-z..etc/0-z...etc for the first two characters in their username..

xargs: Missing quote: /home/users/b/l/blah/ stuff/webtemp/MP#s
the MP#s in the error actually has a folder named "MP3's"


help would be appreciated....

Replies are listed 'Best First'.
Re: drive space script dies on certain characters...
by trammell (Priest) on Jan 18, 2005 at 17:05 UTC
    This isn't a Perl problem, it's a shell/find/xargs problem. One solution is to delimit your piped data with nulls:
    find ... -print0 ... | xargs -0 ...
Re: drive space script dies on certain characters...
by legato (Monk) on Jan 18, 2005 at 17:30 UTC

    Well, this isn't much of a Perl script -- it's mostly a Perl wrapper around a shell command. That might be OK, but as you're finding out, it has limitations. You can either try to anticipate any strangeness (and changes) in the shell output, or you can eliminate the shell altogether by using File::Find.

    I wrote this little tool that lists the top $ARGV[0] largest files found at or below the current directory. This was written for Windows, so it doesn't do symlink exclusion (you'll want to add that, or symlinks will be reported as large files), and I haven't bothered to do "pretty-printing" of directory sizes in KB/MB -- all sizes are in bytes.

    This is by no means fantastic code, but it works; and, with a touch of mod, it will work the way you want it to:

    #!/usr/bin/perl -w use File::Find qw/find/; use File::Spec; my @Files; my $total_size; find(\&wanted, File::Spec->curdir()); @Files = sort_size(@Files); my $limit = @ARGV ? $ARGV[0] : @Files; for (@Files[0..$limit-1]) { printf "%-60s : %d\n", $$_{-name}, $$_{-size} } print "Total size: $total_size\n"; sub wanted { my @stat = stat; #~ print STDERR "$File::Find::name\n" unless $stat[7]; return unless $stat[7]; my %file = ( -name => $File::Find::name, -size => $stat[7] ); $total_size+=$stat[7]; push @Files,\%file; return 1; } sub sort_size { my $desc = $_[0] ? shift : 0; shift unless $desc; my @array = @_; if ($desc) { @array = sort { $$b{-size} <=> $$a{-size} } @array; } else { @array = sort { $$a{-size} <=> $$b{-size} } @array; } return @array; }

    Anima Legato
    .oO all things connect through the motion of the mind

Re: drive space script dies on certain characters...
by qq (Hermit) on Jan 18, 2005 at 17:10 UTC
Re: drive space script dies on certain characters...
by gwhite (Friar) on Jan 18, 2005 at 17:08 UTC
    @dirs=`find $partName -mount -type d | xargs du -sk`;

    you might try putting double quotes around the $partName, looks like a syntax error from find something like (but totally untested).

    @dirs=`find "$partName" -mount -type d | xargs du -sk`;