I am trying to remove files within a bash script.
I can list the latest file with:
ls | grep core | tail -n 1
and rename it like
keep-core-2994-xyz.bin, then remove the rest with:
rm -f core-*
and then rename the latest to the original. In this way, I can keep the latest file and remove the rest. This works fine for this scenario:
-rw------- 1 root root 47M Sep 3 2017 core-2994-xyz.bin -rw------- 1 root root 47M Sep 3 2017 core-3012-xyz.bin -rw------- 1 root root 79M Sep 3 2017 core-3106-xyz.bin
However, this one:
core-10000-xyz.bin causes trouble:
-rw-r--r-- 1 root root 55M Sep 3 2017 core-10000-xyz.bin -rw------- 1 root root 47M Sep 3 2017 core-3012-xyz.bin -rw------- 1 root root 79M Sep 3 2017 core-3106-xyz.bin
... because this time, I need to keep the one I want with
head, not with
Is there any effective way to remove files except the latest one which were created by this order? (Here,
The file timestamps are unreliable; this is an embedded device and timestamps can change due to the internet connection, so I can't list them by timestamp.
This is an embedded device and I don't want to compile it with zsh; I'd like a bash-only solution, please.