
Here is a simple script which will perform Automatic Disk Log Cleanup for any Linux Environment. You just have to provide correct CRUNCHIFY_TMP_DIRS . When a disk has no free space left, all kinds of trouble can occur.
But before that let’s have some basic understanding of some important commands.
Step-1)
Check df -H command.
Use the df
command to display information about total space and available space on a file system.
The FileSystem parameter specifies the name of the device on which the file system resides, the directory on which the file system is mounted, or the relative path name of a file system.
If you do not specify the FileSystem parameter, the df command
displays information for all currently mounted file systems. If a file or directory is specified, then the df command displays information for the file system on which it resides.
Output:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on /dev/disk0s2 499G 114G 385G 23% 27868719 94059510 23% / devfs 189k 189k 0B 100% 640 0 100% /dev
Step-2)
Next filter out filesystem and find out the percentage of space
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'
Output:
23% /dev/disk0s2 100% devfs
So, sometimes programmatically you may want to cleanup files from specific folders in case you ran out of space. In that case, you just need to execute below script and it will take care of cleaning all unused files based on filter criteria mentioned in script. It also send out an email to user specified in script.
Another must read:
Complete Linux DiskCleanup Script:
#!/bin/bash # Diskclean-Linux.sh - Remove unused files from /tmp directories # @author: crunchify.com # ------------- Here are Default Configuration -------------------- # CRUNCHIFY_TMP_DIRS - List of directories to search # DEFAULT_FILE_AGE - # days ago (rounded up) that file was last accessed # DEFAULT_LINK_AGE - # days ago (rounded up) that symlink was last accessed # DEFAULT_SOCK_AGE - # days ago (rounded up) that socket was last accessed CRUNCHIFY_TMP_DIRS="/tmp /var/tmp /usr/src/tmp /mnt/tmp" DEFAULT_FILE_AGE=+2 DEFAULT_LINK_AGE=+2 DEFAULT_SOCK_AGE=+2 # Make EMPTYFILES true to delete zero-length files EMPTYFILES=false #EMPTYFILES=true cd /tmp/log "cleantmp.sh[$] - Begin cleaning tmp directories" echo "" echo "delete any tmp files that are more than 2 days old" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type f -a -ctime $DEFAULT_FILE_AGE \ -print -delete echo "" echo "delete any old tmp symlinks" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type l -a -ctime $DEFAULT_LINK_AGE \ -print -delete echo "" if /usr/bin/$EMPTYFILES ; then echo "delete any empty files" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type f -a -empty \ -print -delete fi echo "Delete any old Unix sockets" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type s -a -ctime $DEFAULT_SOCK_AGE -a -size 0 \ -print -delete echo"" echo "delete any empty directories (other than lost+found)" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth -mindepth 1 \ -type d -a -empty -a ! -name 'lost+found' \ -print -delete echo "" /usr/bin/logger "cleantmp.sh[$] - Done cleaning tmp directories" # send out an email about diskcleanup action mail -s "Disk cleanup has been performed successfully." you@email.com echo "" echo "Diskcleanup Script Successfully Executed" exit 0
Hope this helps. If you have any query on above script then let me know. Any suggestions are most welcome. The script works under Linux and Mac OS X.
Want to run above script every 3 days
? Just use below cron schedule 🙂
Detailed tutorial on Setting up CronJobs
is on it’s way. Please stay tuned.
0 0 */3 * * /opt/crunchify/crunchify_script.sh
First of all Thanks a lot for the perfect script. having following point, could you please help me here.
1. is it possible to create a log file and save all the details of deleted files in it.?
2. I saved the script as “CleanupNew. sh” and the location of temp dir I give is “/home/info/app/tmp” but script while executing giving output like below. how to mention the full dir path instead only “/tmp”
tbd01001$ sh CleanupNew.sh
CleanupNew.sh: line 15: /tmp/: Is a directory
delete any tmp files that are more than 90 days old
at line 15 I have following code
cd
/tmp "cleantmp.sh[$$] - Begin cleaning tmp directories"
3. instead deleting all the files, how to control and delete only specific files types like log, syslog, xml etc…
Thanks in advance.
Sud
Hi Sud. I’ll take a look at this in few days. Could you please share your modified script file and will help debug the same.
I tried to run the same script but got some error.
./Diskclean.sh: line 59: -depth: command not found
./Diskclean.sh: line 60: -type: command not found
./Diskclean.sh: line 61: -print: command not found
./Diskclean.sh: line 68: mail: command not found
PS: i am using ubuntu 16.04. Please help.
Hmm. Sorry but I’m not sure how to fix this. May be related to Dilip 🙁
I’ve just passed your query to our Crunchify forum here.
i am beginner in script.please help
i build a tmpfs in /var/log.
i want clean files in “log folder” every 2 days. automatic every 2 days.
how i can do it ? thank you
Hi Reza –
Try this and let me if you have any more questions:
0 0 */2 * * /opt/crunchify/crunchify_script.sh
I am getting /tmp: Permission Denied error when i run the script. Normally i have to use sudo to delete files under tmp folder. Where i have to make changes in script in order to solve the permission problem? It will be very helpful.
Hi Yum – Why /tmp folder need sudo permission? Ideally it shouldn’t.
what’s cleantmp. sh[$$] ??
Hi Alaa – it’s just a text. You could replace it with anything.
Hi, I want to automate the process of deleting content of a folder(var/www/apps/tmp/logs) every 12hrs. Can I also use the script provided here. Where do I place the script. thanks
Hi Biyi – yes. You could use the same script with minor modification. You may need to setup cronjob in your case. I’ll try to provide detailed tutorial on how to setup cron job on linux. Please stay tuned.
Thanks for the script . Can you also add dry run option?
Thanks Akshay for comment and stopping by. What do you mean by dry run option?
it mean to just print out what action you will be performing on files/resources and not actually performing the action, this way a consumer of code can see what is going to happen when he runs a piece of code.
Ok sure. will add dry run functionality during weekend.
Awesome script thank you! 🙂
I’m glad it helped you Tyler.