CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
find files in the current directory that are zero and deleting them

 



popeye
Novice

Nov 6, 2013, 11:23 AM

Post #1 of 7 (180 views)
find files in the current directory that are zero and deleting them Can't Post

Is there a perl equivalent to
find . -type f -size 0 -exec rm {} \;


Thanks !!


Laurent_R
Enthusiast / Moderator

Nov 6, 2013, 11:53 AM

Post #2 of 7 (179 views)
Re: [popeye] find files in the current directory that are zero and deleting them [In reply to] Can't Post

Yes, you can do that, but it will be slightly longer code.

The glob function will give you a list of files matching a certain shell pattern (which can be "*" if you want every file).

The -s file test operator will give you the size of each file.

And unlink will provide a way of deleting a list of files.

So you could do something like this (quick untested answer):


Code
unlink grep {-s $_ == 0} glob ("$my_path/*.tmp");


But I would advise you to test very very carefully on dummy data before you proceed.


(This post was edited by Laurent_R on Nov 6, 2013, 11:54 AM)


popeye
Novice

Nov 6, 2013, 11:56 AM

Post #3 of 7 (176 views)
Re: [Laurent_R] find files in the current directory that are zero and deleting them [In reply to] Can't Post

Yes. Ill make a test directory and put junk files in there and run from there.

Trying to education myself with perl (first love is ksh, guess everyone has their favorites).

Thanks ... Ill give it a try and let you know how it works.


popeye
Novice

Nov 6, 2013, 12:20 PM

Post #4 of 7 (173 views)
Re: [popeye] find files in the current directory that are zero and deleting them [In reply to] Can't Post

Am I doing something wrong ?


Code
/home/popeye/testrm> perl -e "unlink grep {-s $_ == 0} glob ("/home/popeye/testrm/*.tmp");"



Error

Quote
Bareword found where operator expected at -e line 1, near "*.tmp"
(Missing operator before tmp?)
Bareword found where operator expected at -e line 1, near "/home/popeye"
(Missing operator before hrivida?)
Bareword found where operator expected at -e line 1, near "*.tmp"
(Missing operator before tmp?)
syntax error at -e line 1, near "*.tmp"
syntax error at -e line 1, near "/home/popeye"
Execution of -e aborted due to compilation error


List of test files

Quote
server:> /home/popeye/testrm> ls -l
total 4
-rw-rw-r-- 1 popeye 30 Nov 6 14:09 bike.tmp
-rw-rw-r-- 1 popeye 0 Nov 6 14:09 boat.tmp
-rw-rw-r-- 1 popeye 0 Nov 6 14:09 car.tmp
-rw-rw-r-- 1 popeye 0 Nov 6 14:09 house.tmp



FishMonger
Veteran / Moderator

Nov 6, 2013, 12:34 PM

Post #5 of 7 (170 views)
Re: [popeye] find files in the current directory that are zero and deleting them [In reply to] Can't Post

You have a quoting problem.

Try this:

Code
/home/popeye/testrm> perl -e 'unlink grep {-s $_ == 0} glob ("/home/popeye/testrm/*.tmp");'


BTW, the your find command will recurs a directory tree, but this perl command will not. If you need the recursion, then you'll want to use the File::Find or File::Find::Rule module.

http://search.cpan.org/~rjbs/perl-5.18.1/lib/File/Find.pm
http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm


popeye
Novice

Nov 6, 2013, 12:41 PM

Post #6 of 7 (168 views)
Re: [FishMonger] find files in the current directory that are zero and deleting them [In reply to] Can't Post

Dang!!!

I should have caught that. Works like a charm now. Thanks to all !

Pop


Laurent_R
Enthusiast / Moderator

Nov 6, 2013, 2:34 PM

Post #7 of 7 (161 views)
Re: [popeye] find files in the current directory that are zero and deleting them [In reply to] Can't Post

Yes, Fishmonger is right (and I also noticed) that your find command would recurse on subdirectories whereas the code I provided only deletes files in the current directory. But your post title was "find files in the current directory that are zero and deleting them", so I assumed that you really wanted to process only the current directory.

BTW, pipelining three lists operators the way I did it is relatively advanced Perl (quite easy, really, but not the basics either), I would not advise you to start from that if you want to learn the language.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives