Skip to content

Instantly share code, notes, and snippets.

@fhiyo
Created September 9, 2018 04:34
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 fhiyo/0f505292a974720ed59ec4be0563007b to your computer and use it in GitHub Desktop.
Save fhiyo/0f505292a974720ed59ec4be0563007b to your computer and use it in GitHub Desktop.
Clean your Downloads directory.
#!/usr/bin/env bash
## Description:
## Author : fhiyo
## Email : fhiyo1201@gmail.com
set -u
declare -r SCRIPT_NAME=$( basename ${BASH_SOURCE[0]} )
declare -r SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
usage() {
echo "usage: $( basename $0 ) [OPTIONS]
Clean your Downloads directory.
Make today's name directory on ${HOME}/Downloads, and move all files and dirs (except date name direcrtory) to the dir.
OPTIONS:
-h, --help Print usage
"
}
main() {
oldifs="${IFS}"
IFS=$'\n'
cd ${HOME}/Downloads
dir=$( date "+%Y%m%d" )
src_list=($( ls | \grep -E -v '[0-9]{6}' ))
# echo ${#src_list[@]}
if [ ${#src_list[@]} -eq 0 ]; then
exit 0
fi
mkdir ${dir}
for src in "${src_list[@]}"; do
mv ${src} ${dir}
done
IFS="${oldifs}"
}
# ======= Main =======
cd ${SOURCE_DIR} # Move directory to this script file exists
if [ $# == 0 ]; then
main
exit 0
fi
# Analyse optional arguments
for opt in "$@"; do
case "${opt}" in
'-h' | '--help' )
usage
exit 0
;;
* )
echo "Invalid arguments.\n"
usage
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment