Ruby implementation of a powerful bulk delete file script sharing _ruby topic

Source: Internet
Author: User
Tags glob

Recently packaged servers have increased the number of APK packets, each manual RM operation too cumbersome, so spent a few minutes to write a can be specified in the directory according to the last modified time and wildcard matching for bulk deletion of the script. After you add this script to the crontab, you'll never have to worry about the extra installation pack taking up disk space.

Short Code

Copy Code code as follows:

#!/usr/bin/env Ruby
# Encoding:utf-8
#Usage: Ruby removeoldfiles.rb "Dest_file_pattern" Days_ago
Destfilepattern= Argv[0]
daysago= Argv[1]
Edentime = time.now.to_i-daysago.to_i * 86400
dir[destfilepattern].each{|child|
System "RM-RFV #{child}" if (File.mtime (child). To_i < Edentime)
}

How to use

It's easy to use and the rules are as follows

Copy Code code as follows:

Ruby removeoldfiles.rb "Dest_file_pattern" Days_ago

For example, for example, we want to delete the apk file in the/tmp directory, all last modified 3 days ago, we only need to do this.

Copy Code code as follows:

Ruby Removeoldfiles.rb "/tmp/*.apk" 3

Why the first argument uses double quotes

The first argument is the path that contains the wildcard character, and there is a tool in the shell where Glob will match the path that contains the wildcard character to the specific file, such as a piece of code.

Copy Code code as follows:

!/usr/bin/env Ruby
# Encoding:utf-8
Puts Argv.length
Argv.each do |a|
Puts "Argument: #{a}"
End

We pass in the path parameter containing the wildcard character, and the result is the Glob matching file name (if wildcard characters can be matched to the file).

Copy Code code as follows:

10:41 $ ruby test.rb *.txt
2
Argument:abc.txt
Argument:def.txt

To avoid glob operations, you need to use double quotation marks for path parameters that contain wildcard characters.

Copy Code code as follows:

10:41 $ ruby test.rb "*.txt"
1
Argument: *.txt

So the first parameter must use double quotes when using the script.

How to traverse a file that contains subdirectories within a subdirectory

For example, we want to traverse/tmp/abc/def.txt, we can use/tmp/**/.txt.

Related Article

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.