Setting File Modification Time
The stat command displays detailed timestamp information for a file:
stat /opt/test.conf
To create a file or update its modification time, use touch with the -m and -d options:
# Create or update modification time to July 7, 2020
touch -m -d "2020-07-07 00:00" /opt/abc.txt
If the file doesn’t exist, it will be created as a empty file with the specified modification time. If it exists, only the modification timestamp is updated.
Verification via stat shows:
最近更改:2020-07-07 00:00:00.000000000 +0800
Finding Files by Modification Time
Use find with the -mtime option to locate files based on their last modification time:
-mtime +N: files modified more than N days ago-mtime -N: files modified within the last N days (including to day)
Example: find all .txt file modified more than 3 days ago:
find /path/to/search -name "*.txt" -mtime +3
To find .txt files modified within the last 3 days:
find /path/to/search -name "*.txt" -mtime -3
Searching Files by Size
The find command also supports size-based filtering using -size:
-size Nk: exactly N kilobytes-size -Nm: less than N megabytes-size +Ng: greater than N gigabytes
Valid units include k (kilobytes), M (megabytes), and G (gigabytes).
Example: find files larger than 5MB in /opt:
find /opt -size +5M
Creating Files of Specific Sizes with dd
The dd utility can generate files filled with zeros of a precise size:
# Create a 1MB file
dd if=/dev/zero of=a.txt bs=1M count=1
# Create a 5MB file
dd if=/dev/zero of=b.txt bs=5M count=1
# Create a 10MB file
dd if=/dev/zero of=c.txt bs=10M count=1
Here:
if=/dev/zeroreads from a special device that outputs null bytesof=filenamespecifies the output filebssets the block sizecountdefines how many blocks to copy
File sizes can be verified with:
ls -l # shows size in bytes
ls -lh # shows human-readable sizes (e.g., 1.0M, 5.0M)