Wednesday, March 17, 2021

 

Delete Files using Find Command on Linux

Note - > Before  Deleting files by using find command -exec option you must ensure that files are correct. How to find files in a specific range.

 Delete Files OLDER than X days on Linux

Command Syntax

find /path/to/files -mtimes +<number of days> -exec rm {} \;

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

Explanation 

1. path/to/files -  This argument takes the path where your files are located.

2. mtimes - This argument specify how older file you want to find. i.e. -mtimes +4 means - find the files which are older than 4 or more days.

3. -exec  - This command execute the argument passed to it. Here the argument is rm which remove the files. you can use -f option to forcefully remove the files. {} \; are mandatory this is the end of the command.

Delete Files created in Last X days

Command Syntax

find /path/to/files -mtimes -<number of days> -exec rm {} \;

To Delete files created in the last x days only change -mtimes argument it takes -<number of days> rather +<number of days>

Delete Files Older than x minutes

Command Syntax

find /path/to/files -mmin +<minutes> -exec rm {} \;

Delete files created in x minutes

Command Syntax

find /path/to/files -mmin -<minutes> -exec rm {} \;

No comments:

Post a Comment