Wednesday, March 17, 2021

 How to Find files in a specific range.

Find command is a faster way to retrieve the files. it is better than ls. ls is slower when the number of files increased. 

 Find the Files OLDER than X days on Linux

Command Syntax

find /path/to/files -mtime +<number of days> -printf '%T+ %p\n' [| sort -r | head -100 ]

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

Explanation 

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

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

3. -printf - This argument print files to console %T and %p  and \n is used for new lines.
Optional Part
4. | pipe - used to input-output of the first command to others.

5. sort - This command sort the output of the find command 
6. head - This command shows only a specified range of values.


Find Files created in the Last X days

Command Syntax

find /path/to/files -mtime -<number of days> -printf'%T+ %p\n' 

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

Find Files older than x minutes

Command Syntax

find /path/to/files -mmin +<minutes> -printf'%T+ %p\n' 

Delete files created in x minutes

Command Syntax

find /path/to/files -mmin -<minutes> -printf'%T+ %p\n' 

No comments:

Post a Comment