Thursday 14 May 2015

Deleting all large size files from all directories using Bash in Linux

If you are looking to delete all the files in Linux Web Server by specifing File Name and File Size,
then follow steps below:

1. Login into your server Terminal via root login.
2. Create a new bash file called clean_up.sh in one of the location
e.g. /home/my_script/

3. Then Copy and the below code and Paste into that file and Save it.
clear
echo ' -------------------------------------------------------------------------'
echo '  Cleaning all large size error_log files from all directories.'
echo ' -------------------------------------------------------------------------'

filename="error_log"
filesize="50000"

echo " Deleting all files (including subfolders) having Filename:'$filename', Size: greater than $filesize Kb only."
echo ' '
echo ' Listing all the files which are greater than 50MB'
find / -type f -size +50M -exec du -h {} \; | sort -n

echo ' Deleting all the "error_log" files which are greater than 50MB'
#find -name "error_log" -size +30000k #-delete
find -name "$filename" -size +"$filesize"k -delete

echo ' Listing again all the files which are greater than 50MB'
find / -type f -size +50M -exec du -h {} \; | sort -n

4. Then go to your specified directory where you want to Search and Delete the files by specifying the File Name and File Size as below;

5. Then run the below command
sh  /home/my_script/clean_up.sh

No comments:

Post a Comment

Please post any queries and comments here.