-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory_cleanup
More file actions
executable file
·36 lines (34 loc) · 1.06 KB
/
directory_cleanup
File metadata and controls
executable file
·36 lines (34 loc) · 1.06 KB
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
33
34
35
#!/usr/bin/env bash
# a Bash script that cleans up a directory by organizing
#+ files into subdirectories based on their file extensions.
# read input from user, 'r' option to treat backslashes as literal characters
read -rp "Input Directory Path: " dir
if [ ! -d "$dir" ];then
echo "$dir not a directory Path"
else
# create subdirectories. Use 'mkdir -p' to create parent directories if they do not exist
#+ but this script will be focused on directories that already exist
# subdirectories 'a' and 'b' are just examples to test the script. They can be changed, depending on the type of files
#+ and file extensions you're working with
mkdir -p "a" "b" "c" "others"
for file in "$dir"/*;do
# check if 'file' is a regular file and not an executable file(dso program does not move the executable file)
if [ -f "$file" ] && [ ! -x "$file" ];then
extension=${file##*.}
case "$extension" in
a)
mv "$file" "$dir/a/"
;;
b)
mv "$file" "$dir/b/"
;;
c)
mv "$file" "$dir/c/"
;;
*)
mv "$file" "$dir/others/"
;;
esac
fi
done
fi