To clean up cache and log files, I prefer to run following commands via terminal on daily basis (once a day, mostly in morning).

rm -rf ~/Library/Logs/* ~/Library/Caches/*
sudo rm -rf /Library/Logs/* /Library/Caches/*

I want to make a shortcut for above commands. I was thinking of putting them into a file (say, cleanup) where above and other similar commands are listed on seperate lines. Wheneven, I want to run above commands, it should be simpler command like run cleanup.

Is this possible? Also, can I put something like this when system starts (post booting)?

  • 1
    Setting this as something that can be done is quite easy using launchd. See this answer and this answer for more details. – Allan Jun 25 '18 at 5:18
  • I'm always interested in why people do things like this. Can you comment on what problem you think this is solving? – Marc Wilson Jun 25 '18 at 13:45
  • @MarcWilson for cleanup purpose ;-) I assume that operating systems perform few level of clean up on its own. One of my friend who deals in computer hardware suggested me to cleanup logs and cache files regularly. – I-M-JM Jun 27 '18 at 5:21
  • Take your friend out and beat him with a wet noodle. Seriously. The OS rotates logs. Any OS does. Or else it's broken, and then you fix it and move on. As for the idea that you need to delete what's in a cache... <sigh>. Does the word "counterproductive" mean anything to anyone? – Marc Wilson Jun 28 '18 at 14:05
  • Last comment... if whatever you're doing (for whatever value of "you", not just the OP), needs to have 'sudo' wrapped around it, think long and hard about whether you should be doing it in the first place. Seriously. Usually, the correct answer is 'no'. – Marc Wilson Jun 28 '18 at 14:07

You have a couple of options to do this....

Create a bash script.

rm -rf ~/Library/Logs/* ~/Library/Caches/
sudo rm -rf /Library/Logs/* /Library/Caches/*

Assuming you've named it "cleanup" first make sure you set it to be executable: chmod +x cleanup.

Then put it in one of the directories in your PATH by copying it or symlinking it: cp cleanup /usr/local/bin or ln -s ./cleanup /usr/local/bin/cleanup

Make an alias

alias cleanup="rm -rf ~/Library/Logs/* ~/Library/Caches/; sudo rm -rf /Library/Logs/* /Library/Caches/*"

Put that alias in your ~/.bash_profile so it's set every time you start a Terminal session.

Now that it's an alias, you can execute the command cleanup from Terminal and it will run those two commands.

  • Note the sudo will make the script ask for your password - better to set up the scripts in launchd. That will also allow you to automate when they run – Mark Jun 25 '18 at 11:59
  • @Allan - Thanks for your reply. When I checked contents of .bash_profile, I got following result --PATH=/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php7.1.8/bin:$PATH Should I put alias on new line? – I-M-JM Jun 27 '18 at 5:24
  • Yes. Just put it on a separate line either above or below your ~/.bash_profile – Allan Jun 27 '18 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .