5

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.

herbertD
  • 10,657
  • 13
  • 50
  • 77
  • check out this http://stackoverflow.com/q/11448885/1007273 should be helpful – hovanessyan Jul 27 '12 at 09:21
  • 1
    @herbertD : Note that the time your `ls` shows is the **modification** time, not the creation time. Look at the `find` command, in particular to the options `-mtime` and `-newer`. – user1934428 Feb 17 '20 at 12:53

2 Answers2

8

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

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
7

Simple Example

find /path/to/folder/ -mtime 1 -exec rm {} \; // Deletes all Files modified yesterday

For more examples google for bash find time or take a look here

donald123
  • 5,638
  • 3
  • 26
  • 23