Delete files that were created before today

16

find with -mtime +1 returns files that were modified two or more days ago.

It is a documented behavior i.e. -mtime ignores any fractional portion. From man find (Time calculation is identical in -atime, -mtime and -ctime):

-atime n

File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

Now if you can use couple of solutions:

  1. You can do:

    find . -type f -mtime +0
    

    This would get you the files modified one day or more ago.

    In your case:

    find $HOME -type f -name "myFile_*.sql.zip" -mtime +0
    
  2. To get precision like this case, you should use -mmin option which strictly matches minutes.

    So lets say you want to get the files that were modified more that 7 hours and 10 minutes i.e. 430 minutes ago, you can do:

    find . -type f -mmin +430
    

    In you case, do the calculation and replace 430 with that value:

    find $HOME -type f -name "myFile_*.sql.zip" -mtime +430
    
  3. Another option is to do it the reverse way.

    You can create a reference file e.g. ref.txt:

     touch -d 'today + 2 days ago' ref.txt
    

    Now you can find files that were modified more recently than ref.txt:

    find . -type f -newer ref.txt
    

    In your case:

    find $HOME -type f -name "myFile_*.sql.zip" -newer ref.txt
    

Once you are satisfied with the results, you can remove the file(s) e.g.:

find . -type f -mmin +430 -delete
Share:
16

Related videos on Youtube

Ryan Bruno
Author by

Ryan Bruno

Updated on September 18, 2022

Comments

  • Ryan Bruno
    Ryan Bruno over 1 year

    I am trying to turn my mean values into percentages. So instead of .64 on the y axis, for example, I would like it to display 64%. This might be why my error bars in this graph are gargantuan. Thanks in advance!

    Here is the graph now: enter image description here

    Here is my current code:
        Adult_Remote_Data = subset(Master_Data, Master_Data$Study != "Pilot Adults" & Master_Data$Study != "Children In Person" & Master_Data$Study != "Children Remote")
        Adult_Remote_Data %>%
          group_by(Condition, Gate = fct_inorder(Gate), Sound) %>%
          summarize(mean = mean(Correct),
                    sd = sd(Correct), .groups = "drop") %>%
          ggplot(aes(x = Gate, y = mean, color = Sound, group = Sound)) +
          geom_line() +
          geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd)) +
          facet_wrap(~Condition) +
          theme_minimal()
    
    Here is a reproducible sample of the dataset:
    set.seed(42)
    n <- 100
    dat <- data.frame(Participant=1:n, 
                      Study=rep(LETTERS[1:2], n/2),
                      Condition=rep(LETTERS[25:26], n/2),
                      Gate= sample(1:5, n, replace=TRUE),
                      Sound=rep(LETTERS[3:4], n/2),
                      Correct=sample(0:1, n, replace=TRUE)
    
    • Steam gamer
      Steam gamer over 8 years
      Try this command: find $HOME -iname '*.sql.zip' -atime +1
    • Tung Tran
      Tung Tran over 8 years
      IMO, your command should have worked if you had 1 file modified 1 day before. Check modified date of that again to make sure.
    • user1260928
      user1260928 over 8 years
      it doesn't work, is there a way to tell 'find' to look for the date that is shown when doing ls -al ? (i don't know if it is the creation/modification date)
    • Tung Tran
      Tung Tran over 8 years
      Well, any chance your file was created more than 24 hours before? According to man find: -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. I assume your file created at 1 am yesterday but now it's 3 am today, so the output your command would be correct. In this case, use -mtime +2 instead.
    • user1260928
      user1260928 over 8 years
      when id do ls -al, I am getting the date for this file : 'Dec 9'. I assume that -mtime +1 means : files that have been created at least one day ago. Is that right? If so, I should see this file with the find command. But I can only see a file with the date : 'Nov 20'
    • user1260928
      user1260928 over 8 years
      interesting thing : if I do find $HOME -iname "*.sql.zip" -mtime +0 , I can see this file. But its date (when doing a ls -al) is Dec 9.