Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to pull a string from a file at certain character location 1

Status
Not open for further replies.

jisoo23

Programmer
Jan 27, 2004
192
US
Hey everyone,

I'm trying to pull a file's creation timestamp so that I can do a compare with the current system date. So far I can do this:

ls -l filename >| file.out

file.out contains a line like this:

-rw-r--r-- 1 root user 0 Oct 31 13:34 test1_103105.log

So I'm trying to pull the Oct 31 13:34 portion but I'm having trouble finding a function that will let me substring from "O" to "4". Does anyone know of one? Also, how would I actually do the compare of this substring with the system time? Is there anything similar to a datediff function available?

Thanks for the help,
Jisoo23
 
A starting point to get all the last modified Date/Time info about a file (in ksh-like script):
file="/path/to/filename"
eval $(perl -e '
($ss,$nn,$hh,$dd,$mm,$yy,$wd,$yd,$dst)=localtime((stat("'$file'"))[9]);
printf("yy=%04d mm=%02d dd=%02d hh=%02d nn=%02d ss=%02d\n",$yy+1900,$mm+1,$dd,$hh,$nn,$ss);
printf("DATE=%04d%02d%02d\n",$yy+1900,$mm+1,$dd);
printf("TIME=%02d%02d%02d\n",$hh,$nn,$ss)')
echo "$file:"$DATE,$TIME,$yy,$mm,$dd,$hh,$nn,$ss

To get corresponding infos from system time:
man date


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
jis0023, you can't get creation date of a file. It's not recorded anywhere. You can only get date and time of last access, last modification, and last status change.

Which one you need depends on your purpose in needing it.

Hope this helps.
 
If you did want to just cut it out of the [tt]ls[/tt] listing, this would do it...
Code:
ls -l | cut -c42-53
Keep in mind there are a lot of things that can mess this up because it's based on column alignment and there are a lot of things that can change the formatting of the [tt]ls[/tt] output.

This can get it too...
Code:
ls -l | awk '{print $6,$7}'
Isn't the date for any file going to be before the current system date? Are you expecting files with future dates?

What exactly are you trying to do?
 
Well basically what I'm trying to do is create a cron job with kornshell that will detect a specifically named text file each day. If those text files were created on the current day, then they are moved to a different location. I'm just having trouble trying to discover whether that file was created in the current day or not.
 
Use the [tt]find[/tt] command ([tt]man find[/tt] for more info). Look for the [tt]-mtime[/tt]

Something like...
Code:
find /path_to_file -name '*name*' -mtime -1 -exec mv {} /different_location \;
That would move every file matching that name pattern, in [tt]/path_to_file[/tt] and subdirectories, that is less than a day old.

Hope this helps.
 
man date
man touch
man find

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Actually, that will get files that have changed in the last 24 hours. So if you run it at noon, it will pick up all files new or modified since noon the previous day.

To get files TODAY only, do this...
Code:
#!/bin/ksh

touch -t $(date '+%m%d')0000 /tmp/today.dat.$$

find /some_dir -name '*name*' [b]-newer /tmp/today.dat.$$[/b] -exec mv {} /diff_loc \;

rm /tmp/today.dat.$$
That will create a file who's date it the start of today. Then the find will just get files newer than that, which will be files created or modified today.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top