From baa2066d5dd1866682251f9a5702d2962e7431ca Mon Sep 17 00:00:00 2001 From: Ali-Elbana Date: Thu, 5 Oct 2023 07:57:23 +0300 Subject: [PATCH 1/7] Adding my file organizer script --- file_organizer.sh | 99 --------------------------------------------- student-solution.sh | 75 ++++++++++++++++++++++++++++++++++ student_solution.sh | 1 - 3 files changed, 75 insertions(+), 100 deletions(-) delete mode 100644 file_organizer.sh create mode 100755 student-solution.sh delete mode 100644 student_solution.sh 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 @@ - From e7e480615eed4eeceb884227dcb2ba9d8b4bad3e Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 12:01:44 +0300 Subject: [PATCH 2/7] Adding algorithm description --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ce219b..a9f3c38 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,22 @@ Downloads/ | |-- file6.unknown ``` +************************************************************************************************************************************************************ + +## **Let's take about the algorithm:** + +we can divide the algorithm of the script into 4 parts: +1-Geting the path of the directory we wnat to organize. +2-Creating the sub-directories to organize the files and put them inside the sub-directories. +3-Organize the files and put them inside the sub-directories. +4-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: +1-GetPath +2-MK_DIRS +3-OrganizeFiles +4-LS_DIRS + -## How to upload task on github. -- Please refer to explanation video that has been sent through email. From b510d7c8b363d24c605c74a9e88324f43f587590 Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 12:02:29 +0300 Subject: [PATCH 3/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a9f3c38..7f9c439 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,14 @@ Downloads/ ## **Let's take about the algorithm:** we can divide the algorithm of the script into 4 parts: + 1-Geting the path of the directory we wnat to organize. 2-Creating the sub-directories to organize the files and put them inside the sub-directories. 3-Organize the files and put them inside the sub-directories. 4-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: + 1-GetPath 2-MK_DIRS 3-OrganizeFiles From d83a5efda2d84c0e01546affcee5ed83f8a1ec59 Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 12:03:00 +0300 Subject: [PATCH 4/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7f9c439..f954e93 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Downloads/ we can divide the algorithm of the script into 4 parts: + 1-Geting the path of the directory we wnat to organize. 2-Creating the sub-directories to organize the files and put them inside the sub-directories. 3-Organize the files and put them inside the sub-directories. @@ -53,6 +54,7 @@ we can divide the algorithm of the script into 4 parts: To-do that, I create four functions for each part: + 1-GetPath 2-MK_DIRS 3-OrganizeFiles From 11dbeab10c1c9b246dc5310f2554d31fffedac2b Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 12:04:08 +0300 Subject: [PATCH 5/7] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f954e93..0400298 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,18 @@ Downloads/ we can divide the algorithm of the script into 4 parts: -1-Geting the path of the directory we wnat to organize. -2-Creating the sub-directories to organize the files and put them inside the sub-directories. -3-Organize the files and put them inside the sub-directories. -4-List the sub-directories and the files as a way of verfication to make sure that the algorithm is working as required. +1- Geting the path of the directory we wnat to organize. +2- Creating the sub-directories to organize the files and put them inside the sub-directories. +3- Organize the files and put them inside the sub-directories. +4- 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: -1-GetPath -2-MK_DIRS -3-OrganizeFiles -4-LS_DIRS +1- GetPath +2- MK_DIRS +3- OrganizeFiles +4- LS_DIRS From c261e7a1aa2ce9246c5c37ad8a31ad226be433a4 Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 12:05:07 +0300 Subject: [PATCH 6/7] Update README.md --- README.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0400298..e88013b 100644 --- a/README.md +++ b/README.md @@ -46,19 +46,17 @@ Downloads/ we can divide the algorithm of the script into 4 parts: - -1- Geting the path of the directory we wnat to organize. -2- Creating the sub-directories to organize the files and put them inside the sub-directories. -3- Organize the files and put them inside the sub-directories. -4- List the sub-directories and the files as a way of verfication to make sure that the algorithm is working as required. +- 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: - -1- GetPath -2- MK_DIRS -3- OrganizeFiles -4- LS_DIRS +- GetPath +- MK_DIRS +- OrganizeFiles +- LS_DIRS From e23ff690691abdf2db151c4fdddc543b9a4b9af2 Mon Sep 17 00:00:00 2001 From: Ali El-bana Date: Thu, 5 Oct 2023 14:25:11 +0300 Subject: [PATCH 7/7] Adding design illustration --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e88013b..38af488 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ To-do that, I create four functions for each part: - OrganizeFiles - LS_DIRS +## **The design:** + +![image](https://github.com/Ali-Elbana/M1-A2/assets/97269796/0d6e38f5-ef6e-4f29-bb02-ca72478ac794) +