diff --git a/README.md b/README.md index 4ce219b..38af488 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,28 @@ Downloads/ | |-- file6.unknown ``` +************************************************************************************************************************************************************ + +## **Let's take about the algorithm:** + +we can divide the algorithm of the script into 4 parts: + +- Geting the path of the directory we wnat to organize. +- Creating the sub-directories to organize the files and put them inside the sub-directories. +- Organize the files and put them inside the sub-directories. +- List the sub-directories and the files as a way of verfication to make sure that the algorithm is working as required. + +To-do that, I create four functions for each part: + +- GetPath +- MK_DIRS +- OrganizeFiles +- LS_DIRS + +## **The design:** + +![image](https://github.com/Ali-Elbana/M1-A2/assets/97269796/0d6e38f5-ef6e-4f29-bb02-ca72478ac794) + -## How to upload task on github. -- Please refer to explanation video that has been sent through email. diff --git a/file_organizer.sh b/file_organizer.sh deleted file mode 100644 index c69a3ff..0000000 --- a/file_organizer.sh +++ /dev/null @@ -1,99 +0,0 @@ -#!/bin/bash - -# -# > STATUS code that returned by this script. - - - -# Project Idea: File Organizer -# The goal of this project is to create a Bash script that organizes files in a specified directory based on their file types into separate subdirectories. This can be useful to keep your directories clean and tidy by automatically sorting files into appropriate categories. -#Features: -#The script should take a directory path as an argument and organize the files within that directory. -#It should create subdirectories based on the file extensions (e.g., "txt" for text files, "jpg" for image files). -#Files with unknown or no file extensions should be placed in a "misc" subdirectory. -#If a subdirectory for a particular file type already exists, the script should move the files into that existing directory. -#The script should handle edge cases, such as files with no extensions or hidden files (those starting with a dot). -# please provide status exit code for each case with echo message detect the failure. - - -################################################################################################ -# SUPPORTED file extenstions: .txt, .jpg, .png, .hidden files. # -################################################################################################ -SUPPORTED_EXTENSIONS=('txt' 'jpg' 'png' '.') -############################################################################################### -# Helper functions: -# 1. createDirectory. -############################################################################################### -createDirectory(){ - local DIR_NAME=$1 # first argument. - if [ ! -d ${DIR_NAME} ] # check if directory is exists. - then - mkdir -p ${DIR_NAME} - echo "${DIR_NAME} is created". - fi -} - - -# read directory. -read DIR - -# check if directory is exists or not. -if [ -d ${DIR} ] -then - - # read all files inside this directory. - for file in ${DIR}/*; - do - # check if its file. - if [ ! -f ${file} ]; - then - echo ${file} " is not a file" - continue # skip directories. - fi - # pattern matching, get extension. - FILE_EXTENSION=${file##*.} - - FILE_EXTENSION=$(echo $FILE_EXTENSION | tr [:upper:] [:lower:]) - DIRECTORY_PATH=${file%/*} - FILE_NAME=${file##*/} - - if [ ${FILE_EXTENSION} == ${SUPPORTED_EXTENSIONS[0]} ]; - then - # text folder. - createDirectory "${DIRECTORY_PATH}/text" - echo "Move:" ${file} "to --->: " "${DIRECTORY_PATH}/text/${FILE_NAME}" - mv ${file} "${DIRECTORY_PATH}/text/${FILE_NAME}" - elif [ ${FILE_EXTENSION} == ${SUPPORTED_EXTENSIONS[1]} ]; - then - # jpg folder. - createDirectory "${DIRECTORY_PATH}/jpg" - echo "Move:" ${file} "to --->: " "${DIRECTORY_PATH}/jpg/${FILE_NAME}" - mv ${file} "${DIRECTORY_PATH}/jpg/${FILE_NAME}" - elif [ ${FILE_EXTENSION} == ${SUPPORTED_EXTENSIONS[2]} ]; - then - # png folder. - createDirectory "${DIRECTORY_PATH}/png" - echo "Move:" ${file} "to --->: " "${DIRECTORY_PATH}/png/${FILE_NAME}" - mv ${file} "${DIRECTORY_PATH}/png/${FILE_NAME}" - - elif [[ "${FILE_NAME}" == ^.* ]]; - then - # hidden folder. - #echo hidden - createDirectory "${DIRECTORY_PATH}/hidden" - echo "Move:" ${file} "to --->: " "${DIRECTORY_PATH}/hidden/${FILE_NAME}" - mv ${file} "${DIRECTORY_PATH}/hidden/${FILE_NAME}" - else - # miscellaneous folder. - createDirectory "${DIRECTORY_PATH}/misc" - echo "Move:" ${file} "to --->: " "${DIRECTORY_PATH}/misc/${FILE_NAME}" - mv ${file} "${DIRECTORY_PATH}/misc/${FILE_NAME}" - fi - - done -else - echo "${DIR}, Directory is not exists" - exit 1 -fi - - diff --git a/student-solution.sh b/student-solution.sh new file mode 100755 index 0000000..fdf6081 --- /dev/null +++ b/student-solution.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +DIR_PATH="" + +GetPath() +{ + read -p "Enter the path: " DIR_PATH +} + +MK_DIRS() +{ + cd ${DIR_PATH} + + DIRS=( "txt" "jpg" "pdf" "misc" ) + + # if the directory not found create it # + for DIR in "${DIRS[@]}" ; do + + if [[ ! -d "$DIR" ]] ; then + mkdir "$DIR" + fi + + done +} + +OrganizeFiles() +{ + for file in "$DIR_PATH"/* ; do + + # if the file is a directory skip it # + if [[ -d "$file" ]] ; then + continue + fi + + if [[ "$file" == *.txt ]] ; then + mv "$file" "$DIR_PATH"/txt + + elif [[ "$file" == *.jpg ]] ; then + mv "$file" "$DIR_PATH"/jpg + + elif [[ "$file" == *.pdf ]] ; then + mv "$file" "$DIR_PATH"/pdf + + else + mv "$file" "$DIR_PATH"/misc + fi + + done +} + +LS_DIRS() +{ + + ls $DIR_PATH $DIR_PATH/txt $DIR_PATH/pdf $DIR_PATH/jpg $DIR_PATH/misc + +} + +GetPath $DIR_PATH + +MK_DIRS $DIR_PATH + +OrganizeFiles $DIR_PATH + +LS_DIRS $DIR_PATH + + + + + + + + + + + diff --git a/student_solution.sh b/student_solution.sh deleted file mode 100644 index 8b13789..0000000 --- a/student_solution.sh +++ /dev/null @@ -1 +0,0 @@ -