How to traverse all files under the specified directory under Linux and delete files created before the specified number of days

Source: Internet
Author: User

The script reads as follows:

#!/bin/Bashfunctiondelete_file{dir=$1 Days=$[$2-1] forIinch`Find$dir-type F-ctime +$days ' Do  RM-RF $i Done}
whileRead Line Dodir=`Echo$line |awk '{print $}'' days=`Echo$line |awk '{print $}'' Delete_file $dir$days Done<file. txt

Where the function delete_file is used to delete the Dir directory in the file created before days.

The following while the read line statement is used to read the contents of file.txt lines by row.

File.txt content is similar to:

Dir1 1

Dir2 2

DIR3 1

...

That is, delete files created 1 days before the Dir1 directory, files created 2 days ago in the Dir2 directory, files created 1 days before the Dir3 directory, and so on.

How to traverse all files under the specified directory under Linux and delete files created before the specified number of days

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.