Skip to content

Latest commit

 

History

History
129 lines (111 loc) · 4.94 KB

File Organizer.md

File metadata and controls

129 lines (111 loc) · 4.94 KB

The code:

#!/bin/bash
# This line specifies the interpreter to use, which is bash in this case.

# Function to create directories for each file type
create_directories() {
    # This function creates directories for different file types.
    mkdir -p "${@}"
    # The -p flag ensures that intermediate directories are created if they do not exist.
}

# Function to organize files
organize_files() {
    # This function organizes files in the current directory into subdirectories based on their types.

    # Create directories for common file types
    create_directories "Images" "Documents" "Videos" "Music" "Others"
    # Call the create_directories function to create directories for images, documents, videos, music, and others.

    # Loop through files and directories in current directory
    for file in *; do
        # Iterate over each file in the current directory.

        # Skip special files and directories, including the script itself
        if [[ -e $file && $file != "$(basename "$0")" && ! -d $file ]]; then
            # Check if the file exists and is not a directory and not the script itself.

            # Get file extension
            extension="${file##*.}"
            # Extract the file extension.

            # Determine file type and move it to the corresponding directory
            case $extension in
                jpg|jpeg|png|gif)
                    mv "$file" Images/
                    ;;
                pdf|doc|docx|xls|xlsx|ppt|pptx|txt)
                    mv "$file" Documents/
                    ;;
                mp4|avi|mkv|mov|wmv)
                    mv "$file" Videos/
                    ;;
                mp3|wav|flac|m4a)
                    mv "$file" Music/
                    ;;
                *)
                    mv "$file" Others/
                    ;;
                # Move files with unrecognized extensions to the "Others" directory.
            esac
            # Move the file to the corresponding directory based on its extension.
        elif [[ -d $file && ! ($file == "Images" || $file == "Documents" || $file == "Videos" || $file == "Music" || $file == "Others") ]]; then
            # If it's a directory and not one of the predefined directories.
            mv "$file" Others/
            # Move the directory to "Others".
        fi
    done

    echo "Files and directories organized successfully."
    # Print a success message.
}

# Call the function to organize files
organize_files
# Execute the organize_files function.

This Bash script is designed to organize files in the current directory into subdirectories based on their types. It uses two functions: create_directories and organize_files.

Let's break down the code:

  1. Shebang:

    #!/bin/bash

    This line specifies the interpreter to use, which is bash in this case.

  2. create_directories Function:

    create_directories() {
        mkdir -p "${@}"
    }

    This function takes directory names as arguments and creates these directories using the mkdir command. The -p flag ensures that intermediate directories are created if they do not exist.

  3. organize_files Function:

    organize_files() {
        create_directories "Images" "Documents" "Videos" "Music" "Others"
    
        for file in *; do
            if [[ -e $file && $file != "$(basename "$0")" && ! -d $file ]]; then
                extension="${file##*.}"
                case $extension in
                    jpg|jpeg|png|gif)
                        mv "$file" Images/
                        ;;
                    pdf|doc|docx|xls|xlsx|ppt|pptx|txt)
                        mv "$file" Documents/
                        ;;
                    mp4|avi|mkv|mov|wmv)
                        mv "$file" Videos/
                        ;;
                    mp3|wav|flac|m4a)
                        mv "$file" Music/
                        ;;
                    *)
                        mv "$file" Others/
                        ;;
                esac
            elif [[ -d $file && ! ($file == "Images" || $file == "Documents" || $file == "Videos" || $file == "Music" || $file == "Others") ]]; then
                mv "$file" Others/
            fi
        done
    
        echo "Files and directories organized successfully."
    }
    
    organize_files
    • The create_directories function is called to create directories for Images, Documents, Videos, Music, and Others.
    • The script then iterates through each file in the current directory.
    • For each file, it checks if the file exists, is not the script itself, and is not a directory.
    • It extracts the file extension and uses a case statement to determine the file type.
    • Files are then moved to the corresponding subdirectories (Images, Documents, Videos, Music, or Others) based on their types.
    • If a file is a directory and not one of the predefined directories, it is moved to the "Others" directory.
    • The script prints a success message after organizing the files and directories.