How to Automatically Sort and Organize Files by Extension in Linux using a Bash Script

If you regularly download files or manage a shared directory on a Linux server, your “Downloads” or “Documents” folder can quickly descend into chaos. Manually dragging JPEGs into an images folder and PDFs into a documents folder is tedious and inefficient. Fortunately, the Linux terminal provides everything you need to automate this process entirely. By writing a simple Bash script, you can instruct the operating system to scan a directory, identify the file extensions (like .jpg, .txt, .pdf), create dedicated folders for each type, and move the files accordingly—all in a fraction of a second.

Step 1: Create the Bash Script File

We will create a script file in the directory you wish to organize. For this example, we will assume you are organizing your ~/Downloads folder.

  1. Open your Linux terminal.
  2. Navigate to the target directory:
    cd ~/Downloads
  3. Create a new file named organize.sh and open it in the nano text editor:
    nano organize.sh

Step 2: Write the Sorting Logic

Copy and paste the following code into the nano editor. This script uses a simple for loop to iterate through every file, extracts the characters after the final dot to determine the extension, creates a directory matching that extension, and moves the file into it.

#!/bin/bash

# Loop through all files in the current directory
for file in *; do
    # Skip directories and the script itself to prevent moving them
    if [ -f "$file" ] && [ "$file" != "organize.sh" ]; then
        
        # Extract the file extension
        extension="${file##*.}"
        
        # Check if the file actually has an extension (prevent moving files with no extension into a folder named after the file itself)
        if [ "$file" != "$extension" ]; then
            
            # Convert the extension to uppercase for neater folder names (optional)
            dir_name="${extension^^}"
            
            # Check if a directory for this extension already exists; if not, create it
            if [ ! -d "$dir_name" ]; then
                mkdir "$dir_name"
            fi
            
            # Move the file into the respective directory
            mv "$file" "$dir_name/"
        fi
    fi
done

echo "Directory organized successfully!"

Save the file by pressing Ctrl+O, then Enter, and exit nano by pressing Ctrl+X.

Step 3: Make the Script Executable and Run It

By default, Linux creates new text files without execute permissions for security reasons. You must grant the script permission to run as a program.

  1. Run the chmod command to add execute permissions:
    chmod +x organize.sh
  2. Execute the script:
    ./organize.sh

Instantly, the terminal will print “Directory organized successfully!”. If you run the ls command, you will see that your messy directory has been replaced by neatly categorized folders named PDF, JPG, ZIP, and so on, containing all their respective files. You can run this script manually whenever the folder gets messy, or use a tool like cron to schedule it to run automatically every night.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.