File organizer: A Bash Script to Keep Your Directories Clean


Tired of cluttered folders filled with random files? file_organizer is a lightweight Bash script designed to help you automatically sort files into folders based on their extensions. Whether you're organizing your Downloads folder, a development workspace, or log directories, this script makes it simple and efficient, without needing any external tools.
Features
Automatically organizes files by extension
Files without extensions go into an "others" folder.
Performs input validation and permission checks
Skips directories, symlinks, and already-organized content
Asks for user confirmation before taking action
How It Works
Accepts a single directory path as an argument.
Scans through files in the directory (ignores subdirectories).
Creates subfolders based on file extensions.
Moves files into their respective extension-named folders.
Prompts the user before proceeding with any changes.
Usage
Usage: ./file_organizer.sh <source_directory>
Example:
./file_organizer.sh /home/kali/Downloads
Requirements
Bash (pre-installed on most Linux distributions)
File permissions to read/write in the target directory
Script
#!/bin/bash
# Assign first argument to variable
SOURCE_DIR="$1"
# Argument and Directory Validation
# Check for correct number of arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <source_directory>"
echo "Error: Expected exactly one argument (the source directory path), but $# was given."
exit 1
fi
# Check if the provided path is a directory and exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Directory '$SOURCE_DIR' does not exist or is not a directory."
exit 1
fi
# Check if the directory is writable
if [ ! -w "$SOURCE_DIR" ]; then
echo "Error: Directory '$SOURCE_DIR' is not writable. Please check permissions."
exit 1
fi
# User Confirmation
read -p "Proceed with organizing files in '$SOURCE_DIR'? (yes/no): " confirm_choice
confirm_choice=${confirm_choice,,} # Convert input to lowercase
if [[ "$confirm_choice" != "yes" ]]; then
echo "Operation cancelled by user. No files were moved."
exit 0
fi
echo "Starting file organization in '$SOURCE_DIR'..."
echo "------------------------------------------------"
# File Iteration and Movement
# Loop through all items directly within the source directory
for item in "$SOURCE_DIR"/*; do
# Ensure we are only processing regular files (not subdirectories, symlinks, etc.)
if [ -f "$item" ]; then
filename=$(basename -- "$item")
extension="${filename##*.}" # # extracts everything after the last dot.
# Check if the extracted extension is the same as the full filename.
if [[ "$extension" == "$filename" || -z "$extension" ]]; then
target_subdir="others"
else
# Use the extracted extension as the subdirectory name
target_subdir="$extension"
fi
DEST_PATH="$SOURCE_DIR/$target_subdir" # Full path to the destination subdirectory
# Create the destination directory if it doesn't exist
if ! mkdir -p "$DEST_PATH"; then
echo "Warning: Could not create destination directory '$DEST_PATH'. Skipping file '$filename'."
continue # Skip to the next file in the loop
fi
# Move the file
if mv "$item" "$DEST_PATH/"; then
echo "SUCCESS: Moved '$filename' to '$target_subdir/'"
else
echo "WARNING: Failed to move '$filename' to '$target_subdir/'. Check permissions or if a file with the same name exists."
fi
fi
done
echo "------------------------------------------------"
echo "File organization process complete."
Output
List directory contents.
Run the script
Where This Can Be Useful:
Organizing Downloads, Documents, or Desktop
Cleaning up software project directories
Sorting server logs and reports
Pre-processing data folders for backup or archiving
Conclusion
The file_organizer script is a great way to add order to chaos with just one command. Whether you're a developer, sysadmin, or just like a tidy machine, it's a must-have in your Bash toolkit.
Subscribe to my newsletter
Read articles from Dauda Sahr N'yumah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dauda Sahr N'yumah
Dauda Sahr N'yumah
My name is Dauda Sahr N’yumah, and I transitioned from a background in Banking and Finance to pursue a growing passion for ethical hacking and information security. I'm currently building my skills in penetration testing, specializing in network security, and exploring tools like Kali Linux, Nmap, Metasploit, Wireshark, and Python scripting.