How Can I Create the Date modified of a File?
Now I am hare how create file using previous date. Mainly this is testing for remove previous file or folder older than seven days. So I am find on google many site but can’t exact solution. Finally I found this solution below.
- How To Add illustrator Checkbox Symbol
- How To Install php7.2 In Ubuntu 16.04
- Perhaps Misspelled or Defined by a Module Not Included in the Server Configuration
- Perhaps Misspelled or Defined by a Module Not Included in the Server Configuration
Step #01: Create file using old date below command
[[email protected]]# touch -a -m -t 201912180130.09 fileName-18.ext
[[email protected]]# touch -a -m -t 201912190130.09 fileName-19.ext
[[email protected]]# touch -a -m -t 201912200130.09 fileName-20.ext
[[email protected]]# touch -a -m -t 201912210130.09 fileName-21.ext
[[email protected]]# touch -a -m -t 201912220130.09 fileName-22.ext
[[email protected]]# touch -a -m -t 201912230130.09 fileName-23.ext
[[email protected]]# touch -a -m -t 201912240130.09 fileName-24.ext
[[email protected]]# touch -a -m -t 201912250130.09 fileName-25.ext
[[email protected]]# touch -a -m -t 201912260130.09 fileName-26.ext
Step #02: Check all file created date using ll or ls -l command.
[[email protected]]# ls -l
-rw-r--r-- 1 root root 0 Dec 18 01:30 fileName-18.ext -rw-r--r-- 1 root root 0 Dec 19 01:30 fileName-19.ext -rw-r--r-- 1 root root 0 Dec 20 01:30 fileName-20.ext -rw-r--r-- 1 root root 0 Dec 21 01:30 fileName-21.ext -rw-r--r-- 1 root root 0 Dec 22 01:30 fileName-22.ext -rw-r--r-- 1 root root 0 Dec 23 01:30 fileName-23.ext -rw-r--r-- 1 root root 0 Dec 24 01:30 fileName-24.ext -rw-r--r-- 1 root root 0 Dec 25 01:30 fileName-25.ext -rw-r--r-- 1 root root 0 Dec 26 01:30 fileName-26.ext
Step #03: Now delete older than seven days using command.
[[email protected]]# find /root/file_location/* -mtime +6 -exec rm -rf {} \;
Then check again after delete file older than seven days.
-rw-r--r-- 1 root root 0 Dec 20 01:30 fileName-20.ext -rw-r--r-- 1 root root 0 Dec 21 01:30 fileName-21.ext -rw-r--r-- 1 root root 0 Dec 22 01:30 fileName-22.ext -rw-r--r-- 1 root root 0 Dec 23 01:30 fileName-23.ext -rw-r--r-- 1 root root 0 Dec 24 01:30 fileName-24.ext -rw-r--r-- 1 root root 0 Dec 25 01:30 fileName-25.ext -rw-r--r-- 1 root root 0 Dec 26 01:30 fileName-26.ext
This is details of command.
-a = accessed -m = modified -t = timestamp - use [YY]MMDDhhmm[.ss] time format
Step #04: Now you create one bash file & include cronjob for every day run this command.
[[email protected]]# vi remove_olderthan_7.sh
#!/bin/bash find /root/file_location/* -mtime +6 -exec rm -rf {} \;
For press :wq then press Enter
[[email protected]]# chmod 755 remove_olderthan_7.sh
Step #05: Open cronjob file and add script file for run every day.
[[email protected]]# crontab -e
0 1 * * * /your_location/remove_olderthan_7.sh
For press :wq then press Enter