LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2005, 09:51 PM   #1
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Rep: Reputation: 0
compare date uusing shell programming please...


let say file name is
'file_20040101.txt'
'file_20040802.txt'
'file_20041202.txt'
'file_20050101.txt"

where '20040101' is date for that file.
and todays date is 2006106.

how we compare the date?
where the ouput will remove the files that is 3 month before current date?


in this case, 'file_20040101.txt', 'file_20040802.txt' will be remove and ot
her file will remain in the directory.
pleaseeeeee.......
 
Old 01-11-2005, 11:46 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Assuming you want this in shell script, this will extract the date ie numeric part:

echo file_20050101.txt|cut -d'_' -f2|cut -d'.' -f1

so you need to loop through the filenames and compare the nums as integers.
eg:
for file in `ls -1`
do
curr_date=`echo $file|cut -d'_' -f2|cut -d'.' -f1`
if [[ $curr_date -le <3 mths ago> ]]
then
rm $file
fi
done

of course you'll need to calc <3 mths ago> first ;-)
 
Old 01-12-2005, 12:01 AM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This was fun, hope its not homework...
Code:
#!/bin/sh

# create some files to play with
touch file_20040101.txt
touch file_20040802.txt
touch file_20041202.txt
touch file_20050101.txt

# This is an approx days since 1970, but will probably be good within a few days.
convert_to_mjd() {
    local year month day
    year=${1:0:4}
    month="`echo "${1:4:2}" | sed "s/^0*//"`"  # here sed removes leading zeros
    day="`echo "${1:6:2}" | sed "s/^0*//"`"
    echo $(( (((year-1970)*1461)/4) + ((month-1)*30) + (day-1) ))
}

currentdate="`date "+%Y%m%d"`" # format YYYYMMDD, i.e. 20050111
currentdate_mjd="`convert_to_mjd "$currentdate"`"

for fn in *; do
    [ ! -f "$fn" ] && continue      # only work with regular files
    filedate="`echo "$fn" | sed -n 's/^.*_\([0-9]*\).txt$/\1/p'`"
    [ -z "$filedate" ] && continue  # ignore files that don't follow the date format
    filedate_mjd="`convert_to_mjd "$filedate"`"
    echo "The file $fn is approx $((currentdate_mjd-filedate_mjd)) days old"
done
 
Old 01-12-2005, 12:07 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
You could also use the find command man find and one of it's switches like -mtime -ctime or -atime....
For example:
Code:
#Delete old files with the following command
find /mnt/backup -type f -name '*.gz' , -name '*.log' , \
-mtime +90 -exec rm {} \;
 
Old 01-12-2005, 04:49 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I've done this sort of thing for archiving myself.

What I did was a combination.

You may as well use 'find' as homey says (use the force Luke)
also you can use 'touch' to change the timestamp
(permissions allowing)

So get the file names,

1. extract the date component
2. use 'touch' to modify the timestamp of the file
3. use 'find' to remove old files.

What can possibly go wrong?
 
Old 01-14-2005, 02:28 AM   #6
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Original Poster
Rep: Reputation: 0
thanks

sorry everybody coz i'm new in this programming language..
i've done

year=2005
month=01
day=01
hour=010101

for file in ls ATK*
do
curr_date=`echo $file|cut -d'_' -f4|cut -d'.' -f1`

#echo $curr_date
if [[ $curr_date -le $year$month$day$hour ]]
then
echo "file old"
fi
done

coz the file name will be such:
ATK_7B1_WIP_20041116160700.7.xml.dat
ATK_7B1_WIP_20041116160700.8.xml.dat
ATK_7B1_WIP_20041116160700.9.xml.dat

but it shows error like "ed3.sh[10]: ls: 0403-009 The specified number is not valid for this command. "
i donno bout it?
is it about listing or comparing the date?
helpp.
 
Old 01-14-2005, 07:20 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Different approach

Comparing dates to see if one is older than 3 months or e.g. 12 days is tricky because you'll have to compare the year, month, etc. seperately. The "date" program can also add or substract months, days, years from a date. This can be used to compare dates.

Here a bash function you can use to compare dates (in 20041219 format) in an easy, readable way:
Code:
is_older()
{
	DATE=$(date -d "$1" +%Y-%m-%d)
	TODAY=$(date -d "$(date +%Y-%m-%d) $2 $3 $4 $5" +%s)
	COMPARE=$(date -d "$DATE" +%s)
	test $(($TODAY - $COMPARE)) -le 0 && return 1 || return 0
}
You can call this function for example like this:
Code:
is_older 20041011 -3 months
is_older 20041011 -12 days
The exit code of the "is_older" function will be 0 if 20041011 is more than 3 months in the past, or more than 12 days in the past for the second example.

You can use this like this:
Code:
if is_older 20041011 -3 months ; then
	echo "Older than 3 months ago."
else 
	echo "Younger than 3 months ago."
fi

# OR:

if is_older 20050113 -3 days ; then
	echo "Older than 3 days ago."
else 
	echo "Younger than 3 days ago."
fi
To check your files (assuming the filenames are fixed in length, at least up to and including the date), you can use this:
Code:
#!/bin/bash

is_older()
{
	DATE=$(date -d "$1" +%Y-%m-%d)
	TODAY=$(date -d "$(date +%Y-%m-%d) $2 $3 $4 $5" +%s)
	COMPARE=$(date -d "$DATE" +%s)
	test $(($TODAY - $COMPARE)) -le 0 && return 1 || return 0
}

for FILENAME in ATK_*.dat ; do
	FILEDATE=$(echo $FILENAME | cut -c 13-20)
	if is_older $FILEDATE -3 months ; then
		echo "$FILENAME is older than 3 months."
	else 
		echo "$FILENAME is younger than 3 months."
	fi
done
It gave me this output (on some testing files):
Code:
heiko@hko:~/dummy$ date
Fri Jan 14 14:20:32 CET 2005

heiko@hko:~/dummy$ ./filedatum.sh 
ATK_7B1_WIP_20040920160700.9.xml.dat is older than 3 months.
ATK_7B1_WIP_20041010160700.9.xml.dat is older than 3 months.
ATK_7B1_WIP_20041013160700.9.xml.dat is older than 3 months.
ATK_7B1_WIP_20041014160700.7.xml.dat is younger than 3 months.
ATK_7B1_WIP_20041116160700.8.xml.dat is younger than 3 months.
 
Old 01-14-2005, 07:24 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Note: My function uses +%s as the date format, which is a GNU extension. So the scripts in my post require the GNU-version of the date program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM
How do you compare variable in bash programming? chynna_v Programming 6 09-08-2004 02:17 AM
date manipulation in shell peal_ss Programming 4 03-29-2004 04:43 AM
php date compare omarswan Programming 2 10-02-2002 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration