Skip to content

Instantly share code, notes, and snippets.

Created February 1, 2015 22:37
Show Gist options
  • Save anonymous/ad51dc25290413239f6f to your computer and use it in GitHub Desktop.
Save anonymous/ad51dc25290413239f6f to your computer and use it in GitHub Desktop.
Scan a directory for files over a certain size, print the size and path to the file
#!/bin/bash
# 01/31/15 - 06:12:06 A.M.
echo -e "\ndrops-size is shell script that scans Dropbox and finds files I have shared that
are too large and need to be looked at for potential deletion\n\n"
# Variables that are used often enough to need setting them, yup, cool comment
# Directory where we are scanning files
DEST=/Users/$USER/Dropbox/Public/drops
SIZE="+250k" # The size as defined in the find man page, getting all files larger…
TEMP="/tmp/drops-output.txt"
#Clean up the temp storage location
echo -e "START — Cleaning up the temp file and making a new empty one"
/bin/rm "$TEMP"
/usr/bin/touch "$TEMP"
echo -e "DONE — Cleaning up the temp file and making a new empty one"
# Change to the directory where the work will be done
echo -e "\nChanging to: ${DEST}: "
echo "where files will be scanned, printed by size, "
echo "and stored in a temporary file."
# Perform the actual `cd` change
# Change to the directory where the drops files are that I want to work on
echo -e "cd to DEST: ${DEST}\n"
cd "${DEST}"
# Conditional check: is the current directory the one I want to be the working dirctory?
if [ "$(pwd)" = "${DEST}" ]; then
echo -e "Destination and current working directory are equal, this is good!:\n $(pwd)\n"
fi
# Use `find` to locate a subset of files that are larger than a certain size
# save that to a temp file and process it. I believe this could all be done in
# one find command with -exec or similar but I can't figure it out
find . -type f -size "${SIZE}" -exec ls -lh {} \; >> "$TEMP"
# Number of results located as a result of the find `command` above
RESULTS=$(wc -l "$TEMP" | awk '{print $1}')
echo -e "Located: [$RESULTS] total files greater than or equal to $SIZE\n"
# With a result set found via `find`, now use awk to print out the sorted list of file
# sizes and paths.
echo -e "SIZE DATE FILE PATH"
#awk '{print "["$5"] ", $9, $10}' < "$TEMP" | sort -n
awk '{for(i=5;i<=NF;i++) {printf $i " "} ; printf "\n"}' "$TEMP" | sort -n
if [ -f "$TEMP" ]; then
echo -e "\n$TEMP exists, and should have the backup data in it in case we need it."
echo -e "The next time this script is run, the tmp file will be erased and started fresh.\n"
else
echo -e "\nSomething went wrong, the temp file is not there.\n"
exit $?
fi
# All Done
if [ "$?" -ne "0" ]; then
echo "find of drop files larger than $SIZE completed without errors.\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment