Delete large amounts of files

Script to remove large number of files (amount > ARG_MAX)

Create a script file with whatever name you want, let's say "removefiles.sh" and put in the following content:

#!/bin/bash
folderPath=$1
a=$(du -sk $folderPath | awk '{print$1}')
while [ $a -gt 0 ]; do
        for file in $(ls -p $folderPath | grep -v / | tail -1000)
        do
                rm -rf "${folderPath}/${file}"
        done
        echo "Last file deleted was: ${file}"
        if [ $a -le 50000 ]; then 
                a=$(ls $folderPath | wc -l)
                echo "the number of files remaning in ${folderPath} is ${a}"
        else
                a=$(du -sk $folderPath | awk '{print$1}')
                echo "the size of ${folderPath} is now ${a}"

        fi
done
    

Make the script executable

chmod +x removefiles.sh
    

Run the script the the folder argument in place

./removefiles.sh /path/to/folder/to/clear
    
Previous Next

Drop me a line

Whether you want to just say `Hi` or discuss a project or an idea, drop me a line and I will get back to you as soon as possible.