-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfl.sh
More file actions
executable file
·135 lines (112 loc) · 3.85 KB
/
fl.sh
File metadata and controls
executable file
·135 lines (112 loc) · 3.85 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
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
#!/bin/bash
###################################################################
# The program is intended to find the last entry in the directory #
# #
# Author: Brent Barbachem #
# Date: July 19, 2021 #
###################################################################
# Define some colors based on their ANSI values
ERROR_COLOR='\033[0;31m' # Red
WARN_COLOR='\033[1;33m' # Yellow
NORM_COLOR='\033[0;37m' # Light Grey
NO_COLOR='\033[0m' # No Color
PrintError()
{
if [[ "$-" == *x* ]]; then
printf "${ERROR_COLOR}${1}${NORM_COLOR}\n"
fi
}
PrintWarning()
{
if [[ "$-" == *x* ]]; then
printf "${WARN_COLOR}${1}${NORM_COLOR}\n"
fi
}
FindLastModifiedFile()
{
# Find the last modified file
# :param $1: List of files
# :return: Name of the last mofidied from the selection
# If no files provided, an empty string is returned
local last_updated_file=""
local last_update="0"
a=("$@")
#for filename in "${a[@]}"; do
for filename in $a; do
file_date=$(date -r $filename "+%m-%d-%Y %H:%M:%S")
if [ "$file_date" \> "$last_update" ]; then
last_updated_file=$filename
last_update=$file_date
fi
done
echo $last_updated_file
}
_Help()
{
# Function to display the help information about the program
echo "Usage: fl [OPTION]..."
echo # Blank Line - Do NOT Remove
echo "Find the most recent entry for a file in the directory."
echo # Blank Line - Do NOT Remove
echo " -d, --directory Directory to search [Default to current (.)."
echo " -f, --file_text Text to search for in the filenames. "
echo " -x Print Debugging Statements. "
echo # Blank Line - Do NOT Remove
}
while getopts ":h" help_option; do
case $help_option in
h) _Help; exit;;
esac
done
# unset the options find that was already performed
# This environment variable is only set once [manually]
# each time that getopts is called.
unset OPTIND
# Parameters for the life of the program
_directory="."
_file_search_text=""
while getopts ":d:f:x" option; do
case $option in
d) _directory=$OPTARG;;
f) _file_search_text=$OPTARG;;
x) set -x;;
esac
done
# bash version, for future use in the event of bash version changes
# only care about the major changes to bash
curr_bash_version=${BASH_VERSION:0:1}
PrintWarning "Using Bash ${BASH_VERSION}"
# find all data in the choice directory. Then we can search the results
# to find the data that we are looking for
ls_output=$(find $_directory -maxdepth 1 -type f -exec ls -h {} +)
if [ "$_file_search_text" = "" ]; then
# when no variable was set find the last modified file in the directory
last_updated_file=$(FindLastModifiedFile "$ls_output")
if [ "$last_updated_file" = "" ]; then
PrintError "No files found"
else
echo $last_updated_file
fi
else
SAVED_NO_CASE_MATCH=$(shopt -p nocasematch; true) # save the current state
shopt -s nocasematch # just in case, remove case sensitive match
# variable was set, do a text comparison to see what is available and select
# the last one from that list
ret_array=()
for x in ${ls_output[@]}; do
if [[ "$x" == *"$_file_search_text" ]]; then
ret_array+=($x)
fi
done
eval $SAVED_NO_CASE_MATCH # reset the original value
# This will grab the last `modified` file. SO Please DO NOT
# modify the files and expect a result of finding the last
# created file.
PrintWarning "Searching for last modified file in ${_directory}"
last_updated_file=$(FindLastModifiedFile "${ret_array[*]}")
if [ "$last_updated_file" = "" ]; then
PrintError "No files found"
else
echo $last_updated_file
fi
fi