1833187 Members
3262 Online
110051 Solutions
New Discussion

Deelete command

 
Ahmed_41
Super Advisor

Deelete command

Dear all

i need to delete some files that are older than a specific date, can someone tell me what is the command to do that ?

also what if these files are in folders ? and i want to remove these folders also that are older than specific date ?

Thanks in advance
3 REPLIES 3
Sean OB_1
Honored Contributor

Re: Deelete command

Check the man page for find.

Essentially something like this.

find . -mtime +N | xargs rm

This will search the current directory and subdirectories for files last modified N number of days ago and remove them.

A. Clay Stephenson
Acclaimed Contributor

Re: Deelete command

The key to what you are trying to do is the find command.

For example:

cd to desired starting directory
find . -type f -name 'myfile*' -mtime +30 -exec rm {} \;

This will remove all regular files (-type f) that match the pattern 'myfile*' that have not been modified in more than days.

I suggest that you replace the:
-exec rm {} \;
with a safer command such as:
-exec ls -l {} \;
until you get a better understanding of the find command.

Man find for details.
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor

Re: Deelete command

What you are asking here is very dangerous! There are many OS files that fall into this category.

DO NOT DO THIS SYSTEM WIDE!

You can use the find command to find files that are older than a certain number of days. BEWARE, some OS files will qualify in this condition.

DO NOT DO THIS SYSTEM WIDE!

Do it in a relatively safe area, such as the users /home directory.

# cd /home/
# find . -mtime +100 -exec rm {} \;
The rm -f will delete the directories.

Again, you do this command from the root directory and you will wipe out your system!

A final statement that should be a given, make sure you have good backups! If you do this command incorrectly and do not have good backups and an ignite tape, you have a dead system!

Again, much care is to be taken!

-