Automatic desktop cleanup

Author: | Posted in howto No comments

Desktop Cleanup was the name of an old Windows XP utility used to remove unwanted shortcuts from the desktop. Somewhat useful considering that most Windows installers create desktop shortcuts by default, but extremely limited as it didn’t support other file formats. In the Linux world such an application would be pretty much worthless, but there are [more powerful] ways to deal with unorganized files such as those from ~/Desktop.

I work with several Linux desktop machines (both physical and virtual) daily and I tend to be a little messy when it comes to saving files downloaded via web browser or securely copied from remote locations. My thinking is “I’ll put these on the desktop as they are important and I need to find them easily”. Or course I find many things important so I end up having a full desktop pretty quickly.

One simple way to clean up the Linux desktop periodically is to move or delete unnecessary files automatically during boot. This can be done by adding specific commands to /etc/rc.d/rc.local. I’m using this setup for a while now on several CentOS and Fedora machines which I manage to keep clean and organized.

 

Samples

 

  • ┬ásearch the desktop for links (shortcuts to applications) and move them to a specific folder
find /home/lsc/Desktop/ -maxdepth 1 -type f -name "*.desktop" -exec mv -t /home/lsc/Desktop/shortcuts {} \+
  • search for documents and move them to a folder
find /home/lsc/Desktop/ -maxdepth 1 -type f -regextype egrep -regex '.*.(txt|doc|docx|xls|xlsx|pdf)' -exec mv -t /home/lsc/Desktop/docs {} \+
  • search for image files and move them accordingly
find /home/lsc/Desktop/ -maxdepth 1 -type f -regextype egrep -regex '.*.(jpg|jpeg|png)' -exec mv -t /home/lsc/Desktop/pics {} \+
  • search one level of directories deeper and move all files that were accessed more than 10 days ago
find /home/lsc/Desktop/ -maxdepth 2 -type f -atime +10 -exec mv -t /home/lsc/Desktop/archive {} \+
  • search for files that were modified more than 30 days ago and delete them
find /home/lsc/Desktop/ -maxdepth 1 -type f -mtime +30 -exec rm -rf {} \;

If you put any of these sample commands (customized to suit your needs and environment) in /etc/rc.d/rc.local they will be executed automatically during the boot process. Note that some distributions using systemd may not have this file, so you might want to try creating a systemd service or even using crontab as an alternative. Luckily, Red Hat 7 and CentOS 7 do keep this file for compatibility reasons, only one extra step is required to make it executable.

chmod +x /etc/rc.d/rc.local

Hope this helps!

Add Your Comment