Problem description
Often swim in the ocean of Linux files, often need to find all the files after a certain time. Maybe you're packing them, or maybe you're just checking.
The question is, what do you do if you want to write a script find-newer and put it on the server?
target
With a command like this, you can find all files in. / WP content directory after 19:00 on January 27, 2018!
find-newer 201801271900 ./wp-content -type f
preparation
- A Shell environment
- A folder with many files
Problem solving process
Execute the following command to create an empty script called find newer
touch /usr/local/bin/find-newer
Make scripts executable
chmod a+x /usr/local/bin/find-newer
Edit it, copy the content of the script below and save it.
/usr/local/bin/find-newer
#!/bin/bash
function the_help() {
cat <<EOF
USAGE
$0 TIMESTAMP FIND_OPTS
OPTIONS
TIMESTAMP [[CC]YY]MMDDhhmm[.SS]
FIND_OPTS OPTIONS of find command, except -newer
EXAMPLES
$0 201801182100 . -type f
tar cf files-newer-20180118T2100.tar \$($0 201801182100 . -type f | tr '\\n' ' ')
EOF
exit 1
}
function find_newer() {
local ts=$1; shift
local ts_file=/tmp/$ts.ts
touch -t "$ts" $ts_file || the_help
find "$@" -newer $ts_file
rm $ts_file
}
find_newer "$@"
want a go
xiaoqiang@remotehost:/var/www/html$ find-newer 201801271900 ./wp-content -type f
./wp-content/themes/startupkit/functions.php
./wp-content/themes/startupkit/index.php
./wp-content/themes/startupkit/acf/acf-zh.json
./wp-content/themes/startupkit/acf/acf-en.json
Indeed, this is the document I just revised (after 19:00 on January 27, 2018). Gaga.
If you want to see help, just type find newer.
xiaoqiang@remotehost:/var/www/html$ find-newer
touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]
USAGE
/usr/local/bin/find-newer TIMESTAMP FIND_OPTS
OPTIONS
TIMESTAMP [[CC]YY]MMDDhhmm[.SS]
FIND_OPTS OPTIONS of find command, except -newer
EXAMPLES
/usr/local/bin/find-newer 201801182100 . -type f
tar cf files-newer-20180118T2100.tar $(/usr/local/bin/find-newer 201801182100 . -type f | tr '\n' ' ')
Principle of interpretation
Create a file with a specified timestamp first
touch -t TIMESTAMP TIMESTAMP_FILE
Where the format of TIMESTAMP [[CC]YY]MMDDhhmm[.SS]
Using this timestamp file as a reference, use the - newer timestamp file of the find command to specify that the file to be found is newer than timestamp file.
find SOMEWHERE -newer TIMESTAMP_FILE
More
Uh, uh, uh