Solved I created a copy-paste macro to clean up your computer in a general way

Solved issue

PhantomShadow

New Member
Joined
Oct 6, 2020
Messages
15
Reaction score
3
Credits
256
Greetings to all my fellow nerds (please forgive me if i offend any mods with this post I am not well acquainted with this forum I do not intend to break any rules)

I am just starting to really get acquainted with the use of linux terminal so please forgive my crude macro but I thought I would share this concept with the community. I'm sure many of you will have suggestions or even improvements on this concept.

I am a big fan of STACER <link> https://oguzhaninan.github.io/Stacer-Web/ </link> (link to stacer homepage)

I also enjoy bleachbit <link> https://www.bleachbit.org/ </link> (bleachbit homepage)

that being said those two programs are only useful in cleanup in a GENERAL sense among the cleanup methods that are noob friendly,,,,,,,, I wanted to clean up my system a bit more so I created a crude copy-paste macro that works well with Ubuntu with minor adjustments it should work for almost anyone in other distros.

at the bottom of my copy-paste macro I also left info on resetting your network device because that is often the cause of all kinds of problems ..... without further adieu here is my crude macro that i hope can be useful to the community...........I simply have a text editor file that I copy-paste into terminal after using sudo apt-get clean to get the password out of the way.


sudo apt-get clean &&

sudo dpkg --configure -a &&

sudo apt-get autoclean &&

sudo apt-get autoremove --purge &&

sudo lshw -short -C memory &&

sudo resolvectl flush-caches &&

sync && sudo sysctl -w vm.drop_caches=3 &&

sync && sudo sh -c echo 3 | sudo sysctl vm.drop_caches=3 &&

rm -rf ~/.cache* &&

sudo apt-get -y remove --purge $(deborphan) &&

sudo rm -rf /tmp/* &&

sudo fstrim / &&

sudo journalctl --vacuum-time=1hr &&

sudo systemctl restart NetworkManager


------------------------------------------------------------

Resetting Network Devices in Linux

Resetting network devices in Linux can be done using a few simple terminal commands. Here are the most common methods:
Using Network Manager

For systems using Network Manager, you can restart the service with the following command:

sudo systemctl restart NetworkManager

This command will restart the Network Manager, which manages network connections.
Restarting Networking Service

For systems that do not use Network Manager, you can restart the networking service directly:

sudo /etc/init.d/networking restart

Alternatively, you can use:

sudo systemctl restart networking

Disabling and Enabling Networking

You can also disable and then enable networking using the Network Manager command-line tool:

nmcli networking off
nmcli networking on


Checking Network Status

After resetting, you may want to check the status of your network:

nmcli -o (for Network Manager)
sudo systemctl status networking (for traditional networking service)

These commands will help you reset your network device effectively.
 


Good on you.

Thanks for sharing.

You might explain a little more on whether you have assigned this to a keyboard combination as an alias or other, or if it is the content of a bash script or shell script.

I am moving this to Command Line subforum.

Wizard
 
This does seem to be rather Debian based, I doubt in will run on fedora/redhat/arch/suse systems. However it would be
easy to modify, in some cases (your networkManager example).. you need to run systemctl daemon-reload before the
changes take effect. Some of the are handled automatically but the OS and systemd daemons.
 
Respect for sharing something you built while you’re learning the terminal But I’d be careful recommending this as a general “cleanup macro” because a few lines are either unnecessary on Linux or can break things.

On Linux, caches are mostly a good thing. The kernel will drop RAM cache automatically when it needs memory, so forcing vm.drop_caches=3 usually just makes the system slower right after. I’d only ever do that for a very specific benchmark/test, not as maintenance.

The safe/useful parts here are basically the apt housekeeping:
  • apt clean/autoclean and apt autoremove --purge can free space and remove unused packages.
  • dpkg --configure -a is fine if an install was interrupted, but I wouldn’t run it every time “just because”.
The risky bits:
  • rm -rf ~/.cache* can match more than you think (anything starting with .cache…) and can wipe stuff you didn’t intend. If you really must clear cache, rm -rf ~/.cache/* is safer, but I still wouldn’t do it routinely.
  • sudo rm -rf /tmp/* can break running apps/services that are using /tmp. Most distros already clean /tmp automatically.
  • sudo apt-get remove --purge $(deborphan) is a bit of a foot-gun. deborphan can misidentify things, and purging them can remove packages you actually need. apt autoremove is the safer “normal user” way.
  • journalctl --vacuum-time=1hr is extremely aggressive. You’ll delete logs that are useful when something breaks. I’d use something like 7–30 days or a size limit.
NetworkManager restart and resolvectl flush-caches are fine troubleshooting steps, but they don’t belong in an “always run this” cleanup script.

If you want a beginner-friendly version that’s actually safe to paste, I’d keep it simple, like:
  • update/upgrade
  • autoremove
  • autoclean
  • maybe vacuum logs to 14d
Everything else should be “only when you’re fixing a specific problem,” not part of routine maintenance.
 
I admittedly am still a noob at bash commands I mainly put this together to solve MOST issues I have when the internet is being screwy or my system is lagging.

Because I have some family over seas I have quite a number of strange organizations checking on me and sometimes hacking my system.

I shared this as an idea so people can work on similar ideas and I could take suggestions...

I have a computer that is a glorified chrome-book until I get enough money to get a real main computer so I reset the memory every time it starts to lag or lock up so I put together a all in one copy and paste quick fix.
 
Just one point, apt-get and apt-cache were largely superseded back around 2016 by just apt for the majority of commonly used commands, so sudo apt-get update && sudo apt-get upgrade -y has become sudo apt update && apt upgrade -y but in many Debian distributions the old style still will work,
Also apt is more powerful than apt get/cache one example is,
the apt-get upgrade option cannot install new packages on its own, you need to tell it which to upgrade., Whilst the apt upgrade command will automatically update all dependencies and it can upgrade the Linux kernel.
 
this is exactly why I posed this to the community.........

how should I re-write the crude "macro" for the maximum effect with minimal danger to my system guys?

I put this together because I noticed while stacer and bleachbit and of course apt-get clean,apt-get autremove, and maybe apt-get dpkg..........were not quite cleaning my system completely so I thought I would just put together an entire list from all the "cleanup" terminal commands I could find.
 
.were not quite cleaning my system completely
These apps are designed to clean the operating system including the apps safely but not the data/files associated with them, Linux is not constructed in the same way windows are, it has what is called shared dependencies, snippets of code that if you remove them because you think you don't need them after uninstalling one app, you could wreck a totally different app you do use,at best or at worse completely brick the system
 
Using -y for upgrades is dangerous. I speak from hard experience. Before approving upgrades, make sure nothing essential is going to be removed. I never, ever use the -y option, because I've been bitten, and it hurt.
 
sudo apt-get clean &&

sudo dpkg --configure -a &&

sudo apt-get autoclean &&

sudo apt-get autoremove --purge &&

sudo lshw -short -C memory &&

sudo resolvectl flush-caches &&

sync && sudo sysctl -w vm.drop_caches=3 &&

sync && sudo sh -c echo 3 | sudo sysctl vm.drop_caches=3 &&

rm -rf ~/.cache* &&

sudo apt-get -y remove --purge $(deborphan) &&

sudo rm -rf /tmp/* &&

sudo fstrim / &&

sudo journalctl --vacuum-time=1hr &&

sudo systemctl restart NetworkManager
I wouldn't trust you or anyone else with many of these commands. Part of them are used only by noobs who have seen nothing but WSL before that.

1. The first 3 commands with apt: nothing "auto" has ever cleaned absolutely everything. I spent 4 years with Mint and I know that for a fact. The only REAL way to get rid of all the downloaded packages you don't need is: sudo rm -rv /var/cache/apt/*.
2. I don't see how displaying information about RAM helps with "cleaning up" the computer.
3. DNS cache cleaning is no longer needed, not for a very long time! The best you can achieve is your ISP to permanently or temporarily ban you, if they discovered you, bc with that command you could crash the DNS. And since we're on the network topic, let's jump to the network manager: restarting it won't do a thing! If you have internet problems, reboot the router! If no router and/or still problems, change your ISP.
3. Unless you have installed something veeery specific, in $USER there are always only two types of cache dirs: ~/.cache and ~/.ccache. Nothing else. Which means that the way you've written ~/.cache* with an asterisk is not necessary. Furthermore, the same command is wrong. You're deleting a directory in your own $USER which means you don't need the --force flag OR the ~ symbol. When you open terminal, by default it operates in your $USER dir (/home/username), which makes the ~/ parth of the path redundant. Also, whatever you wanna delete from ~/.cache, it will be deleted without forcing it (-f). If you want to keep the dir ~/.cache but delete only its content, then the proper command is rm -r .cache/* which will delete all files and subdirs in ~/.cache.
4. There's a whole odyssey with the so called orphaned packages but I won't get into that now - I don't wanna ruin the surprise. Speakin' of surprises, I'll leave it to the surprise for you to discover why I used BlitchBit for only a week before I forgot it even existed. Some surprises are better kept secret. :D
5. sudo rm -rf /tmp/* - another nonsense command. "--force" is not only dangerous in some situations, but it's also unnecessary when using "sudo" in front of the command.
6. fstrim is meant only for SSDs. If you try to apply that to a hard disk, the results can be between "it didn't work" and "nasty surprises". Also, using discard in fstab does a lot better job than trim.
7. It's not a good idea to delete logs, no matter how old they are. You never know when you may need a log, even if it's from 5 years ago. Logs are rarely larger than 1 MiB (often even that is too much for a log), so cleaning up a few MiB in total won't do you any good.
The only logs worth to delete are the steam logs and you'd have a good reason to delete them, only if DXVK/VKD3D hangs in an endless loop, usually caused by an internal error in the game which in turn makes the game log bigger than the game itself! And that game was 140+ GiB...

Next time when you copy-paste commands from the internet, thinkin' you found the golden fleece to make you look like a guru, at least take the time to read them and think whether they'd actually do any good.
 
I wouldn't trust you or anyone else with many of these commands. Part of them are used only by noobs who have seen nothing but WSL before that.

1. The first 3 commands with apt: nothing "auto" has ever cleaned absolutely everything. I spent 4 years with Mint and I know that for a fact. The only REAL way to get rid of all the downloaded packages you don't need is: sudo rm -rv /var/cache/apt/*.
2. I don't see how displaying information about RAM helps with "cleaning up" the computer.
3. DNS cache cleaning is no longer needed, not for a very long time! The best you can achieve is your ISP to permanently or temporarily ban you, if they discovered you, bc with that command you could crash the DNS. And since we're on the network topic, let's jump to the network manager: restarting it won't do a thing! If you have internet problems, reboot the router! If no router and/or still problems, change your ISP.
3. Unless you have installed something veeery specific, in $USER there are always only two types of cache dirs: ~/.cache and ~/.ccache. Nothing else. Which means that the way you've written ~/.cache* with an asterisk is not necessary. Furthermore, the same command is wrong. You're deleting a directory in your own $USER which means you don't need the --force flag OR the ~ symbol. When you open terminal, by default it operates in your $USER dir (/home/username), which makes the ~/ parth of the path redundant. Also, whatever you wanna delete from ~/.cache, it will be deleted without forcing it (-f). If you want to keep the dir ~/.cache but delete only its content, then the proper command is rm -r .cache/* which will delete all files and subdirs in ~/.cache.
4. There's a whole odyssey with the so called orphaned packages but I won't get into that now - I don't wanna ruin the surprise. Speakin' of surprises, I'll leave it to the surprise for you to discover why I used BlitchBit for only a week before I forgot it even existed. Some surprises are better kept secret. :D
5. sudo rm -rf /tmp/* - another nonsense command. "--force" is not only dangerous in some situations, but it's also unnecessary when using "sudo" in front of the command.
6. fstrim is meant only for SSDs. If you try to apply that to a hard disk, the results can be between "it didn't work" and "nasty surprises". Also, using discard in fstab does a lot better job than trim.
7. It's not a good idea to delete logs, no matter how old they are. You never know when you may need a log, even if it's from 5 years ago. Logs are rarely larger than 1 MiB (often even that is too much for a log), so cleaning up a few MiB in total won't do you any good.
The only logs worth to delete are the steam logs and you'd have a good reason to delete them, only if DXVK/VKD3D hangs in an endless loop, usually caused by an internal error in the game which in turn makes the game log bigger than the game itself! And that game was 140+ GiB...

Next time when you copy-paste commands from the internet, thinkin' you found the golden fleece to make you look like a guru, at least take the time to read them and think whether they'd actually do any good.
i think that was about what is said jsut with fewer words

 
Thanks for the warnings and imput guys I was mainly putting together a one & done copy and paste command to clean up what stacer didn't clean up and maintain my system constantly because I'm using a chrome book with very slow processor.
 


Follow Linux.org

Members online


Top