In linux shell, When I run
ls -al -t
that show the time of files.
How to cp/rm files by time? just like copy all the files that created today or yesterday. Thanks a lot.
The syntax of this is as follows. -mtime +XXX – replace XXX with the number of days you want to go back. for example, if you put -mtime +5, it will delete everything OLDER then 5 days. -exec rm {} \; – this deletes any files that match the previous settings.
For just files find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete .
Depending on what you actually want to do, find
provides -[acm]time
options for finding files by accessed, created or modified dates, along with -newer
and -min
. You can combine them with -exec
to copy, delete, or whatever you want to do. For example:
find -maxdepth 1 -mtime +1 -type f -exec cp '{}' backup \;
Will copy all the regular files in the current directory more than 1 day old to the directory backup
(assuming the directory backup
exists).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With