'Explaining the 'find -mtime' command
I'm trying to remove all the dateed logs except the most recent. Before I execute a script to remove the files, I want to of course test my commands to make sure I'm bringing up accurate results.
When executing these commands the date is:
Sep 1 00:53:44 AST 2014
Directory Listing:
Aug 27 23:59 testfile.2014-08-27.log
Aug 28 23:59 testfile.2014-08-28.log
Aug 29 23:59 testfile.2014-08-29.log
Aug 30 23:59 testfile.2014-08-30.log
Aug 31 23:59 testfile.2014-08-31.log
Sep 1 00:29 testfile.log
I thought -mtime +1 was supposed to list all files over a day old. Why isn't the 8-30.log one listed?
find . -type f -mtime +1 -name "testfile*log"
./testfile.2014-08-27.log
./testfile.2014-08-28.log
./testfile.2014-08-29.log
This is the desired effect, but it was just trial and error. What is this 0 saying?
find . -type f -mtime +0 -name "testfile*log"
./testfile.2014-08-30.log
./testfile.2014-08-27.log
./testfile.2014-08-28.log
./testfile.2014-08-29.log
Solution 1:[1]
+1 means 2 days ago. It's rounded.
Solution 2:[2]
To find all files modified in the last 24 hours use the one below. The -1 here means changed 1 day or less ago.
find . -mtime -1 -ls
Solution 3:[3]
#{user} is user-name
#name of script is 'place.{user}'
#used manually or from cron
#moves files that are created by automated job queue at night
for the user and identified by find into dated
subdirectories in user's home directory, so moves them
from"
/u/home/{user} to /u/home/{user}/2022/05/05 on the 5th of
May in 2022.
cd /u/home/{user}/
place=`date '+./%Y/%m/%d/'`;
find ./*.csv -mtime -.6 -exec mv {} $place \;
find ./*.txt -mtime -.6 -exec mv {} $place \;
find ./*.tab -mtime -.6 -exec mv {} $place \;
find ./*.pdf -mtime -.6 -exec mv {} $place \;
cd $place
chmod 666 ./*
chown {user} ./*
chgrp users ./*
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | txt |
| Solution 2 | blaucuk |
| Solution 3 |
