1829737 Members
1772 Online
109992 Solutions
New Discussion

Script to move old files

 
2ne1
Occasional Contributor

Script to move old files

I am not too familiar with script , if I want to do the housekeeping in a path as below , can advise what can I do ? Thanks


1) move the files ( /tmp ) elder than 90 days to a specfic path ( /tmp/bak )

2) remove the files elder than 180 days and gzip these remove files in /tmp/bak .

3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Script to move old files

Take a search the category for previous topics that are similar.

 

>1) move the files (/tmp) older than 90 days to a specific path (/tmp/bak)

 

find /tmp -mtime +90 -exec mv {} /tmp/bak \;

(If you want to use "-exec ... +, you'll need to write a script that reverse the parms to mv(1).)

2ne1
Occasional Contributor

Re: Script to move old files

Thx reply,

 

The command you provided is for request 1 , can advise if I would like to do the thing as point 2 , what can i do ? thx

Dennis Handly
Acclaimed Contributor

Re: Script to move old files

>The command you provided is for request 1, can advise if I would like to do the thing as point 2, what can I do?

 

The idea was that you would figure 2) out from the solution to 1).

 

find /tmp -mtime +180 -type f | while read filename; do

   gzip -c "$file" > "/tmp/bak/$file.gz"

   if [ $? -eq 0 ]; then

      rm -f "$file"

   else

      echo "Problem gzipping $file" 1>&2

   fi

done

 

If /tmp has subdirectories, there'll be problems.