Perl Script to find the disk usage and to delete the files which is consuming more space

Hi All,

I have written a script to check the file system usage and to delete the files which is consuming more space.Please check whether the script is corrcet

#Script Starts here 
#!/usr/local/bin/perl
#Program to find the disk space and to delete the older files
#Checks the type of OS here
$OS = `uname -a | awk '{print $1}'`;
print ("The Operating System is $OS");
if ($OS == SunOS ) {
   
    print ("Enter the filesystem related to SUN \n");
    $FILESYSTEM = <STDIN>;
     
    $DF = `df -sk $FILESYSTEM | awk '{print $3,$4,$5}' | awk '{if($3>50)print $3}'`;
    
    print ("The file system $ FILESYSTEM usage is $DF \n");
    while ($DF > 50){
    
    print ("The file system $FILESYSTEM reached its maximum capacity $DF and request you to clear the space /n");
    print ("Please enter "YES" if you want to delete the files which is consuming more space /n");
    print ("Please enter "NO" if you want to proceed as it is /n");
 
    $OPTION = <STDIN>;
    given ($OPTION) {
    when ("YES")  { `find $FILESYSTEM -size +5242880c -size -10485760c 2>/dev/null -print | xargs -i rm -i {}` }
                  { Print "The file system which is consuming more space has been removed \n" }
                  break;
    when ("NO") { exit (); }
 
    }
    }
    } elsif ($OS == HP-UX) {
    print ("Please enter the filesystem related to HP-UX \n");
    $FILESYSTEM1 = <STDIN>;
    $DF1 = `bdf $FILESYSTEM1 | awk -F " " '{ print $3,$4,$5 }' | xargs -i cut -f 3 -d " " {}`;
    print ("The file system $ FILESYSTEM usage is $DF \n");
    
    while ($DF1 > 50){
    print ("The file system $FILESYSTEM1 usage is $DF1 and request you to clear the space \n");
    }
#End Of Script
    }

Such a script is really dangerous and it should contain code to stop it running on any Operating System Partition and anywhere which legitimately has large files. Taking the filesystem as a user-supplied parameter is asking for trouble.

The script comment says "Program to find the disk space and to delete the older files". There is no code in the script which looks at the age of the files, but there is code to look at the size of individual files.

I can't vet Perl code, but the unix commands look iffy.

This find command has a lot wrong with it.
It needs -xdev to confine the scope to one filesystem.
It needs -type f to just look at files.
There is a Shell redirect in the middle of the arguments.
The size range needs escaped brackets or only the second value will be taken into account.
The size range is very strange. It equates to 5 Megabytes to 10 Megabytes.
Test the script with an echo not a direct rm command !

`find $FILESYSTEM -xdev -type f \( -size +5242880c -a -size -10485760c \) -print 2>/dev/null | xargs -i echo rm -i {}``

The delimiter between fields is not a single space, it is multiple spaces.
You probably mean something like:

DF1 = `bdf $FILESYSTEM1 | grep -v "Filesystem"|sed -e "s/%//g"|awk '{print $5}'`

I believe that both Operating Systems have df -Pk and could produce the same format of output.

1 Like

Hi All,

Some one please help me, i'm receiving the following error when i execute this script

./diskusgae.pl: line 7: use: command not found
./diskusgae.pl: line 9: use: command not found
./diskusgae.pl: line 11: use: command not found
./diskusgae.pl: line 17: =: command not found
./diskusgae.pl: line 19: syntax error near unexpected token `"The Operating System is $OS \n"'
./diskusgae.pl: line 19: `print ("The Operating System is $OS \n");'
bash-3.00$ vi diskusgae.pl
"diskusgae.pl" 73 lines, 1613 characters

Please help me as i need to implement this in my pre-prod environment.

The Shebang line MUST be the first line of a script. Delete the line "#Script Starts here".

Is this your first script?

To find out the O/S you just need uname -s . Try it.

1 Like

Hi Methyl,

Thanks a lot for the input.

Let me try to modify the script and let you know if i face any problems.

1 Like

Hi Methyl,

Good Day !!

When i try to execute the below command from command line I'm getting the required output.

df -h $FILESYSTEM -exec | grep -v "Filesystem" | sed -e "s/%//g"|awk '{print $5}' 

But when i try to execute the same in script i'm receving the below exception

bash-2.05$ ./diskusage.pl
The Operating System is SunOS
Enter the filesystem related to SUN
/u001
sh: syntax error at line 2: `|' unexpected

Please guide me.

---------- Post updated 06-13-12 at 08:24 PM ---------- Previous update was 06-12-12 at 11:15 PM ----------

Hi All,

Could some one guide me

When i try to execute the below command from command line I'm getting the required output.

Code:
df -h $FILESYSTEM -exec | grep -v "Filesystem" | sed -e "s/%//g"|awk '{print $5}'
But when i try to execute the same in script i'm receving the below exception

Code:
bash-2.05$ ./diskusage.plThe Operating System is SunOSEnter the filesystem related to SUN/u001sh: syntax error at line 2: `|' unexpected

Please guide me.

I think that this needs someone who knows Perl. The error is coming from unix Shell but the main program is Perl. Perhaps the call from Perl to unix Shell contains an error which is corrupting the command?

Depending on which version of Sunos this is, the sh command may be the old Bourne Shell and you may need to use nawk not awk .

You need to escape the characters which are having also some meaning in perl.

$DF1 = `bdf $FILESYSTEM1 \| awk -F " " '{ print \$3,\$4,\$5 }' \| xargs -i cut -f 3 -d " " {}`;

Try running it and if need escape the "{" and "}" also.
You also might not need to use -F in awk since the default field separator in awk is space/tabs.

Thanks @clk. Learnt a bit about Perl today, though the script is perhaps complicated by using Perl.
Suggested corrected bdf in post #2.

1 Like