Skip to content

Instantly share code, notes, and snippets.

@peeyushsrj
Last active July 17, 2021 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peeyushsrj/477f7f4f21b2634d3e1ed6443351986a to your computer and use it in GitHub Desktop.
Save peeyushsrj/477f7f4f21b2634d3e1ed6443351986a to your computer and use it in GitHub Desktop.
Delete largest files with persistence linux script
# 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
@peeyushsrj
Copy link
Author

How to run it

Mention directory to scan in ./scan-dirs.txt or $scandirs variable (line no.4)

bash dpa.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment