- #!/usr/bin/perl -w
- #
- # sqzfps.pl -- compress FPS batch files
- #
- # CJKim, 27-Jul-2016, Created
- #
- use strict;
- use File::Find ();
- use vars qw/*name *dir *prune/;
- *name = *File::Find::name;
- *dir = *File::Find::dir;
- *prune = *File::Find::prune;
- my $topdir = 'some-top-directory';
- my $aging = 90; # 365;
- my $cputhr = 50;
- my $cpuchk = 50;
- my $dircnt = 0;
- $| = 1;
- sub cpu_ok
- {
- $dircnt++;
- if ($dircnt > $cpuchk) {
- $dircnt = 0;
- while (1) {
- open CPU, 'vmstat 1 2 | awk \'{if (++c == 4) print $15;}\' |';
- my $idle = <CPU>;
- close CPU;
- $idle += 0;
- last if $idle > $cputhr;
- print "Sleeping for 30 seconds since CPU is at $idle% idle...\n";
- sleep 30;
- }
- }
- return 1;
- }
- sub wanted
- {
- # $_ is the file name only
- # $name is the full path
- # $dir is just the directory name
- # $prune may be set to 1 to stop traversing below the current dir
- my ($dev,$ino,$mode,$nlink,$uid,$gid);
- (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))
- && (-d _) # directory only
- && (int(-M _) > $aging) # so many days old
- && ($_ =~ /^\d{16}FF$/) # fits the name pattern
- && ($prune = 1) # stop traversing after this one
- && cpu_ok() # check if we have enough cpu available
- && system "(echo 'Processing $_'; cd $name && zip -m $dir/$_.zip *.* && cd / && rmdir $name)"
- ;
- }
- # traverse the filesystem
- File::Find::find({wanted => \&wanted}, $topdir);
Wednesday, July 27, 2016
Linux: Zipping Directories into Zipped Files Recursively
Here's a perl script to do it. The idea is from using find2perl, which can construct how perl would do Unix/Linux find command. Using it as a base, a simple script can be very powerful. Note the use of $prune. This is equivalent to find's -prune option. It is used to stop traversing below the current directory.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment