Certainly! Here's a BASH script that classifies files in a given folder or the default ~/Downloads folder into different categories. It will create folders for each category found and move the corresponding files into those folders.
#!/bin/bash
# Set the input folder or use the default ~/Downloads
input_folder="${1:-$HOME/Downloads}"
# Create folders for each category if they don't exist
categories=("Documents" "Torrents" "Music" "Videos" "Programs")
for category in "${categories[@]}"; do
mkdir -p "${input_folder}/${category}"
done
# Recursively classify and move files into the respective folders
classify_files() {
local file_path
local file_name
local extension
local target_folder
for file_path in "$1"/*; do
if [[ -f "$file_path" ]]; then
file_name=$(basename "$file_path")
extension="${file_name##*.}"
case "$extension" in
# Document files
txt|doc|docx|pdf|xls|xlsx|ppt|pptx)
target_folder="${input_folder}/Documents"
;;
# Torrent files
torrent)
target_folder="${input_folder}/Torrents"
;;
# Music files
mp3|wav|flac|aac)
target_folder="${input_folder}/Music"
;;
# Video files
mp4|mov|avi|mkv|flv)
target_folder="${input_folder}/Videos"
;;
# Program files
exe|dmg|deb|pkg)
target_folder="${input_folder}/Programs"
;;
*)
target_folder="${input_folder}/Other"
;;
esac
# Move the file to the appropriate folder
mv "$file_path" "$target_folder"
elif [[ -d "$file_path" ]]; then
# Recursively classify files in subdirectories
classify_files "$file_path"
fi
done
}
# Start classification process
classify_files "$input_folder"
To run this script as a cronjob every day at noon, you can follow these steps:
- Save the above script to a file, for example,
file_classification.sh.
- Make the script executable by running the following command in the terminal:
chmod +x file_classification.sh
- Open the cron table for editing:
crontab -e
- Add the following line to the cron table to schedule the script to run every day at noon (12:00 PM):
0 12 * * * /path/to/file_classification.sh
Replace /path/to/file_classification.sh with the actual path where the script is saved.
- Save the cron table and exit the editor.
Now, the script will run automatically as a cronjob every day at noon, classifying files in the specified input folder or the default ~/Downloads folder.