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.