0

I will write some context for my problem.

I have to deal with a process that runs everyday that creates files in three secuential steps: A, B and C. Files from steps A and C are stored in one folder (X) while files from step B are stored in a different one (Y). Both folders are backed up to a different machine (I do not have control over this) once the process finishes. The content of the folder X is as follows:

t_11254an_luvo251n_B.bak    # A 'last modified at Feb  8 20:00'
t_11254aw_n7uo455w_B.bak    # C 'last modified at Feb  8 20:24'
t_11254av_j5ux8n2s_B.bak    # C 'last modified at Feb  8 20:25'
t_11254as_lvuo4e1j_B.bak    # A 'last modified at Feb  9 19:12'
t_11254at_m0uo4525_B.bak    # A 'last modified at Feb  9 19:12'
t_11254ak_m2uo4e1j_B.bak    # A 'last modified at Feb  9 19:12'
t_11254am_m1uo4e1j_B.bak    # A 'last modified at Feb  9 19:12'
t_11254am_m4uo4e4e_B.bak    # A 'last modified at Feb  9 19:13'
t_11254am_nauo4nhj_B.bak    # C 'last modified at Feb  9 21:54'
t_11254ai_n9uo4njs_B.bak    # C 'last modified at Feb  9 21:54'
t_11254ah_n7uo4nvw_B.bak    # C 'last modified at Feb  9 21:54'
t_11254aj_n8uo4njs_B.bak    # C 'last modified at Feb  9 21:54'
t_11254ak_ncuo71pv_B.bak    # A 'last modified at Feb 10 19:01'
t_11254ak_nduo7148_B.bak    # A 'last modified at Feb 10 19:01'
t_11254ay_nguo72t2_B.bak    # A 'last modified at Feb 10 19:03'
t_11254am_niuo71t5_B.bak    # A 'last modified at Feb 10 19:03'
t_11254av_onuo7991_B.bak    # C 'last modified at Feb 10 21:08'
t_11254at_omuo79tt_B.bak    # C 'last modified at Feb 10 21:08'
t_11254at_okuo7991_B.bak    # C 'last modified at Feb 10 21:08'

All files from step C have only one thing in common: they are always created after 20:15. Nor the name nor the size make them different from the files created on step A.

I run a script before the process starts to delete all files from folder X but I would like to keep the files created on step C (from all days) to speed up recoveries. The end result I am looking for is this:

t_11254aw_n7uo455w_B.bak    # C 'last modified at Feb  8 20:24'
t_11254av_j5ux8n2s_B.bak    # C 'last modified at Feb  8 20:25'
t_11254am_nauo4nhj_B.bak    # C 'last modified at Feb  9 21:54'
t_11254ai_n9uo4njs_B.bak    # C 'last modified at Feb  9 21:54'
t_11254ah_n7uo4nvw_B.bak    # C 'last modified at Feb  9 21:54'
t_11254aj_n8uo4njs_B.bak    # C 'last modified at Feb  9 21:54'
t_11254av_onuo7991_B.bak    # C 'last modified at Feb 10 21:08'
t_11254at_omuo79tt_B.bak    # C 'last modified at Feb 10 21:08'
t_11254at_okuo7991_B.bak    # C 'last modified at Feb 10 21:08'

The questions, then, are:

  • Is it possible to find files between two hours, without specifying a date, using find?

  • If not, is there a simple, one-liner way to do it in bash?

Thank you.

3
  • Not with a POSIX find, but all modern find implementations support find dir -mtime -2h
    – schily
    Feb 11 '20 at 13:44
  • To what time in files are you referring? The last modification time? The creation time? Feb 11 '20 at 13:56
  • both GNU and BSD date(1) can do that most probably. Of course find will be more efficient.
    – Jetchisel
    Feb 12 '20 at 8:12
0

Thanks to Jetchisel's answer I have been able to find the solution:

   #!/bin/bash

   for file in /folder/X/*.bak; do
     MODIFIED_DATE=$(stat --format="%y" $file | awk '{print $2}')

     if [[ $MODIFIED_DATE < '20:15' ]]; then
       printf 'rm -rf %s\n' "$file"
     fi
   done

stat --format="%y" shows the last modified date in this format 2020-02-02 19:11:34.000000000 +0100. Using awk I pick the time, which is the information I need, and with that I am able to tell if the file was created before 20:15 and thus delete it. It is not a clean one-liner but at least works :).

1
  • If you're on GNU, then yes that stat will work. jfyi BSD stat has a different syntax, my solution will should work on both, but I'm glad you found a solution you're looking for.
    – Jetchisel
    Feb 13 '20 at 11:47
0

One way with bash, files in between 20:00 and 22:00

#!/usr/bin/env bash

for f in /folder/x/*.bak; do
  var=$(date -r "$f" '+%H:%M')
  if [[ $var > '20:00' && $var < '22:00' ]]; then                                                                                                                                          
    echo "$f"
  fi
done
  • The above code should match your files.
  • Just change to [[ $var < '20:15' ]] If that's just what you're after.
  • That date syntax works on both GNU and BSD date(1)

EDIT: Updated answer

2
  • Hi, I think I did not properly write the question. The 'Feb 10 19:01' is not part of the name of the file, is the date it was last modified. I will change the information on the question to reflect that. Anyway, thanks to your answer I have been able to reach to the solution I was searching. It is not a one-liner, but cool anyways.
    – inivyro
    Feb 13 '20 at 11:11
  • I have updated the answer. but I have not tested it so I just put printed the files. (if it matches a file...)
    – Jetchisel
    Feb 13 '20 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.