Compressing & removing files in a directory & subdirectory

Hi,

I want a simple line of code that will compress files within a directory specified (parameter) and its subdirectories and also i want to remove files which are exactly 365 days old from the sysdate after this compression.
Please help.

Thanks,
JD

the following part of script will

  1. compress files older than 10 days , except already compressed files
  2. delete compressed files older than 365 days

To be adapted :

#
DIRECTORY=/mydir   #change it to $1 for instance
find $DIRECTORY -type f  ! -name '*.gz'  -mtime +10 -exec gzip {} \;
find $DIRECTORY -name '*.gz' -mtime +365 -exec rm {} \; 

Hi,
Here, we can use only one find command:

find $DIRECTORY -type f  ! -name '*.gz'  -mtime +10 -exec gzip {} \; -o -name '*.gz' -mtime +365 -exec rm {} \;

Regards.

1 Like

Use \+ if you are passing files to utilities which can handle multiple files.

This is valid in both gzip and cp, and you should gain performance with small change.

Also, files with .gz extension gzip will not attempt to compress, but rather print on stderr that file(s) already have gz extension and it will skip it.
Keep in mind that if fileXYZ.gz exists on same location where same fileXYZ is being compressed, gzip will prompt you for overwrite halting your program awaiting user input.

You really don't want to come to a machine after N days/months to see N crons with N finds awaiting user input..

Hope that helps
Regards
Peasant.

1 Like

The part after the -type f should be in (esccaped) brackets, otherwise it would attempt to rm an old *.gz directory.
Or this version that also eliminates a redundant -name '*.gz'

find $DIRECTORY -type f -mtime +10 \( ! -name '*.gz' -exec gzip -f {} \; -o -mtime +365 -exec rm {} \; \)

Ok, this will delete all files if +365 days old, including uncompressed ones (where gzip has failed). Rather a feature than a mistake.
The efficient + instead of the \; should work as well.

1 Like

Exact, if name of subdirectory is the name of file where to launch command then this file will delete.
So, We can just repeat -type f after -o connector:

find $DIRECTORY -type f  ! -name '*.gz'  -mtime +10 -exec gzip {} \; -o -type f -name '*.gz' -mtime +365 -exec rm {} \;

Regards.

Hi,

When i execute this line of code:

find $DIRECTORY -type f -mtime +10 \( ! -name '*.gz' -exec gzip -f {} \; -o -mtime +365 -exec rm {} \; \)

i get no error, but the files do not get compressed.
Can anyone help.

Thanks,
Jess

There is -mtime +10 that requires the files to be >10 days old.
Omit that so it will compress all files!

The operators between ( ) are only for filters , not for actions.
See finally my original solution should fit.
You can enhanced it this way

# gzip -q suppresses useless warnings
find /mydir  -type f    -mtime +10 -exec gzip -q {} +
find /mydir  -name '*.gz' -mtime +365 -delete

Regarding performance and security on this topic, you shoud have a look at this page on gnu Finding Files