forked from iiitl/bash-practice-repo-24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extarrange
executable file
·33 lines (26 loc) · 879 Bytes
/
extarrange
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
# Arranges the files by grouping them into folder of their extension name
# TODO: Run this script in your downloads folder
require() {
command -v "$1" 1>/dev/null || { >&2 echo "Error: $1 is not installed";exit 1; }
}
require fd
require egrep
declare -A special_cases=(
['^*.(tar.*|tbz)$']='tar'
# TODO: Add your own pattern here for similar file types appropriately
# Refer https://regex101.com/ & https://github.com/ziishaned/learn-regex/ for explaination and writing patterns
# These (regex) are same as ones used in sed command
)
fd -tf -d1 \
| while read file; do
folder="$PWD/${file##*.}"
for regex in "${!special_cases[@]}"; do
(echo "$file" | egrep -q "$regex") && {
folder="$PWD/${special_cases[$regex]}"
break
}
done
mkdir -p "$folder"
mv "$file" "$folder"
done