script to compress files in directory that changes its name every day

Hi all

I have the following script that should compress a file in a directory:

# compress log files older than 2 days
find /u01/easydone/DBDUMPS/*.dmp -mtime +2 -exec gzip {} \;
 

BUT the problem is that these files that I want to compress are inside a directory with following format:

/u01/easydone/DBDUMPS #ls -lrt
total 6
drwxr-xr-x   2 easydone   easydone      1024 Mar 25 22:22 2012-03-25
drwxr-xr-x   2 easydone   easydone      1024 Mar 26 22:21 2012-03-26
drwxr-xr-x   2 easydone   easydone      1024 Mar 27 22:21 2012-03-27

So the *.dmp files that I want to compress are inside these subdirectories.

Can you help?

Regards

I think find will automatically traverse through the sub-directories and gzip the files as necessary.

You will need to tweak your find command a little. You are currently looking in the directories given by expanding /u01/easydone/DBDUMPS/*.dmp but I fancy that actually your files are called *.dmp

What you need is something more like this:-

find /u01/easydone/DBDUMPS -type f -name "*.dmp" -mtime +2 -exec gzip {} \;

The explanation is:-

  1. Start searching from /u01/easydone/DBDUMPS
  2. Find only files
  3. Only match files that are in the format *.dmp (use " to prevent expansion on the command line)
  4. Only match files that were last updated over two days ago
  5. For each match, execute the gzip command with the file as a parameter.

I hope that this helps. If you have sub-mounted filesystems that you do not want to search or there could be symbolic links that you don't want to follow off to other filesystems, use the -xdev flag too (see the man page for info)

Robin
Liverpool/Blackburn, UK

Robin:

The files are in this format directory

/u01/easydone/DBDUMPS/2012-03-27

and today will be created a "2012-03.28" directory.
The *.dmp is the extension of the files I want to compress:

/u01/easydone/DBDUMPS/2012-03-27 #ls -lrt
total 12936488
-rw-r--r--   1 easydone   easydone      1991 Mar 27 22:00 SESSION-2012-03-27-22-00.log
-rw-r--r--   1 easydone   easydone     41984 Mar 27 22:00 SESSION-2012-03-27-22-00.dmp
-rw-r--r--   1 easydone   easydone      8629 Mar 27 22:21 EDIM-2012-03-27-22-00.log
-rw-r--r--   1 easydone   easydone   2586354688 Mar 27 22:21 EDIM-2012-03-27-22-00.dmp
-rw-r--r--   1 easydone   easydone     10531 Mar 27 23:25 SFEC-2012-03-27-22-21.log
-rw-r--r--   1 easydone   easydone   4036493312 Mar 27 23:25 SFEC-2012-03-27-22-21.dmp
mcel-dunn02[253]/u01/easydone/DBDUMPS/2012-03-27 #

But when I do a ls for testing:

]/u01/easydone/DBDUMPS #find /u01/easydone/DBDUMPS -type f -name "*.dmp" -mtime +1 -exec ls -lrt {} \;
-rw-r--r--   1 easydone   easydone   2580219904 Mar 25 22:22 /u01/easydone/DBDUMPS/2012-03-25/EDIM-2012-03-25-22-00.dmp
-rw-r--r--   1 easydone   easydone   4033813504 Mar 25 23:35 /u01/easydone/DBDUMPS/2012-03-25/SFEC-2012-03-25-22-22.dmp

But if I do:

/u01/easydone/DBDUMPS #find /u01/easydone/DBDUMPS -type f -name "*.dmp" -mtime +2 -exec ls -lrt {} \;
mcel-dunn02[256]/u01/easydone/DBDUMPS #

I got nothing.
What I really want to compress the previous day files..

That would suggest two things. Either:-
There are no files

  1. older that two days
  2. The files are already compressed and therefore no longer match the ".dmp" criteria, they are likely ".dmp.gz" instead.

Can you confirm? In either case, it's telling you that there is nothing to do.

If you are still concerned, can you post the output from:-

cd /u01/easydone/DBDUMPS
find . -print | sort | xargs ls -ld

Thanks, in advance,
Robin

its:

mcel-dunn02[256]/u01/easydone/DBDUMPS #find . -print | sort | xargs ls -ld
drwxrwxrwx   5 easydone   easydone      1024 Mar 27 23:25 .
drwxr-xr-x   2 easydone   easydone      1024 Mar 28 11:45 ./2012-03-25
-rw-r--r--   1 easydone   easydone   2580219904 Mar 25 22:22 ./2012-03-25/EDIM-2012-03-25-22-00.dmp
-rw-r--r--   1 easydone   easydone      8629 Mar 25 22:22 ./2012-03-25/EDIM-2012-03-25-22-00.log
-rw-r--r--   1 easydone   easydone      6181 Mar 25 22:00 ./2012-03-25/SESSION-2012-03-25-22-00.dmp.gz
-rw-r--r--   1 easydone   easydone      1991 Mar 25 22:00 ./2012-03-25/SESSION-2012-03-25-22-00.log
-rw-r--r--   1 easydone   easydone   4033813504 Mar 25 23:35 ./2012-03-25/SFEC-2012-03-25-22-22.dmp
-rw-r--r--   1 easydone   easydone     10531 Mar 25 23:35 ./2012-03-25/SFEC-2012-03-25-22-22.log
drwxr-xr-x   2 easydone   easydone      1024 Mar 26 22:21 ./2012-03-26
-rw-r--r--   1 easydone   easydone   2585207808 Mar 26 22:21 ./2012-03-26/EDIM-2012-03-26-22-00.dmp
-rw-r--r--   1 easydone   easydone      8629 Mar 26 22:21 ./2012-03-26/EDIM-2012-03-26-22-00.log
-rw-r--r--   1 easydone   easydone     41984 Mar 26 22:00 ./2012-03-26/SESSION-2012-03-26-22-00.dmp
-rw-r--r--   1 easydone   easydone      1991 Mar 26 22:00 ./2012-03-26/SESSION-2012-03-26-22-00.log
-rw-r--r--   1 easydone   easydone   4035786752 Mar 26 23:18 ./2012-03-26/SFEC-2012-03-26-22-21.dmp
-rw-r--r--   1 easydone   easydone     10531 Mar 26 23:18 ./2012-03-26/SFEC-2012-03-26-22-21.log
drwxr-xr-x   2 easydone   easydone      1024 Mar 27 22:21 ./2012-03-27
-rw-r--r--   1 easydone   easydone   2586354688 Mar 27 22:21 ./2012-03-27/EDIM-2012-03-27-22-00.dmp
-rw-r--r--   1 easydone   easydone      8629 Mar 27 22:21 ./2012-03-27/EDIM-2012-03-27-22-00.log
-rw-r--r--   1 easydone   easydone     41984 Mar 27 22:00 ./2012-03-27/SESSION-2012-03-27-22-00.dmp
-rw-r--r--   1 easydone   easydone      1991 Mar 27 22:00 ./2012-03-27/SESSION-2012-03-27-22-00.log
-rw-r--r--   1 easydone   easydone   4036493312 Mar 27 23:25 ./2012-03-27/SFEC-2012-03-27-22-21.dmp
-rw-r--r--   1 easydone   easydone     10531 Mar 27 23:25 ./2012-03-27/SFEC-2012-03-27-22-21.log
mcel-dunn02[257]/u01/easydone/DBDUMPS #