A powerful, free, and open-source way to clean and speed up a macOS system directly from the terminal is by using Mole. It is a command-line tool that automatically removes application leftovers, clears caches, and reclaims disk space, making it a popular free alternative to paid optimization tools by saving the following into a .sh file
chmod +x mac_cleaner.sh
sudo ./mac_cleaner.sh
sudo ./mac_cleaner.sh --all
sudo ./mac_cleaner.sh --dry-run