File Organizer Script

Automatically organize files in a directory into folders categorically

bash file

In this tutorial, we are trying to learn how to create a simple Bash script that will help automatically organize files in a directory into folders like images, docs, videos, others etc.

This tutorial has been prepared under Ubuntu 24.04 but it should work in most of the Linux OS distributions.

πŸ“Œ Objective

You need write a script which automatically organize files in a directory into folders like:

images/
docs/
videos/
others/

🧠 1. Concept (5–10 mins)

To write this script, you should have good understanding of following concepts.

  • for loop β†’ iterate through files
  • if-elif-else β†’ decision making
  • case β†’ pattern matching (better for extensions)
  • mkdir -p β†’ create folders safely
  • mv β†’ move files
  • basename β†’ handle file names
  • File extensions (*.jpg, *.pdf, etc.)
Try to learn above concept if you have no or less knowledge. Otherwise, it would be difficult to write the script.

βš™οΈ 2. Pre-Setup

  • Create a project folder with name file-organizer.

NOTE: You can create a master project folder as linux-basic-projects and then create file-organizer inside it for better projects organization.

mkdir file-organizer
cd file-organizer
  • Now create sample files to be required for the project by running following command:
touch ai.jpeg ai.txt image.png movie.mkv photo.jpg song.mp3 ai.mp4 doc.pdf linux.pdf notes.txt report.docx video.mp4

πŸ§ͺ 3. The Script (Solution)

Now, we will begin with writing the script for the project.

  • Create a simple script:
vim organize.sh       # or you can use nano editor
  • Create a target directory and required folders:
# Target directory (current directory)
TARGET_DIR="."     # Fixed to current directory

# Create folders
mkdir -p "$TARGET_DIR/images"
mkdir -p "$TARGET_DIR/docs"
mkdir -p "$TARGET_DIR/videos"
mkdir -p "$TARGET_DIR/others"
  • Now, we need to loop through the target directory which is current directory, then extract the file extension like .jpg, .mp4, .pdf etc. And, based on this extenstion, move the file into respective folder. We can write this in Shell script as:
# Loop through all files
for file in "$TARGET_DIR"/*; do
  # Skip directories
  [ -d "$file" ] && continue

  # Get file extension
  if [[ "$file" == *.* ]]; then
    extension="${file##*.}"
  else
    extension=""
  fi

  case "$extension" in
    jpg|jpeg|png|gif)
      mv "$file" "$TARGET_DIR/images/"
      ;;
    pdf|doc|docx|txt)
      mv "$file" "$TARGET_DIR/docs/"
      ;;
    mp4|mkv|avi)
      mv "$file" "$TARGET_DIR/videos/"
      ;;
    *)
      mv "$file" "$TARGET_DIR/others/"
      ;;
  esac
done

The particular code "${file##*.}" inside above for loop means:

  • file β†’ variable holding filename
  • ## β†’ remove the longest match from the start (left side)
  • *. β†’ match everything up to the last dot

  • Finally print message by echoing:
echo "Files organized successfully!"

πŸ’» 4. Run the script

Now, above script is complete. To run it we need to make it executable by giving proper permission.

chmod +x organize.sh
./organize.sh

🎞 Output

# Before execution
$ ls
ai.jpeg  ai.txt   docs       images     movie.mkv  organize.sh  photo.jpg    song.mp3   videos
ai.mp4   doc.pdf  image.png  linux.pdf  notes.txt  others       report.docx  video.mp4

# After script execution
$ ls
docs  images  others  videos

$ ls docs/
ai.txt  doc.pdf  linux.pdf  notes.txt  report.docx

$ ls images/
ai.jpeg  image.png  photo.jpg

$ ls others/
organize.sh  song.mp3

$ ls videos/
ai.mp4  movie.mkv  video.mp4

Congratulations

Congratulations!

You have learnt how to organize files inside folder by creating a simple shell script.