Som argument kan man ha "m", "min", "minute" eller "minutes", "h", "hour" eller "hours", "d", "day" eller "days", "w", "week" eller "weeks". Så man uttrycker rätt enkelt vad man vill veta....
Jag använder scriptet rätt ofta själv och det är till stor hjälp - typ när man vill veta vad det är man har förändrat under de sista två veckorna....
So, here it is:
Kod: Markera allt
#! /bin/bash
#
# -Find files modified last day
#
MAXDEPTH=1
##################################################################################################################################################
function enterExit {
if [[ $WAIT == true ]]; then
echo -n "[Press ENTER to continue]"
read IN
fi
exit
}
#
function usage {
cat << EOD
Usage: since [-m d|--max=d|--maxdepth=d] [-r|-R|--recurs|--recursive] [N] [units] [filename]
-m d|--max=d|--maxdepth=d
The maximum depth to be searched. If d is specified as 1 only the current directory is searched,
which is the default.
-r|-R|--recurs|--recursive
Specifies that the search will be done recursively up to 99 levels. If both -r and -m are used,
-r must be first, as -m implicitly includes -r up to the specified level, but if -r is specified
after, then 99 overrides the number of levels specified by -m.
Of course -m 99, --max=99 and --maxdepth=99 can also be specified which is exactly the same as -r,
but in bash it is not possible to have an option which optionally can have a parameter. So therefore
-r is just a shortcut for "-m 99" or "--maxdepth=99".
N The number of minutes, hours, days or weeks
units The unit to use, may be:
m min minute minutes
h hour hours
d day days
w week weeks
filename If wildcard is used, it must be escaped like '*.c' or \*.c
Deafult is: "since 1 day" and if just a number is specified but no unit, days are assumed.
EOD
exit
}
#
function isnum {
if [[ $1 =~ ^[0-9]+$ ]]; then
return 0
fi
return 1
}
#
#####################################################################
#
#
TEMP=`getopt -ohm:rR --long help,max:,maxdepth:,recurs,recursive -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then
usage
fi
eval set -- "$TEMP"
CMDNAME=$(basename $0)
while true; do
case $1 in
-h|--help)
usage
exit
;;
-r|-R|--recurs|--recursive)
MAXDEPTH=99
shift
;;
-m|--max|--maxdepth)
shift
MAXDEPTH=$1
shift
;;
--)
shift
break
;;
*)
#echo "-Got a star..."
shift
;;
esac
done
#P1=$1
#P2=$2
#P3=$3
#P4=$4
#
#echo "P1: $P1"
#echo "P2: $P2"
#echo "P3: $P3"
#echo "P4: $P4"
#echo "Param: $param"
#
##################################################################################################################################################
#
#if [[ $1 == "-h" ]]; then
# exit
#fi
#####
if [[ ! -z $4 ]]; then
echo
echo "-Wrong given wildcard"
echo
exit
fi
#
if [[ -z $1 ]]; then
P1=1
elif [[ $1 == 0 ]]; then
P1=1
else
isnum $1; ISNUM=$?
if [[ $ISNUM == 0 ]]; then
P1=$1
P3=$3
else
P1=1
P3=$1
fi
fi
#
if [[ -z $2 ]]; then
P2="day"
else
P2=$2
fi
#
#echo "P1=$P1"
#echo "P2=$P2"
#echo "P3=$P3"
#echo
#
# -atime - Access time days (24 hours)
# -ctime - Creation time days (24 hours)
# -mtime - Modification time days (24 hours)
# -N=less than N days, N=exactly N days, +N=more than N days
#
# -amin - Access time minutes
# -cmin - Cration time minutes
# -mmin - Modification time minutes
# -N=less than N minutes, N=exactly N minutes, +N=more than N minutes
#
echo
if [[ $P2 == "d" || $P2 == "day" || $P2 == "days" ]]; then
N=$P1
OP="-mtime"
elif [[ $P2 == "w" || $P2 == "week" || $P2 == "weeks" ]]; then
let N=7*$P1
OP="-mtime"
elif [[ $P2 == "h" || $P2 == "hour" || $P2 == "hours" ]]; then
let N=60*$P1
OP="-mmin"
elif [[ $P2 == "m" || $P2 == "min" || $P2 == "minute" || $P2 == "minutes" ]]; then
let N=$P1
OP="-mmin"
else
echo "-Unrecognized parameter $P2"
echo
exit
fi
if [[ -z $P3 ]]; then
echo find . -maxdepth $MAXDEPTH $OP -$N -ls | grep -v " .$"
find . -maxdepth $MAXDEPTH $OP -$N -ls | grep -v " .$"
else
echo find . -maxdepth $MAXDEPTH -iname "$P3" $OP -$N -ls | grep -v " .$"
find . -maxdepth $MAXDEPTH -iname "$P3" $OP -$N -ls | grep -v " .$"
fi
echo