You get a bonus - 1 coin for daily activity. Now you have 1 coin

Unix: How to Find, Delete, or Copy Files Older or Newer Than a Given Time

Practice



Task: find and do something with files that were modified longer ago or earlier than a specified number of days.

For example, we need to delete all files older than 30 days. Or we need to copy all files that were modified no more than 30 days ago.

For this we'll use the find utility.


Find files older than N days:
$ find . -mtime +N
where instead of N specify the number of days, for example:
$ find . -mtime +30

Find files younger than N days:
$ find . -mtime -N
as an example:
$ find . -mtime -45

In these examples we're working with the current directory, indicated by the "." dot.



Delete files older than 30 days

$ find . -mtime +30 -exec rm {} \;


Delete files younger than 30 days

$ find . -mtime -30 -exec rm {} \;


Copy files older/younger than 30 days

$ find . -mtime +30 -exec cp {} /target/dir/ \;
where instead of /target/dir/ specify the directory to copy into.


Move files older/younger than 30 days

$ find . -mtime +30 -exec mv {} /target/dir/ \;
where instead of /target/dir/ specify the directory to copy into.


And everything else works the same way.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "LINUX operating system"

Terms: LINUX operating system