Kevin
Kevin

Reputation: 2688

Retrieve Folder Older than Date Based On folder Name

I have a set of snapshots. Each snap shot resides in a accountname folder, and each snap shot is named with a date format as: YYYY-MM-DD-accountname

How can I retrieve the name of the "snap shot folder" where it is older than 2 days old? (The 2017-05-* directories)

Folder structure such as:

/home/snapshots
    /home/snapshots/account1
        /home/snapshots/account1/2017-05-01-account1
        /home/snapshots/account1/2017-05-02-account1
        /home/snapshots/account1/2017-05-03-account1
        /home/snapshots/account1/2017-05-04-account1
        /home/snapshots/account1/2017-05-05-account1
        /home/snapshots/account1/2017-05-06-account1
    /home/snapshots/account2
        /home/snapshots/account2/2017-05-01-account1
        /home/snapshots/account2/2017-05-02-account1
        /home/snapshots/account2/2017-05-03-account1
        /home/snapshots/account2/2017-05-04-account1
        /home/snapshots/account2/2017-05-05-account1
        /home/snapshots/account2/2017-05-06-account1

For instance... I want to list /home/snapshots/account1/2017-05-01 through /home/snapshots/account1/2017-05-04, given that today is 05/06/2017 (US), and vice-versa for account2

I thought find /home/snapshots/ -type d -mtime +2 -exec -ls -la {} \; may do the trick, but that returned me all folders older directories older than 2 days... and adding maxdepth 1 returned nothing...

Upvotes: 0

Views: 484

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84541

Continuing from my comment above, the reason you are having problems is you want to search within /home and then select and delete the snapshots directories found if they are more than two days old. With -execdir, it would be

find /home -type d -name "snapshots" -mtime +2 -execdir rm -r '{}' +

Let me know if you have problems. (also, there is no need to use ls -la within find, the -printf option provide you complete output format control without spawning a multiple separate subshells for each ls call, see man find)

note: you should quote '{}' to protect against filenames with whitespace, etc.

Edit

Sorry I misread your question, Obviously if you only want to delete the account* subdirectories of each snapshots directory, then the search path of /home/snapshots is fine and you then include the account*/*account* designator as @BroSlow correctly caught below.

Upvotes: 1

Related Questions