Operating System - HP-UX
1821062 Members
2710 Online
109631 Solutions
New Discussion юеВ

Perl script for cleaning dir

 
svilen888
Occasional Advisor

Perl script for cleaning dir

Hi!
Please help me with perl-scripting.
So I need to write a script, which:

1. Sort all files in given directory by date.
2. Clean all old files except the 200 newest.

But this script must be in good performance, I mean not to use system("rm ...") to invoke shell every time to remove every file.
Good solutions will be pointed good :))
Regards...

liana
5 REPLIES 5
Victor Fridyev
Honored Contributor

Re: Perl script for cleaning dir

I'm not sure, you need perl in order to obtain good performance. If you don't have subdirectories, the solution can be like this:
ls -1tr | awk 'NR>200 {print}' |xargs rm

HTH
Entities are not to be multiplied beyond necessity - RTFM
H.Merijn Brand (procura
Honored Contributor

Re: Perl script for cleaning dir

# perl -e'@f=sort{-M$a<=>-M$b}<*>;@f<=200&&exit;splice@f,0,200;unlink@f'

Cannot beat that in speed and simplicity I guess :)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Tim Howell
Frequent Advisor

Re: Perl script for cleaning dir

Victor,

I like your script, but I think he would need to leave out the "r" arg in the ls command to keep the 200 newest. (The "t" arg already sorts by latest first...)
if only we knew...
harry d brown jr
Honored Contributor

Re: Perl script for cleaning dir

You need to determine what you mean by old. You have access time, create time and modified time:

this is an example of "ls -lt", "ls -lct" and "ls -lut":

# ls -lt|more
total 31722
-rw-r--r-- 1 root root 7 May 14 11:11 wtmp.stat
-rw-r--r-- 1 root root 53 May 14 11:11 wtmp.out
-rw-rw-rw- 1 root sys 10675 May 14 09:00 vpart6_pm.txt
-rw-rw-rw- 1 root sys 10336 May 14 09:00 vpart5_pm.txt
-rw-rw-rw- 1 root sys 11776 May 14 09:00 vpart3_pm.txt
-rw-rw-rw- 1 root sys 37817 May 14 08:58 vpart1_pm.txt
-rw-r--r-- 1 root root 99 May 14 08:57 sys.tst_systest
drwxr-xr-x 2 root root 96 May 14 08:52 .A
-r--r--r-- 1 root root 0 May 14 08:52 llbdbase.dat
drwx------ 2 root root 96 May 14 08:52 .AgentSockets
-rw------- 1 root root 464 May 14 08:45 portmap.file
-rw------- 1 root root 2004 May 14 08:45 rpcbind.file
-rw-r--r-- 1 root root 4 May 14 08:11 btmp.stat
-rw-r--r-- 1 root root 39 May 14 08:11 btmp.out
-rwxrwxrwx 1 root sys 1456 May 10 10:31 dah
-rw-rw-rw- 1 root sys 665 May 10 09:42 PATH_b4switch
-rw-rw-rw- 1 root sys 1667 Apr 27 08:25 machineinfo.dbs
-rw------- 1 root sys 0 Apr 27 08:25 shcmachineerr.log
-rw------- 1 root sys 0 Apr 27 08:25 shcmachineuser.log
-rw-rw-rw- 1 root sys 199907 Apr 26 14:48 sysinfo.sh
-rw-rw-rw- 1 root sys 250 Apr 26 10:49 SED1
-rw-rw-rw- 1 root sys 138 Apr 26 10:49 FILE.bck

# ls -lut|more
total 31722
lrwxrwxrwx 1 root sys 7 May 17 09:20 hdhdhd -> opc_tmp
lrwxrwxrwx 1 root sys 3 May 17 09:20 hjk -> fgh
-rw-r--r-- 1 root root 53 May 14 11:11 wtmp.out
-rw-r--r-- 1 root root 7 May 14 11:11 wtmp.stat
-rw-rw-rw- 1 root sys 19063 May 14 11:10 ioscan_full_system.txt
-rw-rw-rw- 1 root sys 10675 May 14 09:01 vpart6_pm.txt
-rw-rw-rw- 1 root sys 10336 May 14 09:01 vpart5_pm.txt
-rw-rw-rw- 1 root sys 11776 May 14 09:01 vpart3_pm.txt
-rw-rw-rw- 1 root sys 37817 May 14 09:01 vpart1_pm.txt
-rw-r--r-- 1 root root 99 May 14 08:57 sys.tst_systest
-r--r--r-- 1 root root 0 May 14 08:52 llbdbase.dat
-r--r--r-- 1 root sys 1019 May 14 08:51 install.vars
-rw------- 1 root root 2004 May 14 08:45 rpcbind.file
-rw------- 1 root root 464 May 14 08:45 portmap.file
-rw-r--r-- 1 root root 39 May 14 08:11 btmp.out
-rw-r--r-- 1 root root 4 May 14 08:11 btmp.stat
-rwxrwxrwx 1 root sys 1456 May 10 10:32 dah
drwxr-xr-x 3 root bin 96 May 10 09:45 pam_krb5
drwxrwxrwx 2 oracle dba 96 May 10 09:45 .oracle
drwxrwxrwx 3 root sys 1024 May 10 09:45 dsinstall
drwxrwxrwx 2 root fsr 1024 May 10 09:45 rnd
drwxrwxrwx 2 root sys 1024 May 10 09:45 pr015


# ls -lct|more
total 31722
-rw-r--r-- 1 root root 53 May 14 11:11 wtmp.out
-rw-r--r-- 1 root root 7 May 14 11:11 wtmp.stat
-rw-rw-rw- 1 root sys 10675 May 14 09:00 vpart6_pm.txt
-rw-rw-rw- 1 root sys 10336 May 14 09:00 vpart5_pm.txt
-rw-rw-rw- 1 root sys 11776 May 14 09:00 vpart3_pm.txt
-rw-rw-rw- 1 root sys 37817 May 14 08:58 vpart1_pm.txt
-rw-r--r-- 1 root root 99 May 14 08:57 sys.tst_systest
drwxr-xr-x 2 root root 96 May 14 08:52 .A
-r--r--r-- 1 root root 0 May 14 08:52 llbdbase.dat
drwx------ 2 root root 96 May 14 08:52 .AgentSockets
-rw------- 1 root root 2004 May 14 08:45 rpcbind.file
-rw------- 1 root root 464 May 14 08:45 portmap.file
-rw-r--r-- 1 root root 4 May 14 08:11 btmp.stat
-rw-r--r-- 1 root root 39 May 14 08:11 btmp.out
-rwxrwxrwx 1 root sys 1456 May 10 10:31 dah
-rw-rw-rw- 1 root sys 665 May 10 09:42 PATH_b4switch
-rw-rw-rw- 1 root sys 1667 Apr 27 08:25 machineinfo.dbs
-rw------- 1 root sys 0 Apr 27 08:25 shcmachineuser.log
-rw------- 1 root sys 0 Apr 27 08:25 shcmachineerr.log
-rwxr-x--x 1 root sys 20051 Apr 26 14:49 instSysInfo
-rwxr-x--x 1 root sys 11118 Apr 26 14:49 unSysInfo
-r--r--r-- 1 root sys 6087 Apr 26 14:49 sched.models

note the differences.

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: Perl script for cleaning dir

# perldoc -f -M

M Script start time minus file modification time, in days.
A Same for access time.
C Same for inode change time (Unix, may differ for other platforms)

So use -M as in my snippet for modification time, and change to -C for change time, and -A for access time

# perl -e'@f=sort{-M$a<=>-M$b}<*>;@f<=200&&exit;splice@f,0,200;unlink@f'
# perl -e'@f=sort{-C$a<=>-C$b}<*>;@f<=200&&exit;splice@f,0,200;unlink@f'
# perl -e'@f=sort{-A$a<=>-A$b}<*>;@f<=200&&exit;splice@f,0,200;unlink@f'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn