Skip to content

Instantly share code, notes, and snippets.

@riturajborpujari
Created December 29, 2025 14:36
Show Gist options
  • Select an option

  • Save riturajborpujari/4c687608f3726df116592ef9f69634a7 to your computer and use it in GitHub Desktop.

Select an option

Save riturajborpujari/4c687608f3726df116592ef9f69634a7 to your computer and use it in GitHub Desktop.
Bash script for File Categorization (naive and fast)
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
DIR="${1:-.}"
DIR="$(cd "$DIR" && pwd)"
mkdir -p "$DIR"/{images,videos,audio,documents,archives,code,others}
move() {
dest="$1"
shift
find "$DIR" -maxdepth 1 -type f \( "$@" \) -print0 |
xargs -0 -rn 1000 mv -n -t "$DIR/$dest"
}
# Images
move images \
-iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \
-o -iname '*.gif' -o -iname '*.webp' -o -iname '*.svg' \
-o -iname '*.bmp'
# Videos
move videos \
-iname '*.mp4' -o -iname '*.mkv' -o -iname '*.avi' \
-o -iname '*.mov' -o -iname '*.webm'
# Audio
move audio \
-iname '*.mp3' -o -iname '*.wav' -o -iname '*.flac' \
-o -iname '*.ogg' -o -iname '*.m4a'
# Documents
move documents \
-iname '*.pdf' -o -iname '*.doc' -o -iname '*.docx' \
-o -iname '*.xls' -o -iname '*.xlsx' \
-o -iname '*.ppt' -o -iname '*.pptx' \
-o -iname '*.txt' -o -iname '*.md'
# Archives
move archives \
-iname '*.zip' -o -iname '*.tar' -o -iname '*.gz' \
-o -iname '*.bz2' -o -iname '*.xz' \
-o -iname '*.7z' -o -iname '*.rar'
# Code
move code \
-iname '*.c' -o -iname '*.h' -o -iname '*.go' \
-o -iname '*.js' -o -iname '*.ts' -o -iname '*.py' \
-o -iname '*.sh' -o -iname '*.rs' -o -iname '*.java'
## remaining to others subdirectory
find "$DIR" -maxdepth 1 -type f -print0 |
xargs -0 -r -n 1000 mv -n -t "$DIR/others"
#!/usr/bin/env bash
set -euo pipefail
DIR="${1:-.}"
DIR="$(cd "$DIR" && pwd)"
# Pre-create directories
mkdir -p "$DIR"/{images,videos,audio,documents,archives,code,others}
shopt -s nullglob
for file in "$DIR"/*; do
[[ -f "$file" ]] || continue
name="${file##*/}"
ext="${name##*.}"
ext="${ext,,}"
case "$ext" in
jpg|jpeg|png|gif|webp|svg|bmp)
dest=images
;;
mp4|mkv|avi|mov|webm)
dest=videos
;;
mp3|wav|flac|ogg|m4a)
dest=audio
;;
pdf|doc|docx|xls|xlsx|ppt|pptx|txt|md)
dest=documents
;;
zip|tar|gz|bz2|xz|7z|rar)
dest=archives
;;
c|h|go|js|ts|py|sh|rs|java)
dest=code
;;
*)
dest=others
;;
esac
mv -n -- "$file" "$DIR/$dest/"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment