Linux Removing Folders Older Than 1 Year and More Than 3 Files

find and delete file or folder older than x days

You can make use of this piece of code

find /tmp/* -mtime +7 -exec rm {} \;

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 +7, it will find files older than 7 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.

Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

For deleting folders, after emptying inside of them you can rmdirinstad of rm in the piece of code, also if you only want to see directories you can add

-type d

to piece of code such as below:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;

Shell script to delete directories older than n days

This will do it recursively for you:

find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;

Explanation:

  • find: the unix command for finding files / directories / links etc.
  • /path/to/base/dir: the directory to start your search in.
  • -type d: only find directories
  • -ctime +10: only consider the ones with modification time older than 10 days
  • -exec ... \;: for each such result found, do the following command in ...
  • rm -rf {}: recursively force remove the directory; the {} part is where the find result gets substituted into from the previous part.

Alternatively, use:

find /path/to/base/dir/* -type d -ctime +10 | xargs rm -rf

Which is a bit more efficient, because it amounts to:

rm -rf dir1 dir2 dir3 ...

as opposed to:

rm -rf dir1; rm -rf dir2; rm -rf dir3; ...

as in the -exec method.


With modern versions of find, you can replace the ; with + and it will do the equivalent of the xargs call for you, passing as many files as will fit on each exec system call:

find . -type d -ctime +10 -exec rm -rf {} +

How to delete files and directories older than n days in linux

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

Delete all directories and its files older than a 60 days regardless of whether the directory is empty or not

Your issue is because find is deleting its search base directories before it has finished iterating these, so calling your rm -rf on already deleted entries.

This is easily fixed by adding the -depth option.

Also, you should really end the rm options with a double dash --, to prevent having arguments provided by the find command, to be interpreted as options arguments by the rm command.

find /mnt/games/codes/ -depth -mtime '+60' -type d -exec rm -rf -- {} \;

BASH Script - Delete folders older than X days except for some and all their subfolders/files

TL;DR Fix:

Instead of your line, do:

find /Path/Folder/* -type d -mtime +7 ! -path "/Path/Folder/NODELETE1"  ! -path "/Path/Folder/NODELETE2"  ! -path "/Path/Folder/NODELETE3" -exec rm -rf {} \;

Longer explanation:

The -type d part of your line searches for Directories only. But what you're excluding, are things inside your NODELETE directories. This still means that your NODELETE directories are targets for rm -rf and due to that they get recursively deleted.

Remove Files older than 3 years


Can I just multiply the number?

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

Yes. And to "echo" before you remove

find /path/to/files -mtime +1095 -print

Then the version with -exec rm {} \; to remove the files (when you are ready).



Related Topics



Leave a reply



Submit