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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-20-2012, 08:09 AM
|
#1
|
Member
Registered: Jul 2012
Posts: 30
Rep: 
|
How to find the time difference in min
Hi,
I want to get the difference of two time stamps in min.
Ex:
a= Jul 20 08:07:36
b= Jul 20 08:09:09
I want the difference in minutes.
Please help me to get through it.
Thanks in Advance.. :-)
|
|
|
07-20-2012, 08:35 AM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602
|
Quote:
Originally Posted by pradeepdee6
Hi,
I want to get the difference of two time stamps in min.
Ex:
a= Jul 20 08:07:36
b= Jul 20 08:09:09
I want the difference in minutes. Please help me to get through it.
Thanks in Advance.. :-)
|
You don't provide any details. Help you through it HOW? What are you trying to do this in (bash? perl? python?) What's your goal?? And what have you done/tried so far??
Based on your last thread, you didn't put any effort into your problem, but asked others to solve it for you...and this sounds very much like homework.
|
|
|
07-20-2012, 08:45 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
and this sounds very much like homework.
|
+1
pradeep*;
You have more replies in your other thread---it is good practice to follow thru on existing threads before starting new ones.
Quote:
I want the difference in minutes.
|
1.55 minutes
|
|
1 members found this post helpful.
|
07-20-2012, 12:14 PM
|
#4
|
Senior Member
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144
|
Quote:
Originally Posted by pixellany
+1
pradeep*;
You have more replies in your other thread---it is good practice to follow thru on existing threads before starting new ones.
1.55 minutes
|
Well, I don't know my math if it is good, but this seems wrong to me !?
Isn't it 1:33 minutes ?
so I wrote a script:
Code:
#!/bin/sh
a="Jul 20 08:07:36"
b="Jul 20 08:09:09"
# convert date/time to Epoch
epa=$(date -d"$a" +%s)
epb=$(date -d"$b" +%s)
#do the math
tdiff=$(($epb-$epa))
#convert to human date/time format
th=$(date -d "1970-01-01 $tdiff sec" +"%H:%M:%S")
echo "Time difference (Hours:Min:Sec): $th / ($tdiff seconds)"
### OUTPUT
Time difference (Hours:Min:Sec): 00:01:33 / (93 seconds)
confirmed time difference is: 93 seconds
so that is 1 minute (60 sec) and 33 seconds to me.
I hope it helps.
good luck
Last edited by lithos; 07-20-2012 at 12:17 PM.
|
|
|
07-20-2012, 12:30 PM
|
#5
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
01:33 = 1 minute, 33 seconds = 1.55 minutes
seriously, as long as we are doing the OP's homework, I wonder if there is not a simpler solution...
|
|
|
07-20-2012, 12:35 PM
|
#6
|
Senior Member
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144
|
Oh,
you mean 1.55 where a minute is in 100's not 60 seconds (.50 = 30 seconds, 100 = 60sec/1min).
Now I got it. I'm sorry...
|
|
|
07-20-2012, 02:31 PM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602
|
Quote:
Originally Posted by pixellany
01:33 = 1 minute, 33 seconds = 1.55 minutes
seriously, as long as we are doing the OP's homework, I wonder if there is not a simpler solution...
|
I think the OP should post the simpler solution, since we've now done TWO of his homework assignments for him.
|
|
|
07-23-2012, 03:14 AM
|
#8
|
Member
Registered: Jul 2012
Posts: 30
Original Poster
Rep: 
|
Hi Everyone. Thank u so much for your answers
Hi,
Im new here in the forum. So learning form you all seniors here.
Actual Issue is: I need to get the list of files in a ftp process and change the permissions of those files alone.
Problem i was facing was, sftp should perform "mget" method to a common directory. And search string was like "mget ABC*". So after ftping If i grep for the files with the string ABC* it may even select other preexixting files which would match.
I tried using "sed" command,(In other post) it was working but not accurate { I used ls -lrt before and after FTP process and was gathering the difference in lists}
So My Idea was to get the time difference and to list the files FTPed with in that time limit, As i had find command {find . -mmin -$min}
So thought of applying the time difference.
Im working in "bash"
Ex of My Code format:
DATE1=date
FTP process
DATE2=date
TIME_DIFF=(DATE1-DATE2) # which i wanted in min to use find command
find . -mmin -$TIME_DIFF # find . -newermt "2012-07-23 13:42" doesnt work in bash :-(
Really sorry if im not clear. I ll try to Improve myself.
Thank you so much in Advance.
|
|
|
07-23-2012, 05:11 AM
|
#9
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
In BASH, I have not seen a solution simpler than what lithos posted.
In Python, there are some useful routines in the "datetime" module.
|
|
|
07-23-2012, 06:01 AM
|
#10
|
Member
Registered: Jul 2012
Posts: 30
Original Poster
Rep: 
|
Im Close to the solution, Help me
Hi everyone.. Thanku very much for all the posts.
I just found one line code for my problem.
In previous post i was trying to get the difference of time and list the files.
Now that can be achieved by an ready made option of "find" command.
Ex:
find . -newer "File Name" # where File Name should contain the Time from which i should list.
# This can be achieved By creating a file with date command in it.
But My problem is: Should not create and extra file as we are most concerned about the performance and code optimization.
So I can not create a new file "File Name"
Can anyone help me to solution.
|
|
|
07-23-2012, 08:43 AM
|
#11
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602
|
Quote:
Originally Posted by pradeepdee6
Hi everyone.. Thanku very much for all the posts.
I just found one line code for my problem. In previous post i was trying to get the difference of time and list the files. Now that can be achieved by an ready made option of "find" command.
Ex:
find . -newer "File Name" # where File Name should contain the Time from which i should list.
# This can be achieved By creating a file with date command in it.
But My problem is: Should not create and extra file as we are most concerned about the performance and code optimization. So I can not create a new file "File Name"
Can anyone help me to solution.
|
Again, post what you've written/tried so far. Your posts thus far have all seemed like homework questions, and you've not provided any evidence that you've tried to do this, or shown what error(s) you've got. As with any programming task, you can either create a file, or put data into a variable/array for later use. There is abundant documentation on how to do this that you can find with a brief Google search.
|
|
|
07-23-2012, 10:04 AM
|
#12
|
Member
Registered: Jul 2012
Posts: 30
Original Poster
Rep: 
|
Hi TB0ne
These are few things i have worked out and gives required answer for my local setup.
Quote:
vari=`ls -1t | head -1`
echo $vari # Here I ll be getting latest modified file name
FTP PART
MYFILES=`ls -lt | sed -n "1,/${vari}/p" | awk {'print $9'} | grep -v $vari `
|
But i need to work it out with production where large number of files get created and get deleted simultaniously. So it wont be efficient and dependable.
Then tried with capturing the log for FTP part. Faced an issue for creating an extra file.
Quote:
FTP Part > logfile # will capture the files FTPed
MYFILES=`cat logfile | grep -i "Fetching" | awk {'print $4'}`
|
Then worked with sed command as in other post.
And now im trying with the above find command.
Now Im trying to use the batch file which is already being used in the script. So that i can feed input to the find command.
Quote:
BATCHFILE=/tmp/mftget.$CONSUMER.$PRODUCER.$TIMESTAMP.$$.tmp
echo "cd /$PRODUCER/Inbox
$GETFILES
bye" > $BATCHFILE # modifying Batch file time stamp
FTP Part
find . -newer $BATCHFILE # Using the Batch file as latest modified file
|
Thanks for your help and guidance.
Last edited by pradeepdee6; 07-23-2012 at 10:09 AM.
|
|
|
07-23-2012, 12:01 PM
|
#13
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602
|
Quote:
Originally Posted by pradeepdee6
Hi TB0ne
These are few things i have worked out and gives required answer for my local setup.
But i need to work it out with production where large number of files get created and get deleted simultaniously. So it wont be efficient and dependable. Then tried with capturing the log for FTP part. Faced an issue for creating an extra file.
Then worked with sed command as in other post. And now im trying with the above find command. Now Im trying to use the batch file which is already being used in the script. So that i can feed input to the find command.
|
Ok, so use the Unix epoch date in a variable in your script. That is VERY fine grained, and the chances of two files being created in the EXACT same thousandth of a second is very small. That'll give you a unique time stamp. Then convert that into a 'regular' date, and save your file. A simple check would tell you if that file already exists, and you can append something to the date, like a "(01)", so you can have a file called "2012072073258(01).txt", and "20120720073258(02).txt"...two files same date/time/second, with SLIGHTLY different names.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 01:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|