DEV Community

Preetpal Basson
Preetpal Basson

Posted on

Directories created using Alphabets (A to Z) from Current Directory

Have you ever wanted to organise your files and directories systemically.
Well a script has been designed with that very purpose in mind.

Alphabet-MkDirs.sh is a script designed to make Directories from A to Z using current Directory. Will make either Normal or Static directories based on user Selection.

When the script has been executed the following options appear:

  • Normal: Creates the A to Z directories based on current directory name
  • Static: Creates the A to Z directories based on current directory name and create an empty file that means directory must be manually deleted
    • find . -type d -empty -delete would remove all empty directory as in Normal selected. Static would prevent this, hence manual removal.
  • _ToSort: This creates additional directory
    • eg TestingDirectory/TestingDirectory-T/ToSort-TestingDirectory/ToSort-TestingDirectory-001

Steps

  1. Navigate to directory that you wish to create directories in.
    1. eg $USER/Downloads/TestDirectory
  2. Execute the BASH script from the command line
    1. eg bash $HOME/bashscripts/bashscripts-M/MkDirs-Script/Alphabet-MkDirs/Alphabet-MkDirs.sh
      1. Change the above path to where you have the script. This must be an Absolute path otherwise you cannot run this from any directory as expected.
  3. Select one of the Script options
    1. Normal:
    2. Normal_ToSort:
    3. Static:
    4. Static_ToSort:
  4. Directories outputted to terminal
    1. Directory Output
  5. Sorting commence
    1. Directories

Let me know if this was useful or have any questions.

GitHub Script

#!/bin/bash

# Change Path below to reflect when script is being executed on local. eg `bash $HOME/Downloads/Alphabet-MkDirs.sh`
# bash $HOME/bashscripts/bashscripts-M/MkDirs-Script/Alphabet-MkDirs/Alphabet-MkDirs.sh

# AUTHOR: PREETPAL BASSON
# PURPOSE: Make Directory from A to Z using current Directory. Will make either Normal or Static directories. Static means directory will not be deleted as empty, 
# PREREQUISITE: Execute from Current Directory to creates new Directories 

function header(){
    echo "-----------------------------"
    echo "# Script - $1"
    echo
}


function staticselect() {
    echo "Please Select An Directory Builder?"
    select list in Normal Normal_ToSort Static Static_ToSort Exit
    do
        case $list in
                Normal)
                    IS_STATIC=false
                    IS_SORT=false
                    echo && echo " $list Has Been Selected "
                    break
                ;;
                Normal_ToSort)
                    IS_STATIC=false
                    IS_SORT=true
                    echo && echo " $list Has Been Selected "
                    break
                ;;
                Static)
                    IS_STATIC=true
                    IS_SORT=false
                    echo && echo " $list Has Been Selected "
                    break
                ;;
                Static_ToSort)
                    IS_STATIC=true
                    IS_SORT=true
                    echo && echo " $list Has Been Selected "
                    break
                ;;
                Exit)
                    echo && echo "Script will now exit"
                    exit
                    break
                ;;
                *)
                    echo "Invalid Option"
                ;;
        esac
    done
}

function initialize() {
    FULL_PATH="$PWD"
    CUR_DIR=${PWD##*/}
    IS_STATIC=false
    IS_SORT=false
    T_CHAR="T"
}

header "START"

initialize

staticselect

for n in {A..Z}
do
    CUR_PATH=$(printf "$FULL_PATH/$CUR_DIR-$n")

    if [ ! -d $CUR_PATH ]; then
        mkdir -vp "$CUR_PATH"
    fi
    if [[ -d $CUR_PATH && "$IS_STATIC" = true ]]; then
        touch "$CUR_PATH/.$CUR_DIR-$n"
        echo "$(printf "$CUR_DIR-$n")/.$CUR_DIR-$n" >> "$CUR_PATH/.$CUR_DIR-$n"
    fi
    if [[ "$n" == "$T_CHAR" && "$IS_SORT" == "true" ]]; then
        mkdir -vp "$CUR_PATH/ToSort-$CUR_DIR/ToSort-$CUR_DIR-001"
    fi
done 

header "END"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)