diff --git a/organize_directory.sh b/organize_directory.sh old mode 100644 new mode 100755 index f1f641af19bf623505554b29f35499b5d15f3fd3..2d0b33f22157cad5a8f99206844c99a5a070728b --- a/organize_directory.sh +++ b/organize_directory.sh @@ -1 +1,239 @@ #!/usr/bin/env bash +### + # Moves files from indicated input folder to indicated output folder. + # Organizes files by type. + ## + + +# Import utils script. +. $(dirname ${0})/utils.sh + + +# Global Variables. +input_dir="" +output_dir="" +output_subdir="" + + +### + # Script start. + ## +function main () { + display_args_kwargs + + # Check number of passed function args. + if [[ ${#args[@]} > 1 ]] + then + # Two or more args passed. Continue. + + # Grab directory locations. + input_dir=${args[0]} + output_dir=${args[1]} + + # Validate input dir. + if [[ -d ${input_dir} ]] + then + get_absolute_path ${input_dir} + input_dir=${return_value} + + else + echo -e "${text_red}Passed value ( ${input_dir} ) is not a valid directory.${text_reset}" + exit 1 + fi + + # Validate output dir. + if [[ -d ${output_dir} ]] + then + get_absolute_path ${output_dir} + output_dir=${return_value} + + else + echo -e "${text_red}Passed value ( ${output_dir} ) is not a valid directory.${text_reset}" + exit 1 + fi + + echo "input_dir: ${input_dir}" + echo "output_dir: ${output_dir}" + + # Loop through all files in input directory. + for file in ${input_dir}/* + do + # Verify is file. + if [[ -f ${file} ]] + then + handle_file ${file} + fi + done + + # Handle for too few args. + else + echo -e "${text_red}Too few args passed. Expected at least two.${text_reset}" + echo -e "${text_red}Please provide input dir (arg 1) and output dir (arg 2).${text_reset}" + exit 1 + fi +} + + +### + # Full handling for single file. + ## +function handle_file () { + local file=${1} + + parse_file_name ${file} + to_lower ${file_extension} + file_extension=${return_value} + + # Handle based on found extension. + # Handle images. + if [[ "${file_extension}" == ".jpg" || + "${file_extension}" == ".jpeg" || + "${file_extension}" == ".png" ]] + then + output_subdir="${output_dir}/Images" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + + # Handle animated images/video. + elif [[ "${file_extension}" == ".gif" ]] + then + output_subdir="${output_dir}/GIFs" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + elif [[ "${file_extension}" == ".mp4" ]] + then + output_subdir="${output_dir}/MP4s" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + + # Handle flash files. + elif [[ "${file_extension}" == ".flv" || + "${file_extension}" == ".sfw" ]] + then + output_subdir="${output_dir}/Flash" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + # Handle document files. + elif [[ "${file_extension}" == ".doc" || + "${file_extension}" == ".docx" ]] + then + output_subdir="${output_dir}/Word" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + elif [[ "${file_extension}" == ".pdf" ]] + then + output_subdir="${output_dir}/PDFs" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + elif [[ "${file_extension}" == ".ppt" ]] + then + output_subdir="${output_dir}/PowerPoints" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + elif [[ "${file_extension}" == ".xls" || + "${file_extension}" == ".xlsx" ]] + then + output_subdir="${output_dir}/Spreadsheets" + + # Verify location exists. + if [[ ! -d ${output_subdir} ]] + then + mkdir ${output_subdir} + fi + + # Move file. + move_file + + # Handle all other file types. + else + echo " Is unknown type." + fi +} + + +function move_file () { + local file_name_counter=0 + local orig_file_name=${file_name} + + # Check if file with same name already exists in output dir. + if [[ -f "${output_subdir}/${file_name}${file_extension}" ]] + then + # Duplicate file name found. Keep trying new counters until valid one is found. + while [[ -f "${output_subdir}/${file_name}${file_extension}" ]] + do + # Update filename counter. + file_name_counter=$(expr ${file_name_counter} + 1) + + # Check file name combination. + if [[ ! -f "${output_subdir}/${file_name}_${file_name_counter}${file_extension}" ]] + then + # New file name combination found. Save. + file_name="${file_name}_${file_name_counter}" + fi + done + fi + + local input_location="${input_dir}/${orig_file_name}${file_extension}" + local output_location="${output_subdir}/${file_name}${file_extension}" + + echo -e "Moving ${text_purple}${orig_file_name}${file_extension}${text_reset} to ${text_purple}${output_location}${text_reset}" + mv ${input_location} ${output_location} +} + +main