1

I'm using IBM AIX Unix and it's limited with command option. I want to implement below filter criteria using find and mtime (which works) option. I would like to remove the files listed with below filter condition.

  • files starting with number [0-9]
  • ending with ".dat"
  • and duration is between 1st Aug 2018 -15th Oct 2018

I was trying with find command but it didn't help much.

 find /mydirectory -type f -mtime -45 -mtime +5 
  • 1
    Are relative dates ok, as you've started with? They depend on you running the command on a particular day. – Jeff Schaller Oct 22 '18 at 9:52
2

That find command looks for things that are files AND things last modified less than 45 days ago AND things last modified more than 5 days ago. (The "AND" restricts the dataset, not enlarges it. Read it as "that are also" if you prefer.)

Assuming you've matched your absolute date range, you now just need to add a clause that completes the set of criteria you've specified:

find /mydirectory -type f -mtime -45 -mtime +5 -name '[0-9]*' -name '*.dat'

Optimising a little, these two naming criteria

  • files starting with digit [0-9]
  • ending with ".dat"

can be merged into a single statement:

  • files matching the glob ("shell wildcard pattern") [0-9]*.dat

Finally, you need a "remove the matching files" action, which we can implement with rm -f, and this then leads us to your required solution.

find /mydirectory -type f -mtime -45 -mtime +5 -name '[0-9]*.dat' -exec echo rm -f {} \;

Remember to remove echo from that command when you see it's selecting files as you expect, so that the rm will be actioned rather than just printed.

1

If the dates are fixed, meaning you want to delete files with modification dates within that range regardless of when you run the command, then you could use touch command to create reference files and find's -newer predicate to limit the results:

touch -t 201807312359.59 /tmp/oldest-file
touch -t 201810152359.59 /tmp/newest-file
find /mydirectory -type f -newer /tmp/oldest-file ! -newer /tmp/newest-file -name '[0-9]*.dat' -exec rm {} \;

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.