Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Delete files older than a certain number of days using find command  (Read 2451 times)

0 Members and 1 Guest are viewing this topic.

nirmal

  • Jr. Member
  • **
  • Posts: 56
  • Karma: +0/-0
Hi Guys,


Delete files older than 30 days
++++++   
find . -mtime +30 -exec rm {} \;
++++++

1. Save the deleted files to a log file
++++++
find /home/a -mtime +5 -exec ls -l {} \; > mylogfile.log
++++++
This way, you’ll get a line at top with the date the line is executed (also a line at the bottom).
The last two semicolons are necessary, one is for find and one for bash.


2. modified

Find and delete files modified in the last 30 minutes
++++++
find /tmp/ -type f -mmin 30 -exec rm {} \;
++++++

mtime = days
mmin = minutes

3. force

force delete temp files older then 30 days

++++++
find /tmp -mtime +30 -exec rm -f {} \;
++++++

4. move the files

move files older than 30 days to an archive folder – and preserve path strcuture

++++++
find /tmp -mtime +30 -exec mv -t {} /archive/directory/ \;
++++++

mv -t: ensure directory structure is preserved

Thank you for checking this :)
Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!