LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete file less than 23hrs old (https://www.linuxquestions.org/questions/linux-newbie-8/delete-file-less-than-23hrs-old-4175594046/)

trickydba 11-22-2016 12:33 PM

Delete file less than 23hrs old
 
From command line, how do I delete a file less than 23 hrs old so a new file can come in? The file is a txt file

TB0ne 11-22-2016 12:43 PM

Quote:

Originally Posted by trickydba (Post 5633445)
From command line, how do I delete a file less than 23 hrs old so a new file can come in? The file is a txt file

Read the man page on the find command.
Code:

find /some/path -name <pattern> -type f -mtime -1 -delete
...will delete a file less than 1 day old.

trickydba 11-22-2016 12:44 PM

Perfectamuno!!! Thank you!!! Dang I love this site!! FAST REPLIES!!!!!!

trickydba 11-22-2016 12:45 PM

Pattern of course is the file name right?

szboardstretcher 11-22-2016 12:47 PM

You will use 'find' to find files of a certain age. The paramaters -mmin (number of minutes) or -mtime (number of days) is what you will be looking for.

What you have LITERALLY asked for, is to find a file that is LESS than 23 Hours. Which means that this will find and delete the file if it were made in the last 1 minute, 2 minutes, 3 minutes, 4 minutes, 5 minutes and so on up until 23 hours and delete them. That can be done with this (change the 'ls' to 'rm' when you are certain it is deleteing what you want it to):

Code:

find . -type f -name 'TESTING.txt' -mmin -1380 -exec ls {} \;
Explained here: http://explainshell.com/explain?cmd=...+%7B%7D+%5C%3B

trickydba 11-22-2016 12:52 PM

Sorry, should have said less than 24 hours

szboardstretcher 11-22-2016 12:54 PM

No problem. Looks like tb0ne had you covered there. Take notice of the difference in the commands though. They might come in handy.

trickydba 11-22-2016 12:54 PM

You all are AWESOME!!!!!!

Sefyir 11-22-2016 09:44 PM

Using -mmin and -mtime matches files modified at a certain time.
If you are continually updating this file, it will never become "older" as the modified time will always reflect the most recent change.
In fact, you may discover if you download a file it will have a very old modified time (eg from March 2006)

I believe there are some instances of recording the creation date, but these should be considered unreliable.

In most cases, using the modified date as a match for "oldness" works fine, but you should be aware of what it is matching. Read the manual pages (as suggested) to fully understand the commands you are using.

Code:

      -mmin n
              File's data was last modified n minutes ago.

      -mtime n
              File's  data was last modified n*24 hours ago.  See the comments for -atime to
              understand how rounding affects the interpretation of file modification times.


trickydba 11-28-2016 08:06 AM

I love this site!!!! OPTIONS OPTIONS OPTIONS!!!! You guys/gals are great!!! Thank you for all your help!!!


All times are GMT -5. The time now is 05:55 AM.