I need to delete files under directory except latest 2.
I have prepared command to list according to date and delete files. It work when I run manually from command line , however it does not work in crontab.
In crontab,
* * * * * /bin/ls -t /mytest | /usr/bin/tail -n +2 | /usr/bin/xargs rm --
This command works when I run this command from commandline.
Also tried to add command in bash script then called that script from crontab but it did not work again.
How can I run that command via crontab?
crontab only expects one command. If multiple commands are to be executed, they can be bundled using bash -c
* * * * * bash -c 'ls -t /mytest | tail -n +2 | xargs rm --'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With