среда, 14 июня 2023 г.

Linux Bash remove old files more than 10 if exists

if [ $(expr $(ls -1 /test/111 | wc -l | tr -d ' ')) -gt 10 ]; then rm $(ls -d -1 -At /test/111/* | tail -n $(expr $(ls -1 /test/111 | wc -l | tr -d ' ') - 10)) ; fi 

to crontab

/bin/bash -c "if [ $(expr $(ls -1 /test/111 | wc -l | tr -d ' ')) -gt 10 ]; then rm $(ls -d -1 -At /test/111/* | tail -n $(expr $(ls -1 /test/111 | wc -l | tr -d ' ') - 10)) ; fi"