LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-20-2009, 02:32 AM   #1
kevinyeandel
Member
 
Registered: Jun 2008
Posts: 49

Rep: Reputation: 16
script Q : how do I force bash to perform the math?


Hi

I want to know how old a file is so I've reduced the problem to give me two timestamps but:

echo `date +%s` - `stat -c %Z howoldisthisfile.txt`

gives

1235114433 - 1235093198

instead of

21235

I've tried () around various parts but just end up with syntax errors.

How do I persuade bash to give up the result without assigning to variables.

Many thanks
kevin
 
Old 02-20-2009, 03:15 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
The mathematical operation syntax for bash is $(( )). I also recommend $() for command nesting instead of using the ` backticks

Code:
echo "$(( $(date +%s) - $(stat -c %Z howoldisthisfile.txt) ))"
You do realize that on posix filesystems there is no "created time" flag, only a "last modified" one, I hope. It's impossible to know with certainty how old a file actually is, only how long it's been since it was last changed.
 
Old 02-20-2009, 03:22 AM   #3
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 306

Rep: Reputation: 38
Expr way:
Code:
expr `date +%s` - `stat -c %Z howoldisthisfile.txt`
It's not pure bash as you need bc, but this'll work too:
Code:
echo "`date +%s`-`stat -c %Z howoldisthisfile.txt`" | bc
 
Old 02-20-2009, 03:24 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,693
Blog Entries: 54

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Code:
# quick
echo $[`date +%s` - `stat -c %Z howoldisthisfile.txt`]
# precise
echo `date +%s` - `stat -c %Z howoldisthisfile.txt`|bc -l
# Full M/C time
date --date="$(echo $[$(date +%s) - $(stat -c %Z .Xdefaults)] seconds ago)"
 
Old 02-20-2009, 03:35 AM   #5
kevinyeandel
Member
 
Registered: Jun 2008
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by unSpawn View Post
Code:
# quick
echo $[`date +%s` - `stat -c %Z howoldisthisfile.txt`]
# precise
echo `date +%s` - `stat -c %Z howoldisthisfile.txt`|bc -l
# Full M/C time
date --date="$(echo $[$(date +%s) - $(stat -c %Z .Xdefaults)] seconds ago)"
Thank you all for your help - quote a lot learned from your collective contributions!

Kevin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Force Script (Bash) to Edit File? carlosinfl Linux - General 3 10-02-2008 11:57 AM
bash simple math (division) question babag Programming 3 04-03-2008 06:24 PM
helping me in a bash script that perform a "select" menus Task adam_blackice Programming 5 09-15-2007 02:09 PM
how do i use bash to perform an operation on multiple files Brynn Linux - Newbie 6 07-24-2006 07:02 AM
bash and math division problem bennethos Programming 5 10-17-2004 02:51 PM


All times are GMT -5. The time now is 12:45 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration