First of all, please excuse my poor english.
I am trying to write a shell script to clean up some files of certain type in my backup folders.
I need to remove files of certain type, except 10 newest in each folder.
Folder structure looks like this:
Root folder
│
├── folder_1
│ ├── file_1.txt
│ └── file_2.txt
│ └── file_n.txt
│
├── folder_2
│ ├── file_1.txt
│ └── file_2.txt
│ └── file_n.txt
│
├── folder_n
│ ├── file_1.txt
│ └── file_2.txt
│ └── file_n.txt
│
I've used script from David Foerster as a basis, but can't figure out on how to make it work in different folders separately without manually writing each folder name in script.
Currently, script looks like this:
find /volume1/rootfolder/ -type f -name *.txt -printf '%T@ %p\0' |
sort --zero-terminated --reverse --numeric-sort --field-separator=' ' --key 1,1 |
gawk -F ' ' -v RS='\0' -v ORS='\0' -v retain_count=10 \
'BEGIN{ maxage = systime() - retain_younger_days * 24 * 3600; }
(NR > retain_count) && (int($1) < maxage) { print(substr($0, length($1) + 2)); }' |
xargs -r0 -- rm --
But the issue is that it removes all files in all folders, except 10 latest. So, overall i have just 10 files in all folders, not 10 in each folder.
Please, help me figure out, how to make script to process each folder separately.
zsh, which has glob qualifiers for selecting files based on modification time. See for example Bash script: Conditionally delete older files while keeping latest copies or Remove all files within directory older than the most recently added ten files – steeldriver Nov 17 '21 at 14:06may someone with both/all 3 cards in hand bump into the question someday and give me/us the answer :)
– brezniczky Nov 18 '21 at 14:11