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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script to find the disk usage and to delete the files which is consuming more space
# 1  
Old 06-11-2012
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

Code:
#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
    }


Last edited by pludi; 06-11-2012 at 12:32 PM..
# 2  
Old 06-11-2012
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.

Quote:
`find $FILESYSTEM -size +5242880c -size -10485760c 2>/dev/null -print | xargs -i rm -i {}`
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 !
Code:
`find $FILESYSTEM -xdev -type f \( -size +5242880c -a -size -10485760c \) -print 2>/dev/null | xargs -i echo rm -i {}``


Quote:
DF1 = `bdf $FILESYSTEM1 | awk -F " " '{ print $3,$4,$5 }' | xargs -i cut -f 3 -d " " {}`;
The delimiter between fields is not a single space, it is multiple spaces.
You probably mean something like:
Code:
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.

Last edited by methyl; 06-11-2012 at 06:03 PM.. Reason: typo
This User Gave Thanks to methyl For This Post:
# 3  
Old 06-11-2012
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.
# 4  
Old 06-11-2012
Quote:
#Script Starts here
#!/usr/local/bin/perl
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.
This User Gave Thanks to methyl For This Post:
# 5  
Old 06-11-2012
Hi Methyl,

Thanks a lot for the input.

Let me try to modify the script and let you know if i face any problems.
This User Gave Thanks to arunkarthick For This Post:
# 6  
Old 06-13-2012
Hi Methyl,

Good Day !!

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.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.

Last edited by Franklin52; 06-13-2012 at 03:45 AM.. Reason: Please use code tags for data and code samples
# 7  
Old 06-13-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Files consuming more space in HP-UX

Hi, Could you please provide OS command to find large files in size MB and GB... under specific directory in HP-UX? Regards, Maddy (4 Replies)
Discussion started by: Maddy123
4 Replies

2. Shell Programming and Scripting

Need Generic command for disk space usage

Given this directory /web I need to get the current usage (in %) on Linux and Unix both using the same command on bash shell ? The command i tried was working on Unix (solaris) but does not filter the desired same value when run of Linux. My command df -h /web | awk '{print $5}' | sed -n... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. UNIX Desktop Questions & Answers

Issue with disk space usage

Issue with disk space usage I have the following line in my "df -h" output: Filesystem Size Used Avail Capacity Mounted on /dev/ad4s1a 496M 495M -39M 109% / What is the issue with having 9% excess utilisation? How can I find out what this partition is... (2 Replies)
Discussion started by: figaro
2 Replies

4. Shell Programming and Scripting

Disk Usage - Space Used

Hi all, FreeBSD7.1 @ sh. In a backup script I am trying to get the blocks used by the backup once completed. I am using the function: #!/bin/sh spaceused() { du -d 0 "${1}" | awk -F"+" '{ print $1 } } to return the blocks used of said directory and contents. Via. command line... (7 Replies)
Discussion started by: Festus Hagen
7 Replies

5. Shell Programming and Scripting

script to monitor disk space usage

Some times my disk space is used upto 100% due to the application logs . So this script is to monitor the disk space usage and wall message to the users about the disk space usage if it exceeds the limit set in the script. Here for example the limit is set to 80%. This job is added in cron to... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

6. UNIX for Dummies Questions & Answers

How to find a file whick is consuming larger disk space in file system

Hello, Can anybody please tell me the command to find out the filesystem or a file which is consuming larger disk space sing i want to find out the file and want to compress it please help me out any help would be appreciated (6 Replies)
Discussion started by: lokeshpashine
6 Replies

7. Shell Programming and Scripting

Shell script delete log files from folder & subfolders on space usage

Hi, I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes. I have to keep the latest 5 files at any time, my problem is that log files... (3 Replies)
Discussion started by: saha
3 Replies

8. Shell Programming and Scripting

Perl script to check free disk space

hello, I have to check the free space on the disk that would work both on Windows and Unix platform e.g on C: \ for Windows and / on Unix. I could use Unix command 'df ' ( my windows system has Unix emulator cygwin and could run 'df ' as well). But I'd like not to rely on system command but... (1 Reply)
Discussion started by: susja
1 Replies

9. UNIX for Dummies Questions & Answers

how to determine the disk space usage

how can we determine the disk space used by a certain directory? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies

10. UNIX for Dummies Questions & Answers

finding disk space usage

How would I go about finding the about of disk space occupied by a certain directory? For example, /u1/cvera => 530 MB Thanks =) (3 Replies)
Discussion started by: cvera8
3 Replies
Login or Register to Ask a Question