Operating System - HP-UX
1752587 Members
3968 Online
108788 Solutions
New Discussion

Re: how to get count of files created on specific date (yesterday)

 
SOLVED
Go to solution
Suhag
Occasional Advisor

how to get count of files created on specific date (yesterday)

Hi Experts,

I have lots of file in 1 directory and want the count of files created on specific date. i.e. I am getting count of files created on septermber 13, 2017 with below command :

ls -l | grep "Sep 13" | wc -l

but this command provides the count of all the files created on Septemeber 13 of all the year instead of year 2017.

Please help me to get the count of files created on specific date and year.

 

4 REPLIES 4
Steven Schweda
Honored Contributor

Re: how to get count of files created on specific date (yesterday)

> I have lots of file in 1 directory [...]

   On what?

      uname -a

> ls -l | [...]

   Parsing "ls -l" output is almost always more work than it's worth.
The usual way to do things like this is with "find", particularly using
"[!] -newer" to set the date-time limits.  Use "touch -t" to
create/modify a comparison file (or two) for use with "-newer".  (Having
to create a file to specify a date-time value is lame, but easy enough
to do.)

   A Forum or Web search for keywords like:
      find -newer touch
should find many examples.

Bill Hassell
Honored Contributor

Re: how to get count of files created on specific date (yesterday)

The creation timestamp is the same as the modification timestamp. In other words, you can only see when the file was last created or modified.

The ll command changes the format of the timestamp when the timestamp is older than 6 months (the hour:minute changes to the year (YYYY). So it is a very poor tool for selecting or sorting by timestamp.

The tool to use is find which has Boolean expression capabilities. The technique to find files within a time range is the -newer primary. Then provide two temporary files, one with a timestamp at the beginning of the range and the other at the end. Then use find to list all files within that range.

For the Sep 13, 2017 range, create two files like this:

cd /desired-directory
touch -t 201709130000.00 start-time
touch -t 201709132359.59 end-time

The filenames can be anything and can be removed after the find is complete.
The -t option is YYYYmmddhhmm.ss

Now use this command to display all files within the time range:

cd /desired-directory
find . -type f -newer start-time ! -newer end-time -exec ls -lt {} \;

To show just the filenames, leave off everything from -exec to the end:

cd /desired-directory
find . -type f -newer start-time ! -newer end-time

The above is a simple example.
To be completely accurate and find files created exactly at the beginning of Sep 13, the start-time should be changed to the last second of the previous day:

touch -t 201709122359.59 start-time

 Now a file created or modified on Sep 9 at 00:00.00 will be included.



Bill Hassell, sysadmin
Suhag
Occasional Advisor
Solution

Re: how to get count of files created on specific date (yesterday)

Thanks you very much experts....I got the required count of files

thanks again

Dennis Handly
Acclaimed Contributor

Re: how to get count of files created on specific date (yesterday)

> ll(1) changes the format of the timestamp when the timestamp is older than 6 months

 

You can change the message catalog so that the two formats are identical.  :-)