While browsing the internet for Ubuntu articles, I came across this command:
sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
The author said that this is a single line command which will delete all the previous versions of Linux, leaving only the current one!
I'm actually looking for such a command, but I'm not so sure on how safe this is. I'd like to know:
- Whether it is safe to execute this command?
- How does this command work? i.e. explanation of small parts of such a big command
- If this command serves some different purpose, then what would be the correct command to achieve what the author claims it to do?
I become very confused and frustrated when I try to infer out all by myself. How does this command work for it contains numerous /
, |
, \
, *
, and ^
characters which are hard to Google for.
I am looking for a step by step translation & explanation for this command which I was unable to find across the internet!
apt-get autoremove
doesn't suggest any removals of older kernels for me. If I don't delete them, they'll just pile up till my/boot
is out of space and updates fail. Do you have a reference for it about that it should do this?s/^[^ ]* [^ ]* \([^ ]*\).*/\1/
instead of simplys/^[^ ]* \([^ ]*\).*/\1/
. The script is not very robust or elegant. For example why check for current kernel before extracting the package name from the output? As for an alternative,sudo apt-get autoremove --purge
purges most old kernels in some latest release of Ubuntu like Xubuntu 15.10.