File Organizer Script
Automatically organize files in a directory into folders categorically
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,.pdfetc. 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