-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal-alpha-v2.bash
executable file
·253 lines (229 loc) · 8.38 KB
/
final-alpha-v2.bash
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
path="${1:-.}"
cd "$path" || exit 1
# MENUS SECTION
## MAIN MENU
main-menu(){
clear
while true; do
echo "==== FiDir - File Sort with Filters ===="
echo "1. Sort and display"
echo "2. Filter and display"
echo "3. exit"
echo "========================================"
read -p "Enter your choice: " choice
clear
case $choice in
1) sort-menu
;;
2) filter-menu
;;
3) exit 0
;;
*) echo "Invalid choice, try agin... "
sleep 2
clear
;;
esac
done
}
## SORT MENU
sort-menu(){
clear
while true; do
echo "=================================================="
echo "1. Display files and directories by size"
echo "2. Display files by file type"
echo "3. Sort files/directories by date created"
echo "4. List files/directories by last modified date"
echo "5. Back"
echo "6. Exit"
echo "=================================================="
read -p "Enter your choice: " schoice
clear
case $schoice in
1) display_files_by_size
;;
2) display_files_by_type
;;
3) display_files_by_date_created
;;
4) display_files_by_last_modified
;;
5) main-menu
;;
6) exit 0
;;
*) echo "Invalid choice. Please try again."
;;
esac
done
}
## FILTER MENU
filter-menu(){
clear
while true; do
echo "=================================================="
echo "1. Filter with file type"
echo "2. Filter by permission"
echo "3. Back"
echo "4. exit"
echo "=================================================="
read -p "Enter your choice: " fchoice
clear
case $fchoice in
1) read -p "Enter the file type: " file_type
list_files "$file_type"
;;
2) read -p "Enter permission name (eg. write): " perm
list_files_by_permission $perm
;;
3) main-menu
;;
4) exit 0
;;
*)
echo "Invalid choice. Please select a valid option."
;;
esac
done
}
########################## SORT FUNCTIONS SECTION - START ##########################
# Function to display files and directories by size using du command
display_files_by_size() {
echo "Sort by Size:"
echo "1. Sort from large to small size"
echo "2. Sort from small to large size"
read -p "Enter your choice: " sort_choice
if [ "$sort_choice" = "1" ]; then
sort_option="-k 1,1hr"
elif [ "$sort_choice" = "2" ]; then
sort_option="-k 1h"
else
echo "Invalid choice. Defaulting to big to small size sort."
sort_option="-k 1,1hr"
fi
echo "------------------------ Sort by Size -----------------------"
echo "Size Date (Last Modified) File/Dir Name"
echo "-------------------------------------------------------------"
for file in *; do
if [ -f "$file" ]; then
file_size=$(du -h "$file" | cut -f1)
last_modified_date=$(stat -c "%y" "$file")
printf "%-16s %-20s %s\n" "$file_size" "${last_modified_date%% *}" "$file"
elif [ -d "$file" ]; then
dir_size=$(du -h -s "$file" | cut -f1)
last_modified_date=$(stat -c "%y" "$file")
printf "%-16s %-20s %s\n" "$dir_size" "${last_modified_date%% *}" "$file"
fi
done | sort $sort_option
}
# Function to display files by file type
display_files_by_type() {
local file_types=("jpg|jpeg|png|gif|bmp" "mp4|avi|mkv|mov" "txt" "sh")
for type in "${file_types[@]}"; do
files=($(find $path -maxdepth 1 -type f -name "*.$type"))
if [ ${#files[@]} -gt 0 ]; then
echo "========================= ${type^^} Files ========================="
echo "Size Date (Last Modified) File/Dir Name"
echo "-------------------------------------------------------------"
for file in "${files[@]}"; do
file_date=$(stat -c "%y" "$file")
file_size=$(du -sh "$file" | cut -f1)
echo -e "$file_size\t\t${file_date%% *}\t\t$file"
done
fi
done
# Handle other file types
other_files=($(find $path -maxdepth 1 -type f -not -name "*.*"))
if [ ${#other_files[@]} -gt 0 ]; then
echo "======================== Other Files ========================"
echo "Size Date (Last Modified) File/Dir Name"
echo "-------------------------------------------------------------"
for file in "${other_files[@]}"; do
file_date=$(stat -c "%y" "$file")
file_size=$(du -sh "$file" | cut -f1)
echo -e "$file_size\t${file_date%% *} $file"
done
fi
}
# Function to sort and display files/directories by date created
display_files_by_date_created() {
echo "-------------------- Sort by Date Created -------------------"
echo "Size Date (Created) File/Dir Name"
echo "-------------------------------------------------------------"
for file in *; do
if [ -f "$file" ] || [ -d "$file" ]; then
file_date=$(stat -c "%w" "$file")
file_size=$(du -sh "$file" | cut -f1)
printf "%-16s %-20s %s\n" "$file_size" "${file_date%% *}" "$file"
fi
done | sort -r -k 2
}
# Function to list files/directories by last modified date
display_files_by_last_modified() {
echo "----------------- Sort by Last Modified Date ----------------"
echo "Size Date (Last Modified) File/Dir Name"
echo "-------------------------------------------------------------"
for file in *; do
if [ -f "$file" ] || [ -d "$file" ]; then
last_modified_date=$(stat -c "%y" "$file")
file_size=$(du -sh "$file" | cut -f1)
printf "%-16s %-20s %s\n" "$file_size" "${last_modified_date%% *}" "$file"
fi
done | sort -r -k 2
}
########################### SORT FUNCTIONS SECTION - END ###########################
######################### FILTER FUCNTIONS SECTION - START #########################
# Function to list files of a specific type
list_files() {
local file_type="$1"
local files=($(find $path -maxdepth 1 -type f -name "*.$file_type"))
if [ ${#files[@]} -gt 0 ]; then
echo "======================= $file_type Files ======================="
echo "Size Date (Last Modified) File Name"
echo "-------------------------------------------------------------"
for file in "${files[@]}"; do
file_date=$(stat -c "%y" "$file")
file_size=$(du -sh "$file" | cut -f1)
file_name=$(basename "$file")
echo -e "$file_size\t\t${file_date%% *}\t\t$file_name"
done
else
echo "No $file_type files found in the directory."
fi
}
list_files_by_permission() {
local permission_name="$1"
if [ "$permission_name" == "read" -o "$permission_name" == "r" ]; then
permission_name="readable"
permission="r"
elif [ "$permission_name" == "write" -o "$permission_name" == "w" ]; then
permission="w"
permission_name="writable"
elif [ "$permission_name" == "execute" -o "$permission_name" == "x" ]; then
permission="x"
permission_name="executable"
else
echo "Invalid permission name. Please use 'read', 'write', or 'execute'."
return
fi
local files=($(find $path -maxdepth 1 -type f -name "*"))
if [ ${#files[@]} -gt 0 ]; then
echo "====================== $permission_name files ======================"
echo "Size Date (Last Modified) File Name"
echo "-------------------------------------------------------------"
for file in "${files[@]}"; do
if [ -$permission "$file" ]; then
file_date=$(stat -c "%y" "$file")
file_size=$(du -sh "$file" | cut -f1)
file_name=$(basename $file)
echo -e "$file_size\t\t${file_date%% *}\t\t$file_name"
fi
done
else
echo "No ${permission_name^}able files found in the directory."
fi
}
########################## FILTER FUCNTIONS SECTION - EHD ##########################
main-menu