-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrecently_modified_files.sh
executable file
·219 lines (197 loc) · 5.65 KB
/
recently_modified_files.sh
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
#!/usr/bin/env bash
# Script Name: recently_modified_files.sh
# Description: Lists the most recently modified files in a given directory with advanced options.
# Usage: recently_modified_files.sh [options]
#
# Options:
# -d, --directory DIR Specify the directory to search (default: current directory).
# -n, --number N Number of files to list (default: 10).
# -t, --time TYPE Time to sort by: mtime (modification), atime (access), ctime (change) (default: mtime).
# -r, --reverse Reverse the sort order.
# -e, --exclude PATTERN Exclude files matching PATTERN.
# -i, --include PATTERN Include only files matching PATTERN.
# -l, --log-file FILE Log output to specified file.
# -v, --verbose Enable verbose output.
# -h, --help Display this help message.
#
# Examples:
# recently_modified_files.sh -d /home/user/documents -n 5
# recently_modified_files.sh --time atime --reverse
# recently_modified_files.sh -i '*.txt' -e '*.log'
set -euo pipefail
# Default configurations
DIRECTORY="."
NUMBER=10
TIME_TYPE="mtime"
REVERSE=false
EXCLUDE_PATTERN=""
INCLUDE_PATTERN=""
LOG_FILE=""
VERBOSE=false
# Function to display usage information
usage() {
cat << EOF
Usage: $0 [options]
Options:
-d, --directory DIR Specify the directory to search (default: current directory).
-n, --number N Number of files to list (default: 10).
-t, --time TYPE Time to sort by: mtime (modification), atime (access), ctime (change) (default: mtime).
-r, --reverse Reverse the sort order.
-e, --exclude PATTERN Exclude files matching PATTERN.
-i, --include PATTERN Include only files matching PATTERN.
-l, --log-file FILE Log output to specified file.
-v, --verbose Enable verbose output.
-h, --help Display this help message.
Examples:
$0 -d /home/user/documents -n 5
$0 --time atime --reverse
$0 -i '*.txt' -e '*.log'
EOF
}
# Function for logging
log() {
local message="$1"
if [[ "$VERBOSE" == true ]]; then
echo "$message"
fi
if [[ -n "$LOG_FILE" ]]; then
echo "$message" >> "$LOG_FILE"
fi
}
# Parse command-line arguments
ARGS=("$@")
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--directory)
if [[ -n "${2-}" ]]; then
DIRECTORY="$2"
shift 2
else
echo "Error: --directory requires a non-empty argument."
exit 1
fi
;;
-n|--number)
if [[ -n "${2-}" ]]; then
NUMBER="$2"
shift 2
else
echo "Error: --number requires a non-empty argument."
exit 1
fi
;;
-t|--time)
if [[ -n "${2-}" ]]; then
TIME_TYPE="$2"
shift 2
else
echo "Error: --time requires a non-empty argument."
exit 1
fi
;;
-r|--reverse)
REVERSE=true
shift
;;
-e|--exclude)
if [[ -n "${2-}" ]]; then
EXCLUDE_PATTERN="$2"
shift 2
else
echo "Error: --exclude requires a non-empty argument."
exit 1
fi
;;
-i|--include)
if [[ -n "${2-}" ]]; then
INCLUDE_PATTERN="$2"
shift 2
else
echo "Error: --include requires a non-empty argument."
exit 1
fi
;;
-l|--log-file)
if [[ -n "${2-}" ]]; then
LOG_FILE="$2"
shift 2
else
echo "Error: --log-file requires a non-empty argument."
exit 1
fi
;;
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
# Validate directory
if [[ ! -d "$DIRECTORY" ]]; then
echo "Error: Directory '$DIRECTORY' does not exist."
exit 1
fi
# Validate number
if ! [[ "$NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: The number of files to list must be a positive integer."
exit 1
fi
# Validate time type
if [[ "$TIME_TYPE" != "mtime" && "$TIME_TYPE" != "atime" && "$TIME_TYPE" != "ctime" ]]; then
echo "Error: Invalid time type '$TIME_TYPE'. Must be 'mtime', 'atime', or 'ctime'."
exit 1
fi
# Build find command
FIND_CMD=(find "$DIRECTORY" -type f)
# Include pattern
if [[ -n "$INCLUDE_PATTERN" ]]; then
FIND_CMD+=(-name "$INCLUDE_PATTERN")
fi
# Exclude pattern
if [[ -n "$EXCLUDE_PATTERN" ]]; then
FIND_CMD+=(! -name "$EXCLUDE_PATTERN")
fi
# Determine time format
case "$TIME_TYPE" in
mtime)
TIME_FLAG="-printf"
TIME_FORMAT='%TY-%Tm-%Td %TT %p\n'
TIME_FIELD=1
;;
atime)
TIME_FLAG="-printf"
TIME_FORMAT='%AY-%Am-%Ad %AT %p\n'
TIME_FIELD=1
;;
ctime)
TIME_FLAG="-printf"
TIME_FORMAT='%CY-%Cm-%Cd %CT %p\n'
TIME_FIELD=1
;;
esac
# Build the full command
FIND_CMD+=("$TIME_FLAG" "$TIME_FORMAT")
log "Executing command: ${FIND_CMD[*]}"
# Execute find command and sort results
if [[ "$REVERSE" == true ]]; then
SORT_ORDER=""
else
SORT_ORDER="-r"
fi
RESULTS=$( "${FIND_CMD[@]}" | sort $SORT_ORDER | head -n "$NUMBER" )
# Output results
echo "Most recently modified files in '$DIRECTORY':"
echo "$RESULTS"
# Log results
log "Found files:"
log "$RESULTS"