Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In linux shell, How to cp/rm files by time?

Tags:

linux

shell

time

cp

rm

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.

like image 609
herbertD Avatar asked Jul 27 '12 09:07

herbertD


People also ask

How delete multiple files by date in Linux?

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.

How do I delete files older than a certain date in Linux?

For just files find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete .


1 Answers

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).

like image 102
verdesmarald Avatar answered Sep 18 '22 20:09

verdesmarald