PDA

View Full Version : Timestamps?



earobinson111
22nd September 2004, 03:48 AM
Anyone know the command to get the modification time of a file on my hard drive?

imdeemvp
22nd September 2004, 03:50 AM
i think if it gets moved to another directory......it gives a new timestamp, and i was just reading about 3 days ago in the linux cook book.

EDITED, misread question. you want to extract the files info?

jayemef
22nd September 2004, 03:57 AM
The easiest way to find that out would be to simply open your file browser and change the view to details. If you want to do it in terminal, I'm pretty sure

ls -l
will show you the timestamp also.

Hope that's what you were looking for. I might have misunderstood...

imdeemvp
22nd September 2004, 04:02 AM
yes, that's it.... ls -l

earobinson111
22nd September 2004, 04:25 AM
thanks, but what im looking to do is to write a scrypt that renames the files acording to there last modified time, and i cant seem to extract the data from ls, right now im using ls-o, but i cant seem to use cut on that for some reason

Chas.H
22nd September 2004, 04:32 AM
See this in terminal...

man touch

touch is a utility for fiddling timestamps and the like.

earobinson111
22nd September 2004, 04:37 AM
the man page says touch is only for editing a mod time

jayemef
22nd September 2004, 04:41 AM
Hmm, I think touch is used to change the timestamps to the current time. I'm not exactly sure though.

Thinking about it a bit, one way you could probably do it is to first output the information to a file. You could do this with

script
ls -o
exit
The information will now be saved in a file called typescript. Now you can isolate the fields. This probably isn't the best way, but I think it would work.

earobinson111
22nd September 2004, 04:42 AM
try it i cant get it to work

jayemef
22nd September 2004, 04:44 AM
Could you be more specific please on what you can't get to work? :)

earobinson111
22nd September 2004, 04:46 AM
well i can list the files but i can use cut -f3 to get feild 3

Chas.H
22nd September 2004, 05:01 AM
the man page says touch is only for editing a mod time
I think that would be touch -m filename
Touch without parameters hits them all
I think :rolleyes:

earobinson111
22nd September 2004, 05:32 AM
that gets no output
[earobinson@user78-213 test]$ ls
new file 1 new file 2 renamer tester
[earobinson@user78-213 test]$ touch -m tester
[earobinson@user78-213 test]$

earobinson111
22nd September 2004, 04:18 PM
that gets no output
[earobinson@user78-213 test]$ ls
new file 1 new file 2 renamer tester
[earobinson@user78-213 test]$ touch -m tester
[earobinson@user78-213 test]$

but thanks all the same for trying

Chas.H
22nd September 2004, 06:43 PM
It's not meant to give output. It changes the dates silently. After doing that do:
ls -l
and you will see the date changes to the moment you ran touch.

earobinson111
23rd September 2004, 06:09 AM
no i want to rename my files to there mod times using a shell scrypt

earobinson111
23rd September 2004, 09:09 PM
no i want to rename my files to there mod times using a shell scrypt, i think a ls -l |cut may do it but im not sure

RedFedora
23rd September 2004, 10:04 PM
If you want to get the mod time on a file, you have to remember that the system breaks it
down into month, day and (sometimes) time. If you want the date only, for example, try awk.

For example:
ls -l | awk '{print $6$7$8}'

This will print out the month, day and year the file was modified.
You can add spacing between the "$6", "$7" and "$8" for a more
human readable output.
The numbers 6, 7 & 8 represent the field (collum) in the
output from "ls -l".

Now, if you want to change the filename to the month, day and year,
then try this:

ls -l | awk ' { system("mv " $9 " " $6$7$8) }'

This should, I think, change the name of each file in the current directory
from its original name to the date it was last modified. There is, of course,
a catch. If you have two files that where modified on the same date and
in the same minute, one will be lost when you do this. I recommend
being very careful doing a mass renaming of your files using the date and time

A slightly saver method may be this:

ls -l | awk ' { system("mv " $9 " " NR$6$7$8) }'

This should change the file names from their original
name to a unique number, followed by the date/time it
was last modified. The "NR" is the number of
files that awk has currently processed.

Best of luck.

earobinson111
23rd September 2004, 10:12 PM
thanks a lot

and if i wanted yyyy mm dd hh:mm:AM/PM.jpg? for example 2004 06 12 12:03:AM.jpg

RedFedora
24th September 2004, 01:47 AM
There I see two possibilities. The first being that you know that all of your files are
either mp3 or jpg or whatever extensions. If that is the case, then put these lines in
a script for ease of use:

ls -l *.jpg | awk ' { system("mv " $9 " " NR$6$7$8 ".jpg") }'
ls -l *.mp3 | awk ' { system("mv " $9 " " NR$6$7$8 ".mp3") }'
etc etc

However, if you do not know the extensions of your files, then we're
getting a little carried away here. You might consider the three following:
1. Write a C program to do this. (probably not)
2. Get a copy of Krename. (quick and easy)
3. Use the file's orginal name after the date. (simple)
For example:

ls -l | awk ' { system("mv " $9 " " $6$7$8 "-" $9) }'

Which renames the files like so. File abc.jpg would become Sept222004-abc.jpg and so on.

Hope this helps.

earobinson111
24th September 2004, 06:45 PM
hum cant seem to get the format i want with any of those options
and krenamer throws a cantrename error

earobinson111
25th September 2004, 06:44 PM
hum cant seem to get the format i want with any of those options
and krenamer throws a cant rename error