banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Wiki Pick: Getting Rid Of Unwanted/Unneeded Files


Relevant to all versions of PCLinuxOS


This page is the results of the questions asked about how to remove unneeded/unwanted files on the PCLinuxOS forum.

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


Removing Unwanted/Unneeded files

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 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 '*.[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

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.


WikiPick
Image by Olena from Pixabay

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.

Editor’s Note: You can take this one step further, and create a bona fide bash script that checks to make sure the user is launching it as the root user (and exits if not), and then executes the commands. Then, you can also build a .desktop file for it, and place that desktop file on your desktop or in a launcher on your panel. That way, all you have to do is to enter the root password when prompted.
Here’s one possibility that I threw together in less than a couple of minutes:
#! /bin/sh

# MUST BE RUN AS ROOT USER
if [ "$UID" != "0" ]; then
zenity --warning --width=275 --title "Privilege Error" --text "You must run this program as the root user."
exit 0
fi

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

zenity --info --width=275 --title “Docleaning” --text “Docleaning has finished cleaning up unwanted/unneeded files.”

exit 0

The script checks to see if you’re the root user. If not, it exits before any of the commands are executed. The command to start the script should read something like “pkexec docleaning.sh”, provided that you store the script in a directory that is in your system’s $PATH statement. Otherwise, the command to start the script will most likely read something like “pkexec [path-to-script]/docleaning.sh”.


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

(Tip within a tip: each new line in the cron job listing starts with @daily. If it starts with @daily, then that signifies a new line in the file. This will help discern each individual line, and not where the text is wrapped. You can also replace @daily with @weekly, or any other timeframe recognized by cron.)

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 buffers. 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, daily.


You can view this PCLinuxOS Knowledgebase Wiki article here.



Previous Page              Top              Next Page