link
It’s two steps but I like to do it this way:
First create a file with a particular date/time. In this case, the file is 2008-10-01 at midnight
touch -t 0810010000/tmp/t
Now we can find all files that are newer or older than the above file (going by file modified date. You can also use -anewer for accessed and -cnewer file status changed).
find /-newer /tmp/t
find /-not -newer /tmp/t
You could also look at files between certain dates by creating two files with touch
touch -t 0810010000/tmp/t1
touch -t 0810011000/tmp/t2
This will find files between the two dates & times
find /-newer /tmp/t1 -and -not -newer /tmp/t2
Other Example
link
The syntax is as follows:
ls -l | grep 'yyyy-mm-dd'
ls -l | grep --color=auto '2006-01-05'
|
Where,
- 2006 – Year
- 01 – Month
- 05 – Day
You can sort it as follows:
ls -lu | grep --color=auto '2006-01-05'
|
find Command Example
If you need a specific date range many days ago, than consider using the find command. In this example find files modified between Jan/1/2007 and Jan/1/2008, in /data/images directory:
touch --date "2007-01-01" /tmp/start
touch --date "2008-01-01" /tmp/end
find /data/images -type f -newer /tmp/start -not -newer /tmp/end
|
You can save list to a text file called output.txt as follows:
find /data/images -type f -newer /tmp/start -not -newer /tmp/end > output.txt
|
List ALL *.c File Accessed 30 Days Ago
Type the following command
find /home/you -iname "*.c" -atime -30 -type -f
|
See also:
You need to use the find command. Each file has three time stamps, which record the last time that certain operations were performed on the file:
[a] access (read the file’s contents) – atime
[b] change the status (modify the file or its attributes) – ctime
[c] modify (change the file’s contents) – mtime
You can search for files whose time stamps are within a certain age range, or compare them to other time stamps.
You can use -mtime option. It returns list of file if the file was last accessed N*24 hours ago. For example to find file in last 2 months (60 days) you need to use -mtime +60 option.
- -mtime +60 means you are looking for a file modified 60 days ago.
- -mtime -60 means less than 60 days.
- -mtime 60 If you skip + or – it means exactly 60 days.
So to find text files that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -print
Display content of file on screen that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -exec cat {} \;
Count total number of files using wc command
$ find /home/you -iname "*.txt" -mtime -60 | wc -l
You can also use access time to find out pdf files. Following command will print the list of all pdf file that were accessed in last 60 days:
$ find /home/you -iname "*.pdf" -atime -60 -type -f
List all mp3s that were accessed exactly 10 days ago:
$ find /home/you -iname "*.mp3" -atime 10 -type -f
There is also an option called -daystart. It measure times from the beginning of today rather than from 24 hours ago. So, to list the all mp3s in your home directory that were accessed yesterday, type the command
$ find /home/you -iname "*.mp3" -daystart -type f -mtime 1
Where,
- -type f – Only search for files and not directories
-daystart option
The -daystart option is used to measure time from the beginning of the current day instead of 24 hours ago. Find out all perl (*.pl) file modified yesterday, enter:
find /nas/projects/mgmt/scripts/perl -mtime 1 -daystart -iname "*.pl"
|
You can also list perl files that were modified 8-10 days ago, enter:
To list all of the files in your home directory tree that were modified from two to four days ago, type:
find /nas/projects/mgmt/scripts/perl -mtime 8 -mtime -10 -daystart -iname "*.pl"
|
-newer option
To find files in the /nas/images directory tree that are newer than the file /tmp/foo file, enter:
find /etc -newer /tmp/foo
|
You can use the touch command to set date timestamp you would like to search for, and then use -newer option as follows
touch --date "2010-01-05" /tmp/foo
# Find files newer than 2010/Jan/05, in /data/images
find /data/images -newer /tmp/foo
|
Read the man page of find command for more information:
man find