I have an ip cam that records videos in a folder structure like this:
├── 2023
│ ├── 10
│ ├── 11
│ │ ├── cam_00_20240921153706.mp4
│ │ ├── cam_00_20240921153928.mp4
│ │ ├── cam_00_20240921164743.mp4
│ │ └── cam_00_20240921230558.mp4
│ └── 12
└── 2024
├── 08
└── 09
├── 21
│ ├── cam_00_20240921153706.mp4
│ ├── cam_00_20240921153928.mp4
│ ├── cam_00_20240921164743.mp4
│ ├── cam_00_20240921230558.mp4
│ └── cam_00_20240921230646.mp4
└── 22
├── cam_00_20240922101839.mp4
└── cam_00_20240922102150.mp4
Now I would like to delete all files that are older than 2 days, and all empty folders. To do this, I did
#delete files older than 2 days
find /recordings/????/??/?? -depth -type d -mtime +2 -exec rm -rf {} \;
#delete empty month dir
find /recordings/????/?? -depth -empty -type d -mtime +2 -exec rm -rf {} \;
#delete empty year dir
find /recordings/???? -depth -empty -type d -mtime +2 -exec rm -rf {} \;
Can this be done with a one liner? Or in a nicer way?
find
has a-o
(or) parameter, that allows combining two commands:Explanation:
find /recordings/
- Search recursively starting from the/recordings/
directory.-type f -mtime +2 -exec rm -f {} \;
- Find files older than 2 days and delete them.-o
- OR operator to combine different find expressions.-type d -empty -exec rmdir {} \;
- Find empty directories and remove them.