Last active
July 17, 2021 20:55
-
-
Save peeyushsrj/477f7f4f21b2634d3e1ed6443351986a to your computer and use it in GitHub Desktop.
Delete largest files with persistence linux script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# User input variables | |
max_files=20 #No of files to List | |
scandirs="./scan-dirs.txt" #list of directories separated by newline to scan | |
ignore_files="./ignore-files.txt" #non-deleted files path will be stored here to avoid re-appearing | |
# Program Tests | |
test -f $scandirs || touch $scandirs | |
test -f $ignore_files || touch $ignore_files | |
test -f /tmp/maxfiles_mid || touch /tmp/maxfiles_mid | |
# Main Program | |
truncate -s 0 /tmp/maxfiles_mid | |
for dir in $(cat $scandirs); do | |
# 1 print conveyer id | |
echo "Scanning $dir" | |
find $dir -type f -printf "%s\t%p\n" 2> /dev/null | grep -vFf $ignore_files | sort -nr | head -$max_files | tee -a /tmp/maxfiles_mid >> /dev/null | |
done | |
echo "\n\nLargest files along with their files sizes" | |
echo "==========================================" | |
cat /tmp/maxfiles_mid | sort -nr | grep -vFf $ignore_files | head -n $max_files | tee /tmp/maxfiles >> /dev/null | |
cat /tmp/maxfiles | |
echo "\n" | |
cat /tmp/maxfiles | awk '{$1=""; print $0}' | cut -c 2- | tee /tmp/maxfiles2 >>/dev/null | |
while IFS= read -r file; do | |
read -p "Delete file $file? [y]: " answer</dev/tty | |
if [ "$answer" = "y" ] ; then | |
echo $file | sed 's/ /\\ /g' | xargs rm | |
echo "Deleted: "$file | |
else | |
echo $file >> $ignore_files | |
fi | |
done < /tmp/maxfiles2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run it
Mention directory to scan in
./scan-dirs.txt
or $scandirs variable (line no.4)bash dpa.sh