hjf

Wednesday, November 11, 2015

Organize Downloads Folder In Linux (Or Mac)

Organize Downloads Folder In Linux
(Or Mac)



If any of you use Linux or Mac (even Windows, but I'm talking about *nix today) and download lots of stuff then you know how messy it can get really quick even if you try to stay organized.

I have written a shell script to organize your lose files in your home downloads folder fairly intuitively. It first checks for my preset directory tree to be built inside the downloads folder with sections: Archives, Audio, Video, Documents, Images and Installers with many preset sections inside each directory for the different file types. For example you would find .pdf files inside ...Documents/pdf... or .txt files in Documents/txt etc... If the directories are not found it creates them.

It then searches for a large list of preset file extensions in the main Downloads folder non-recursively and then checks the destination folder to see if the file already exists, if not it will then move the file to the destination. If the file is already there it will ask what you would like to do with the file: Replace, Delete or Do Nothing. This seems like a lot (I'm a good enough coder to turn 2600 lines of code into 200 :D ), but the folder checking literally takes less than 1 second (probably 500 ms) and the search for files takes less than a couple. Search is used with 'find' at -maxdepth 1 (for non-recursive).

This script is easily editable if you are familiar with *nix shells, directories and file types can be added accordingly. You can really pull my functions and and use in any type of folder organization.

This is open-source code; you may use in your home or business, but any use in any sell-able product is strictly prohibited unless I have given permission.

Script code below, header is for bash (#!/bin/bash), but I am sure this is POSIX friendly and doesn't need a head so strip the BASH header for non bash shells. If you do not already know how to use scripts then this script is probably not for you, but if you need help running and are willing to learn then please ask away; I'll help.

Script File if you prefer:
mvarchives: mvarchives script file @ Dropbox
Script Code:

#!/bin/bash
# Organizer for lose files in $HOME/Downloads

# Directory list
# Calling users download directory
dldir="$HOME/Downloads"

# Archive directories
arch="$dldir/Archives"
zip="$arch/zip"
gzd="$arch/gz"
tar="$arch/tar"
rar="$arch/rar"
bz2="$arch/bz2"
szdir="$arch/7z"

# Installer directories
instl="$dldir/Installers"
deb="$instl/deb"
rpm="$instl/rpm"

# Image directories
picf="$dldir/Images"
png="$picf/png"
jpg="$picf/jpg"
bmp="$picf/bmp"
tif="$picf/tif"
gif="$picf/gif"
ico="$picf/ico"
svg="$picf/svg"
jpeg="$picf/jpeg"

# Video directories
vidf="$dldir/Videos"
mov="$vidf/mov"
mp4="$vidf/mp4"
mpg="$vidf/mpg"
avi="$vidf/avi"
mkv="$vidf/mkv"
flv="$vidf/flv"
wmv="$vidf/wmv"
mpeg="$vidf/mpeg"
m4v="$vidf/mv4"
tgp="$vidf/3gp"

# Music directories
musf="$dldir/Audio"
mp3="$musf/mp3"
ogg="$musf/ogg"
flac="$musf/flac"

# Document directories
docf="$dldir/Documents"
txt="$docf/txt"
doc="$docf/doc"
bak="$docf/bak"
cfg="$docf/cfg"
css="$docf/css"
html="$docf/html"
php="$docf/php"
js="$docf/js"
asp="$docf/asp"
docx="$doc"
htm="$html"
ini="$docf/ini"
jso="$js"
log="$docf/log"
odf="$docf/odf"
ppt="$docf/ppt"
rtf="$docf/rtf"
xml="$docf/xml"
pdf="$docf/pdf"

# Main file moving function
mv_files () {
 fnamevar="$1"
 for archives in $(sudo find $dldir -maxdepth 1 -type f -iname "$fnamevar"); do
  fname=$(basename "$archives")
  if [ ! -f "$2/$fname" ]; then
   sudo mv "$archives" "$2"
   if [ ! -f "$2/$fname" ]; then
    echo "File: $fname was not moved to $2, something went wrong."
    echo ""
   else
    echo "File $fname was successfully moved to $2."
    echo ""
   fi
  else
   echo "File $fname was found in the target directory, file was not copied." 
   echo "What would you like to do with the file $archives? "
   PS3="Make a choice: "
   options=("Replace" "Delete" "Do Nothing")
   select opt in "${options[@]}"
   do
    case $opt in
     "Replace")
      sudo mv -f "$archives" "$2"
      if [ ! -f "$archives" ]; then
       echo "File: $fname in $dldir replaced file $fname in $2 successfully."
       echo ""
      else
       echo "File: $fname in $2 was not replaced, something went wrong."
       echo""
      fi
      break
      ;;
     "Delete")
      sudo rm -f "$archives"
      if [ ! -f "$archives" ]; then
       echo "File $archives deleted successfully."
       echo ""
      else
       echo "File $archives could not be deleted, something went wrong."
       echo ""
      fi
      break
      ;;      
     "Do Nothing")
       echo "You chose do nothing with file $archives, file remains."
       echo ""
      break
      ;;
     *) echo invalid option;;
    esac
   done   
  fi
 done
}

# Directory check and creation
dirn="$docf $txt $doc $bak $cfg $css $html $php $js $asp $docx $htm $ini $jso $log $odf $ppt $rtf $xml $pdf $arch $zip $gzd $tar $rar $bz2 $szdir $instl $deb $rpm $picf $png $jpg $jpeg $bmp $tif $gif $ico $svg $vidf $mov $mp4 $mpg $avi $mkv $flv $wmv $mpeg $m4v $tgp $musf $mp3 $ogg $flac"
read -r -a dirna <<< "$dirn"
for typed in ${dirna[@]}; do
 if [ ! -d "$typed" ]; then
  mkdir "$typed"
  if [ -d "$typed" ]; then
   echo "Folder $typed created successfully."
  else
   echo "Folder $typed was not created, something went wrong."
  fi
 fi
done

# File movement
IFS=$(echo -en "\n\b") 
mv_files "*.zip" "$zip"
mv_files "*.tar" "$tar"
mv_files "*.bz2" "$bz2"
mv_files "*.rar" "$rar"
mv_files "*.rpm" "$rpm"
mv_files "*.7z" "$szdir"
mv_files "*.deb" "$deb"
mv_files "*.gz" "$gzd"
mv_files "*.png" "$png"
mv_files "*.jpg" "$jpg"
mv_files "*.bmp" "$bmp"
mv_files "*.tif" "$tif"
mv_files "*.gif" "$gif"
mv_files "*.ico" "$ico"
mv_files "*.svg" "$svg"
mv_files "*.jpeg" "$jpeg"
mv_files "*.mov" "$mov"
mv_files "*.mp4" "$mp4"
mv_files "*.mpg" "$mpg"
mv_files "*.avi" "$avi"
mv_files "*.mkv" "$mkv"
mv_files "*.flv" "$flv"
mv_files "*.wmv" "$wmv"
mv_files "*.mpeg" "$mpeg"
mv_files "*.m4v" "$m4v"
mv_files "*.3gp" "$tgp"
mv_files "*.mp3" "$mp3"
mv_files "*.ogg" "$ogg"
mv_files "*.flac" "$flac"
mv_files "*.pdf" "$pdf"
mv_files "*.txt" "$txt"
mv_files "*.doc" "$doc"
mv_files "*.bak" "$bak"
mv_files "*.cfg" "$cfg"
mv_files "*.css" "$css"
mv_files "*.html" "$html"
mv_files "*.php" "$php"
mv_files "*.js" "$js"
mv_files "*.asp" "$asp"
mv_files "*.docx" "$doc"
mv_files "*.htm" "$html"
mv_files "*.ini" "$ini"
mv_files "*.jso" "$js"
mv_files "*.log" "$log"
mv_files "*.odf" "$odf"
mv_files "*.ppt" "$ppt"
mv_files "*.rtf" "$rtf"
mv_files "*.xml" "$xml"