- #!/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.
Running the Same Command Periodically and Watch the Output
Have you ever find yourself typing the same command over and over again just to see what's been changing? On a standard Linux, there is a little-known utility called "watch". It can run a command repeatedly every so many seconds and shows you the output. But it goes one step further by showing the differences between the last run and current by highlighting the differences. Yes, it's a clunky command line tool but does a beautiful job of saving a lot of typing and figuring out what changed. See the man pages for watch(1) for the detail and the option flags.
I often used it with SQL*Plus to see the changes in database. For example, you can have a SQL statement in a file called, say, count.sql. You can invoke it using SQL*Plus but you will see the output only one time. To see the output periodically, you can invoke it with watch(1), e.g.
watch -d sqlplus scott/tiger@xe @count
Don't forget to have "EXIT" as the last command in count.sql so that sqlplus would exit after executing the SQL statements.
I often used it with SQL*Plus to see the changes in database. For example, you can have a SQL statement in a file called, say, count.sql. You can invoke it using SQL*Plus but you will see the output only one time. To see the output periodically, you can invoke it with watch(1), e.g.
watch -d sqlplus scott/tiger@xe @count
Don't forget to have "EXIT" as the last command in count.sql so that sqlplus would exit after executing the SQL statements.
Cloning a Directory Tree in Linux/Unix
Here's an old command that clones an entire directory tree in Unix or Linux. So far, the command appears to work on all variants of Unix (HP-UX, Solaris, AIX, RedHat, Ubuntu, CentOS, and Fedora):
$ cd to_the_directory_to_be_cloned
$ find . -depth -print | cpio -pdv destination_directory
While the above command clones all files, since find(1) offers many options, you can clone selected files. For example, if you wish to clone only java programs, specify the option "-name '*.java'" to do so.
$ cd to_the_directory_to_be_cloned
$ find . -depth -print | cpio -pdv destination_directory
While the above command clones all files, since find(1) offers many options, you can clone selected files. For example, if you wish to clone only java programs, specify the option "-name '*.java'" to do so.
Subscribe to:
Posts (Atom)