banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Wiki Pick: Getting Rid Of Unwanted/Unneeded Files


Editor’s Note: Wiki Pick is a new monthly column highlighting one article from the PCLinuxOS Knowledge Base Wiki every month. Whenever possible (and when known), we’ll attribute the Wiki Pick article to the PCLinuxOS user who made the Wiki post. The Wiki cannot survive and thrive without the efforts of PCLinuxOS members contributing and keeping it updated. So, visit and contribute to YOUR PCLinuxOS Knowledge Base Wiki!

This ONLY involves cleaning old logs and other system stuff that is not needed.


Wiki PCLinuxOS Knowledge Base

These files can be found in the /var directory and are log and cache files. There are several ways to get rid of these unwanted and unneeded files.

The first way is to remove them by hand.

Open a console window and su to root.

Now you can simply enter the following lines one at a time, pressing enter after each line.

rm -rf /var/cache/cups/job*
rm -rf /var/cache/fontconfig/*
rm -rf $(find /var/spool -type f)
rm -rf $(find /var/lib/spool -type f)
rm -f $(find /var/log -type f -iname '*.old')
rm -f $(find /var/log -type f -name '*.gz')
rm -f $(find /var/log -type f -iname '*.[1-9]')
cat /dev/null |tee $(find /var/log -type f -iname '*log')
cat /dev/null |tee /var/log/dmesg
cat /dev/null |tee /var/log/explanations
cat /dev/null |tee /var/log/messages
cat /dev/null |tee /var/log/wtmp
cat /dev/null |tee /var/log/ConsoleKit/history

DO NOT just simply delete files in your /var/log folder, as some of these files are indeed needed by the system while it is running.

This is a list of some of the files that are required by the system while running dmesg, explanations, messages, wtmp and history.

You will note in the above list they start with the command cat /dev/null >. This command DOES NOT delete the file. It simply “empties” the contents of the file. Again, DON'T delete these files.

The above is not only time-consuming and error-prone, but it takes a lot of time to enter and execute each and every line.

Forum user “footstep11” brings a bit better solution by saving the commands to a filename called docleaning. To do this, simply copy the following lines and save them in your home directory as a file called docleaning.

rm -rf /var/cache/cups/job*
rm -rf /var/cache/fontconfig/*
rm -rf $(find /var/spool -type f)
rm -rf $(find /var/lib/spool -type f)
rm -f $(find /var/log -type f -iname '*.old')
rm -f $(find /var/log -type f -name '*.gz')
rm -f $(find /var/log -type f -iname '*.[123456789]')
cat /dev/null |tee $(find /var/log -type f -iname '*log')
cat /dev/null |tee /var/log/dmesg
cat /dev/null |tee /var/log/explanations
cat /dev/null |tee /var/log/messages
cat /dev/null |tee /var/log/wtmp
cat /dev/null |tee /var/log/ConsoleKit/history

Now to run this file, open a console window and su to root. At the prompt, type in the following command and then press enter: sh ./docleaning.

While this is somewhat better, you still have to open a console, change to the root user and type in some code. I don't know about you, but I just don't remember all of the possible commands that are required all the time.


Wiki PCLinuxOS Knowledge Base
Image by OpenClipart-Vectors from Pixabay


Now comes CRON to the rescue

The nice part is we only need to set it up once and then let cron do all the work, and we can forget about it.

Here's what we need to do to set it up.

Copy the following lines:

@daily rm -rf /var/cache/cups/job*
@daily rm -rf /var/cache/fontconfig/*
@daily rm -rf $(find /var/spool -type f)
@daily rm -rf $(find /var/lib/spool -type f)
@daily rm -f $(find /var/log -type f -iname '*.old')
@daily rm -f $(find /var/log -type f -name '*.gz')
@daily rm -f $(find /var/log -type f -iname '*.[123456789]')
@daily cat /dev/null |tee $(find /var/log -type f -iname '*log') && cat /dev/null |tee /var/log/dmesg && cat /dev/null |tee /var/log/explanations
@daily cat /dev/null |tee /var/log/messages && cat /dev/null |tee /var/log/wtmp && cat /dev/null |tee /var/log/ConsoleKit/history

Save them as a file called root to /var/spool/cron. You will need to be the root user to save this new file.

  1. Open a console window, become the root user.

  2. Open nano (IE: nano /var/spool/cron/root).

  3. Past the above lines into nano.

  4. To save the file, press ctrl + x.

  5. Answer Y when asked to Save modified buffer. Then press enter.

Your new file has been saved. You will need to set the permissions for your new file. Again, as root in the console window, type in the following at the command prompt: chmod 600 /var/spool/cron/root.

Press enter. That's it… You can now close the console window.

Now cron will do the job of cleaning your files automatically.

Editor's Note #1: You can use EITHER [123456789] *or* the regular expression [1-9]. Both will work here.

Editor's Note #2: You can also save the docleaning file above from footstep11 as docleaning.sh (don't forget to insert the bash shebang as the first line), and then just copy or move that script (you'll have to do this as the root user) into /etc/cron.daily to run under cron every day, or /etc/cron.weekly to have cron run the script once every week. Be sure to mark the file as being executable.

You can view the Wiki entry here.



Previous Page              Top              Next Page