user2335924
user2335924

Reputation: 55

list file that match time range using unix time stamp

These files contains unix time as part of their name. these files are as below:

1.1437039767.xml 1.1437039614.xml etc..

Here is my bash function; I am trying to list only files that their name are between Today and tomorrow in unix time.

list () {

D=$(date "+%m/%d/%Y 00:00:00")
d=$(date -d "${D}" +%s)
CURRENT=$(date "+%m/%d/%Y %H:%M:%S")
u=$(date -d "${CURRENT}" +%s)

LS=$(ls -1A "${1}"* | sed -e "s/1.//" -e "s/.xml$//")

for file in ${LS};
do
if [ "${file}" -gt "${d}" -a "${file}" -lt "${u}" ];
then
echo "1.${file}.xml"
fi
done
}

list ${1}

This function gives such of error

[: 16-07-15/1437025363: integer expression expected

Any help :)

Adding the output with the -x

Here is my command including ${1}:

./test.sh /path/

+ '[' path/1437043449 -gt 1437001200 -a path/1437043449 -lt 1437057123 ']'
./test.sh: line 17: [: path/1437043449: integer expression expected

Thank you. Al.

Upvotes: 0

Views: 213

Answers (1)

meuh
meuh

Reputation: 12255

Your problem is that you are leaving the directory name in the numbers you try to compare, eg path/1437043449 -gt 1437001200 when you should be comparing simply 1437043449 -gt 1437001200.

I've edited your script to use expr instead of sed to extract just the number part from the complete pathname (the part inside \(...\)).

list () {
    D=$(date "+%m/%d/%Y 00:00:00")
    d=$(date -d "${D}" +%s)
    u=$(date +%s)

    for file in $(ls -1A "${1}"*)
    do if    number=$(expr "$file" : '.*\.\([0-9]*\)\.xml$')
       then  if [ "$number" -ge "${d}" -a "$number" -le "${u}" ]
             then   echo "$file"
             fi
       fi
    done
}
list ${1}

Upvotes: 1

Related Questions