Linux: Delete Files Older Than X Days

This is a very simple tutorial how to find and delete files older than X days. I needed this for a where i collected some images and after a while they took too much space.

With this you will be able with the find to find your JPG files older then 30 days and then execute rm on them.

Delete command

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +30 is for files older then 30 days.
  • Fifth part -exec executes a command. In this case rm is the command, {} gets the filelist and \; closes the command

Move command

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec mv {} /path/to/archive/ \;

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +30 is for files older then 30 days.
  • Fifth part -exec executes a command. In this case mv is the command, {} gets the filelist, path where to move the files and \; closes the command

Combine commands

Now we can combine this two command to archive the images older than 10 days and delete them from the archive if they are older then 30 days.

We are going to create a shell script that will do that and we can run it with a .

#!/bin/bash
/usr/bin/find /path/to/files/ -type f -name '*.jpg' -mtime +10 -exec mv {} /path/to/archive/ \;
/usr/bin/find /path/to/archive/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

Linux or – Find and remove file syntax

The find command syntax is:

find dir-name criteria action

  1. dir-name : – Defines the working directory such as look into /tmp/
  2. criteria : Use to select files such as “*.sh”
  3. action : The find action (what-to-do on file) such as delete the file.

To remove multiple files such as *.jpg or *.sh with one command find, use:

find . -name "FILE-TO-FIND" -exec rm -rf {} \;

OR

find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

The only difference between above two syntax is that the first command remove directories as well where second command only removes files. Options:

  1. -name "FILE-TO-FIND" : File pattern.
  2. -exec rm -rf {} \; : Delete all files matched by file pattern.
  3. -type f : Only match files and do not include directory names.

Examples of find command

WARNING! These examples may crash your computer if executed. Before removing file makes sure, you have of all-important files. Do not use rm command as user it can do critical damage to the system.

Find all files having .bak (*.bak) extension in the current directory and remove them:
$ find . -type f -name "*.bak" -exec rm -f {} \;

Find all core files in the / (root) directory and remove them (be careful with this command):
# find / -name core -exec rm -f {} \;

Find all *.bak files in the current directory and removes them with confirmation from user:
$ find . -type f -name "*.bak" -exec rm -i {} \;
Sample outputs:

rm: remove regular empty file `./data0002.bak'? y
rm: remove regular empty file `./d234234234fsdf.bak'? y
rm: remove regular empty file `./backup-20-10-2005.bak'? n

This command should work on most of the Linux distributions.

More on findhttp://www.linuxmanpages.com/man1/find.1.php

Print Friendly, PDF & Email

Comments

comments

Bài viết liên quan

Be the first to comment

Để lại lời nhắn