Perl script to delete file based on filename character

I had list of random file with the following title :

SQjb2LNl  7XUPq4pE33  BfXleRiBEs  DScVfSTsmp  g98OFxz2Qy  ILAct4iIj6    kwO1CyUMfD      NDooi2dgzK      PPWVzWKSwU  s3nj04fMuG  UIL7ywnzGo      wughd2WRh5  ZAZjFiudHt
39TFHrbdFc  7XUrHjgPq7  BfXM2XVgMp  DScXFGQuYc  g99G9Lk0X0  ILAFJ9iQjH    KwoaLh0EKN      NDOVoveuoE      ppwWQ3w2A1  s3nJj5bnhJ  uILFVt2YHP      wUGhzV43Xk  ZaZlhRXpsp
39toXq0R6p  7xuXcgXcxU  BFXnAsoL5Z  dscZ8LysSH  G99vjNZeks  iLaGbhaCW7    kWob3qoT9n      NDOYO7gbMx      ppWX3JObX7  s3NJUE465L  uiLhd6Sxii      WuGJnRIZR2  zazmXjqYZ7
39U7jTRy8y  7xUyotFrly  BfXp3Q2Two  dSCzDw0byL  g9a0vLKpJv  IlAjkfH8mn    KwoCDNhW5g      NDp0eU0TjO      PpwYC9CqnG  S3NMVcc4va  UilI72Bw0o      wugm2aKMwT  zAZnbyixd5
39Ui9in6xN  7XuZBiDTpA  BFXPrL5Izj  dSD3x3OM43  G9aCZLn44P  IlaJp098s4    kWOCjOfZYp      nDP12tgUXi      Ppx11IGezl  s3noT2ti9q  UilIfElLiB      WugQbXIliR  zazogpQUIZ
39UPFbH1xr  7xUzhUUzE5  bFXq3j1Thx  DSd9tQnfis  g9aiXtwuU5  ilAleaQfgm    kWOkNyRgey      Ndp4uNOeVK      ppX2WtUIEK  S3nXZojsN9  UiLioaP5Vj      WuGtkUd7vo  ZaZoLC9mDU
39uPhykrG6  7XV83jOLLs  bfXTB8pVF8  dSDbuPHqAt  g9AJiNqwjk  ILARXYbcwy    KWOMlpjHhS      NDpa2BkAPi      pPX3TbonIY  S3obC4D7iJ  uILpjtM1bS      wUH0sLrm4w  ZazRQK1ovf
39UsKLZ2uo  7XvCUqHZzE  bFXTRfzmCo  dSDdHuDY4q  G9ARfJIlJX  IlatV1EOvv    kWOnBztryB      NdpE4d2gn7      pPX4bqUSlB  S3ohRIY3cj  uilqxZKS6i      WUh348a8nl  zAZrxJGHZc
39uz22D02M  7xVFhYWHef  bFXuiQ7woi  dsDE4nIeNk  G9ATGzugP0  ilAUZ2zYM5    kwOUdUWQFl      nDPE4JkK85      PpX9S1C11x  S3Oiia6rEU  UiLt3NP012      WUh4bzkzFq  zAztwimlGj
39v4Ix6cAL  7XvPjpZO9R  BFXVgk9cov  dSdJxYq3UC  G9ATP2cZtY  ILax6AuZbF    KWouvkVMO9      ndpFYRNgIw      PPXcDJNc96  s3oKlOzSZs  uILX65VCgG      wUHAsBXLgk  ZB0377Cuhb
39vAFo96h8  7xvPZX4NiS  BFXx9KUgVT  DSdmb63vmo  G9BHWCUfFs  ILay12gaNZ    KwOxWpWR18      Ndpg7TRXaB      ppXdbY9HYl  s3OMKa2TGx  uilzI7gGI3      WUhB1To6eY  Zb0cG66lDq
39VNQKKHoF  7XVRTBUz3E  bFY1hoLDKM  dSdMZBPXoc  G9BJx5LeGc  IlayqNkuWH    KWoXXO4Aem      NdpI4IR4CP      pPXia2ItD9  S3oPA0J1FR  Uilzze3n3p      wuHbhOQAcN  ZB0Cm75ZAt
39W9aHgRoY  7XVVI8F6vf  BfY2y2NAlu  DSDQtPHe9w  g9bM8a171h  iLB5GHaXjy    kWpaQpSvQD      NdPiKJrceD      ppxJmb9cZg  s3OSdZBD1v  uIm8DHfVdq      WuHDPVGVrI  zb0mm1ACMC
39wC6gXShg  7xVWd0JpRS  bfY63S1IbC  DsDsN5R9Ax  g9boByCEzb  ilB6ovam7w

but it had eat upto 20 GB of diskspace..

one of similiarities is the count of character used in filename..

so , I did the following to delete it.. few thousand of them..

my $dir = "/tmp";
opendir (DH,$dir);
while (my $f = readdir (DH)) {
 my $char = split ("",$f);
 print scalar $char . " : $f \n";
 if ( $char = 10 ) {
 #system ( "rm -rf /tmp/".$f);
 $file = $dir."/".$f;
 unlink $file or warn "Could not unlink $file: $!";
 print "deleted : $file"."\n";
 }
};
closedir(DH)

Related Post

One Response

  1. SPIce says:

    Thank you for your post.

    I tried to use the NEWLINE=1 – Option first, but it gave me strange output.

    You just saved my day with your little ‘magic’ :o)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *