Find age of a directory

  • Follow


Is there is any way to find the age of a directory in days?

drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
bash-3.2$ date
Sat Dec 12 11:36:39 GMT 2009

so in the above case, get out the number 21, as the directory is 21 days old?

bash-3.2$ mkdir foo
bash-3.2$ ls -ld foo
drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo

In the above case, print 0, as I just made the directory, so its zero days old.

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/12/2009 11:40:37 AM

Dave wrote:
> Is there is any way to find the age of a directory in days?
> 
> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
> bash-3.2$ date
> Sat Dec 12 11:36:39 GMT 2009
> 
> so in the above case, get out the number 21, as the directory is 21 days 
> old?
> 
> bash-3.2$ mkdir foo
> bash-3.2$ ls -ld foo
> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
> 
> In the above case, print 0, as I just made the directory, so its zero 
> days old.
> 
I'd prefer something that makes use of /bin/sh, and not make use of perl and 
certainly no basisms.

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/12/2009 11:46:55 AM


2009-12-12, 11:40(+00), Dave:
> Is there is any way to find the age of a directory in days?
>
> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
> bash-3.2$ date
> Sat Dec 12 11:36:39 GMT 2009
>
> so in the above case, get out the number 21, as the directory is 21 days old?
>
> bash-3.2$ mkdir foo
> bash-3.2$ ls -ld foo
> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>
> In the above case, print 0, as I just made the directory, so its zero days old.

perl -le 'print -M shift' /tmp

-- 
St�phane
0
Reply Stephane 12/12/2009 11:48:41 AM

2009-12-12, 11:46(+00), Dave:
> Dave wrote:
>> Is there is any way to find the age of a directory in days?
>> 
>> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
>> bash-3.2$ date
>> Sat Dec 12 11:36:39 GMT 2009
>> 
>> so in the above case, get out the number 21, as the directory is 21 days 
>> old?
>> 
>> bash-3.2$ mkdir foo
>> bash-3.2$ ls -ld foo
>> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>> 
>> In the above case, print 0, as I just made the directory, so its zero 
>> days old.
>> 
> I'd prefer something that makes use of /bin/sh, and not make use of perl and 
> certainly no basisms.


You could try:

echo /tmp |
 pax -x ustar -wd |
 dd bs=4 skip=34 count=3 2> /dev/null |
 tr -d '\0' |
 awk '{srand(); print srand() " 8k8i" $0 "-250600/p"}' | dc

If you have GNU date, you can use date -r /tmp +%s

-- 
St�phane
0
Reply Stephane 12/12/2009 12:32:59 PM

Dave <foo@coo.com> writes:

> Dave wrote:
>> Is there is any way to find the age of a directory in days?
>>
>> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
>> bash-3.2$ date
>> Sat Dec 12 11:36:39 GMT 2009
>>
>> so in the above case, get out the number 21, as the directory is 21
>> days old?
>>
>> bash-3.2$ mkdir foo
>> bash-3.2$ ls -ld foo
>> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>>
>> In the above case, print 0, as I just made the directory, so its
>> zero days old.
>>
> I'd prefer something that makes use of /bin/sh, and not make use of
> perl and certainly no basisms.

If you can get a file's date as an integer in seconds, then the rest
can be done with POSIX expr and test.

The two ways I know to get this are 'date -r dir +%s' (already
mentioned) and 'stat --format=%Y dir'.  Sadly, neither is POSIX.  If
the first is available, use 'date +%s' to get the current time.  If
the second, make a temporary file and use that method to get the time
now in seconds.

Of course, POSIX mandates a C compiler so you could write a few lines
of C to do it.

-- 
Ben.
0
Reply Ben 12/12/2009 5:12:42 PM

On December 12, 2009 06:40, in comp.unix.shell, foo@coo.com wrote:

> Is there is any way to find the age of a directory in days?

What do you mean "age" of a directory?

Do you mean "how long since the directory was created"? If so, then you're
out of luck; "creation date" is not one of those attributes available on
files and directories.

Do you mean "how long since the directory was last modified"? (That being
the time /normally/ shown by the "ls -l" command)


> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
> bash-3.2$ date
> Sat Dec 12 11:36:39 GMT 2009
> 
> so in the above case, get out the number 21, as the directory is 21 days
> old?

No. It's been 21 days since the directory was last modified (files added or
deleted from it).


> bash-3.2$ mkdir foo
> bash-3.2$ ls -ld foo
> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
> 
> In the above case, print 0, as I just made the directory, so its zero days
> old.

No. Zero days have transpired since the directory was last modified. 

In general, the ls(1) command does not have a flag to show mtime, ctime,
and/or atime in terms relative to today's date. To get that, you'll have to
write some scripting magic, and compute the value.

Your ls(1) command may provide flags to alter the presentation of the
mtime/ctime/atime such that the format is compatible with other tools (awk,
date, etc) that can be used in a script to compute days-relative-to-today.

See the manpage for your ls(1) command for details; on the gnu ls(1),
the --time-style flag is what you need.

Also, decide on /which/ timestamp you are interested in:
- mtime (the default for "ls -l") is the timestamp for when the *contents*
  of the file or directory were last altered,
- ctime is the timestamp for when the *contents* of the file or directory
  were last read, and
- atime is the timestamp for when the metadata (permission bits, etc) of the
  file or directory were last altered.

-- 
Lew Pitcher
Master Codewright & JOAT-in-training   | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
----------      Slackware - Because I know what I'm doing.         ------


0
Reply Lew 12/12/2009 5:58:46 PM

Lew Pitcher <lpitcher@teksavvy.com> writes:
<snip>
> Also, decide on /which/ timestamp you are interested in:
> - mtime (the default for "ls -l") is the timestamp for when the *contents*
>   of the file or directory were last altered,
> - ctime is the timestamp for when the *contents* of the file or directory
>   were last read, and
> - atime is the timestamp for when the metadata (permission bits, etc) of the
>   file or directory were last altered.

I think those last two are the other way round.  Also, it is not
uncommon for a file system to be mounted with noatime in which case
atime is not recorded.

-- 
Ben.
0
Reply Ben 12/13/2009 4:25:47 AM

Stephane CHAZELAS wrote:
> 2009-12-12, 11:46(+00), Dave:
>> Dave wrote:
>>> Is there is any way to find the age of a directory in days?
>>>
>>> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
>>> bash-3.2$ date
>>> Sat Dec 12 11:36:39 GMT 2009
>>>
>>> so in the above case, get out the number 21, as the directory is 21 days 
>>> old?
>>>
>>> bash-3.2$ mkdir foo
>>> bash-3.2$ ls -ld foo
>>> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>>>
>>> In the above case, print 0, as I just made the directory, so its zero 
>>> days old.
>>>
>> I'd prefer something that makes use of /bin/sh, and not make use of perl and 
>> certainly no basisms.
> 
> 
> You could try:
> 
> echo /tmp |
>  pax -x ustar -wd |
>  dd bs=4 skip=34 count=3 2> /dev/null |
>  tr -d '\0' |
>  awk '{srand(); print srand() " 8k8i" $0 "-250600/p"}' | dc
> 
> If you have GNU date, you can use date -r /tmp +%s
> 

That srand trick does not work with FreeBSD - apparently POSIX says the random 
number generator has to be derrived from the clock, but does not say in which 
way. Most unix systems use time since the epoch, but FreeBSD does not.

Dave

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/13/2009 8:41:11 AM

Lew Pitcher wrote:
> On December 12, 2009 06:40, in comp.unix.shell, foo@coo.com wrote:
> 
>> Is there is any way to find the age of a directory in days?
> 
> What do you mean "age" of a directory?
> 
> Do you mean "how long since the directory was created"? If so, then you're
> out of luck; "creation date" is not one of those attributes available on
> files and directories.
> 
> Do you mean "how long since the directory was last modified"? (That being
> the time /normally/ shown by the "ls -l" command)

Thank you.

It's the time since its contents was last modified that I want.

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/13/2009 8:47:45 AM

Dave wrote:
> Stephane CHAZELAS wrote:
>> 2009-12-12, 11:46(+00), Dave:
>>> Dave wrote:
>>>> Is there is any way to find the age of a directory in days?
>>>>
>>>> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
>>>> bash-3.2$ date
>>>> Sat Dec 12 11:36:39 GMT 2009
>>>>
>>>> so in the above case, get out the number 21, as the directory is 21 
>>>> days old?
>>>>
>>>> bash-3.2$ mkdir foo
>>>> bash-3.2$ ls -ld foo
>>>> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>>>>
>>>> In the above case, print 0, as I just made the directory, so its 
>>>> zero days old.
>>>>
>>> I'd prefer something that makes use of /bin/sh, and not make use of 
>>> perl and certainly no basisms.
>>
>>
>> You could try:
>>
>> echo /tmp |
>>  pax -x ustar -wd |
>>  dd bs=4 skip=34 count=3 2> /dev/null |
>>  tr -d '\0' |
>>  awk '{srand(); print srand() " 8k8i" $0 "-250600/p"}' | dc
>>
>> If you have GNU date, you can use date -r /tmp +%s
>>
> 
> That srand trick does not work with FreeBSD - apparently POSIX says the 
> random number generator has to be derrived from the clock, but does not 
> say in which way. Most unix systems use time since the epoch, but 
> FreeBSD does not.
> 
> Dave
> 
Correction, it's OpenBSD this does not work with.

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/13/2009 9:06:44 AM

On Sat, 12 Dec 2009 09:40:37 -0200, Dave <foo@coo.com> wrote:

> Is there is any way to find the age of a directory in days?
>
> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
> bash-3.2$ date
> Sat Dec 12 11:36:39 GMT 2009
>
> so in the above case, get out the number 21, as the directory is 21 days old?
>
> bash-3.2$ mkdir foo
> bash-3.2$ ls -ld foo
> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>
> In the above case, print 0, as I just made the directory, so its zero days old.
>

$ D=/bin;echo $(((`date +%s`-`date -r $D +%s`)/(24*3600)))
7
$
0
Reply mop2 12/13/2009 11:45:03 AM

mop2 wrote:
> On Sat, 12 Dec 2009 09:40:37 -0200, Dave <foo@coo.com> wrote:
> 
>> Is there is any way to find the age of a directory in days?
>>
>> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
>> bash-3.2$ date
>> Sat Dec 12 11:36:39 GMT 2009
>>
>> so in the above case, get out the number 21, as the directory is 21 
>> days old?
>>
>> bash-3.2$ mkdir foo
>> bash-3.2$ ls -ld foo
>> drwxr-xr-x   2 drkirkby staff          2 Dec 12 11:37 foo
>>
>> In the above case, print 0, as I just made the directory, so its zero 
>> days old.
>>
> 
> $ D=/bin;echo $(((`date +%s`-`date -r $D +%s`)/(24*3600)))
> 7
> $


OK, I admit I did not explicitly say I wanted GNUisms, but I did say I wanted to 
avoid bashisms.

date +%s is a GNU specific option, and not one supported by many operating 
systems - Solaris and HP-UX do not support it. Neither do they support the -r 
option to date.

There was a discussion about getting time since the Epoch using portable code at:

http://groups.google.com/group/comp.unix.shell/browse_thread/thread/121344445ef2c75a/fe44b83b56478e36?hl=en&#fe44b83b56478e36

Here's the latest POSIX definition of the date command.

http://www.opengroup.org/onlinepubs/9699919799/utilities/date.html

Dave
-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/13/2009 12:06:13 PM

Dave <foo@coo.com> writes:
<snip>
> OK, I admit I did not explicitly say I wanted GNUisms, but I did say I
> wanted to avoid bashisms.

Does you system's find have the -printf %T@ action?  That would be a
way round all the shell calculations to compute an epoch time from
components.  It's not in POSIX but you might have it (it's too long
since I've used HP-UX to be useful).

<snip>
-- 
Ben.
0
Reply Ben 12/13/2009 1:30:43 PM

2009-12-13, 12:06(+00), Dave:
[...]
> OK, I admit I did not explicitly say I wanted GNUisms, but I did say I wanted to 
> avoid bashisms.
>
> date +%s is a GNU specific option, and not one supported by many operating 
> systems - Solaris and HP-UX do not support it. Neither do they support the -r 
> option to date.
>
> There was a discussion about getting time since the Epoch using portable code at:
>
> http://groups.google.com/group/comp.unix.shell/browse_thread/thread/121344445ef2c75a/fe44b83b56478e36?hl=en&#fe44b83b56478e36
>
> Here's the latest POSIX definition of the date command.
>
> http://www.opengroup.org/onlinepubs/9699919799/utilities/date.html
[...]

You can also use http://stchaz.free.fr/wide_strftime.sh

and:

IFS=' '
timegm $(TZ=GMT0 date '+%Y %m %d %H %M %S')

(result in $REPLY)

The only standard command to get last modification time is ls
(tough you could also parse a pax generated archive as in the
example I gave), but the output is not easy to parse (portably)
and is not precise for old files. That's one of the things
missing badly from the Unix toolchest. To work around that, zsh
and ksh93 have builtins for that, many systems have a stat
command. You can also use GNU find, GNU stat, GNU date as
already mentionned.

The most portable is probably to use perl. You could also use
find -mtime and a dichotomy in that case.

There was a discussion not so long ago about awk's srand()
portability.

-- 
St�phane
0
Reply Stephane 12/13/2009 2:44:52 PM

Ben Bacarisse wrote:
> Dave <foo@coo.com> writes:
> <snip>
>> OK, I admit I did not explicitly say I wanted GNUisms, but I did say I
>> wanted to avoid bashisms.
> 
> Does you system's find have the -printf %T@ action? 

I'm not looking for this to work on my system, but any Unix or Unix-like system.

-- 
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
0
Reply Dave 12/13/2009 2:53:41 PM

Dave <foo@coo.com> writes:

> Ben Bacarisse wrote:
>> Dave <foo@coo.com> writes:
>> <snip>
>>> OK, I admit I did not explicitly say I wanted GNUisms, but I did say I
>>> wanted to avoid bashisms.
>>
>> Does you system's find have the -printf %T@ action? 
>
> I'm not looking for this to work on my system, but any Unix or
> Unix-like system.

That's going to be almost impossible as stated.  If you are prepared
to ignore old Unix systems and settle for POSIX then you'll be OK
(though I think it is fiddly).  If you want to get close to "any Unix
or Unix-like system" then a short C program that gets compiled when
required will get you pretty close (depending on how broad your view
of "Unix-like" is).

-- 
Ben.
0
Reply Ben 12/13/2009 11:31:43 PM

On 2009-12-12, Dave <foo@coo.com> wrote:
> Is there is any way to find the age of a directory in days?

For what purpose? 

If you need this age as an input into some further procedure, then
you're asking the wrong question. It should be ``how do I solve the
following problem involving file dates ...''.

For instance, if you want to find all directories which have a
modification time of more than so many days, you can use the
find program.

> drwxr-xr-x   2 drkirkby staff          6 Nov 21 12:56 tmp
> bash-3.2$ date
> Sat Dec 12 11:36:39 GMT 2009
>
> so in the above case, get out the number 21, as the directory is 21 days old?

We know that the directory hasn't been modified for 21 days (no
directory entry has been added or removed). We don't really know how old
it is.  We also don't know what the last modification was: we don't konw
whether it was a removal or creation, and we don't know the names of the
directory entries that were affected.

Directory modification times are a next to completely useless piece
of information. You can use it for optimization: if a program maintains
a cached copy of a directory in memory, it can check the timestamp of
the one on disk to tell whether its cached copy is stale.
0
Reply Kaz 12/14/2009 2:00:13 AM

Ben Bacarisse wrote:

> Of course, POSIX mandates a C compiler

No it doesn't.

Look at the SYNOPSIS section of the c99 page.  It has a "CD" margin
marker for the whole section.  Now look at XBD7 section 1.7.1 Codes
where it says:

    [CD] C-Language Development Utilities
    The functionality described is optional.

-- 
Geoff Clare <netnews@gclare.org.uk>



0
Reply Geoff 12/14/2009 1:46:29 PM

On 2009-12-14, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote:
> Ben Bacarisse wrote:
>
>> Of course, POSIX mandates a C compiler
>
> No it doesn't.
>
> Look at the SYNOPSIS section of the c99 page.  It has a "CD" margin
> marker for the whole section.  Now look at XBD7 section 1.7.1 Codes
> where it says:
>
>     [CD] C-Language Development Utilities
>     The functionality described is optional.

For a little prespective, all of POSIX is entirely optional, and the
vast majority of the computer installations on the planet opt out.
0
Reply Kaz 12/14/2009 5:08:42 PM

Geoff Clare <geoff@clare.See-My-Signature.invalid> writes:

> Ben Bacarisse wrote:
>
>> Of course, POSIX mandates a C compiler
>
> No it doesn't.
>
> Look at the SYNOPSIS section of the c99 page.  It has a "CD" margin
> marker for the whole section.  Now look at XBD7 section 1.7.1 Codes
> where it says:
>
>     [CD] C-Language Development Utilities
>     The functionality described is optional.

Ah, thanks.  I still think that it might provide the simplest
solution.  The OP has not said if he can or can not assume a C
compiler.

-- 
Ben.
0
Reply Ben 12/15/2009 12:09:11 AM

19 Replies
172 Views

(page loaded in 0.326 seconds)

Similiar Articles:


















7/11/2012 3:37:40 PM


Reply: