2

I have a directory named repository which has a number of files and sub directories. I want to find the files and directories which have not been modified since last 14 days so that I can delete those files and directories. I have wrote this script but it is giving the directory name only

#!/bin/sh

M2_REPO=/var/lib/jenkins/.m2/repository
echo $M2_REPO
OLDFILES=/var/lib/jenkins/.m2/repository/deleted_artifacts.txt
AGE=14 

find "${M2_REPO}" -name '*' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
cloudbud
  • 39
  • 3
  • 8
  • 1
    Possible duplicate of [Shell script to delete directories older than n days](https://stackoverflow.com/q/13868821/608639) – jww Jul 20 '18 at 13:40

3 Answers3

2

find /path/to/files* -mtime +5 -exec rm {} \;

Note that there are spaces between rm, {}, and \;

Explanation

The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.

The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.

The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • Before executing rm command is there a way I can write the files name and directory names that will be deleted in a file – cloudbud Sep 22 '15 at 06:51
  • 1
    Use find /path/to/files* -mtime +5 > logfile for getting files only older than 5 days. – neo Sep 22 '15 at 06:58
1

You can give the find -delete flag to remove the files with it. Just be careful to put it in the end of the command so that the time filter is applied first.

You can first just list the files that the command finds:

find "${M2_REPO}" -depth -mtime +${AGE} -print

The -d flag makes the find do the search depth-first, which is implied by the -deletecommand.

If you like the results, change the print to delete:

find "${M2_REPO}" -mtime +${AGE} -delete
Edu
  • 2,017
  • 17
  • 23
  • It will print the dir as well as the files ? – cloudbud Sep 22 '15 at 06:59
  • Yes, it will print (nad delete) both. With `-type f` you can limit the results to just regular files. The manual page has then all the dirty details on symlink handling etc, if needed. – Edu Sep 22 '15 at 07:11
  • When I am executing the command, I am getting a warning find: warning: the -d option is deprecated; please use -depth instead, because the latter is a POSIX-compliant feature. – cloudbud Sep 22 '15 at 07:19
  • I updated my answer to use -depth, for which the -d is just a synonym. – Edu Sep 22 '15 at 07:37
  • This did not delete directories when I tried it, probably bcos deleting a file updates the mtime on the directory. – chrisinmtown Feb 10 '22 at 13:23
0

I know this is a very old question but FWIW I solved the problem in two steps, first find and delete files older than N days, then find and delete empty directories. I tried doing both in one step but the delete operation updates the modification time on the file's parent directory, and then the (empty) directory does not match the -mtime criteria any more! Here's the solution with shell variables:

age=14
dir="/tmp/dirty"
find "$dir" -mtime "+$age" -delete && find "$dir" -type d -empty -delete
chrisinmtown
  • 3,571
  • 3
  • 34
  • 43