Wednesday, July 28, 2010

Find Files By Access, Modification Date / Time Under Linux or UNIX

SkyHi @ Wednesday, July 28, 2010
I do not remember where I saved pdf and text files under Linux. I have downloaded files from the Internet a few months ago. How do I find my pdf or text files?

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

List ALL *.c File Accessed 30 Days Ago

Type the following command
find /home/you -iname "*.c" -atime -30 -type -f
  
Finds files older than 18months and newer than 24 months, cat’s the output to a CSV in the format:
/some/path/somewhere, size in bytes, Access Time, Modified Time

find /dir/dir -type f -mtime +540 -mtime -720 -printf \”%p\”,\”%s\”,\”%AD\”,|”%TD\”\\n > /dir/dir/output.csv

REFERENCES
http://www.cyberciti.biz/faq/howto-finding-files-by-date/
http://www.cyberciti.biz/faq/linux-unix-osxfind-files-by-date/ 



========================================================================


Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time).

Unless you have explicitly opted out with a noatime option mounting your Linux file system, there are generally 3 types of time on each and every file of Linux: atime or access time, ctime or change time, and mtime or modification time.

These are the differences between the 3 file system times:
  1. atime – access time, or the last accessed time of a file or directory, whenever you accessed, opened or touched it, the atime is updated to the current time. You can get the file atime value in PHP.
  2. ctime – change time, or the last changed time of the file or directory, whenever you change and update the file such as changing the file ownership or permissions or modifying the file content, the ctime of the file is updated to the current time. You can get the file ctime in PHP.
  3. mtime – modification time, or the last modified time of the file or directory, whenever you modify and update the content of the file. Modifying file ownerships or permissions doesn’t change mtime of the file. To get mtime of a file in PHP, you need a the php function filemtime.


Linux file ctime vs. mtime (differences):  
mtime is last time the content of the file is modified; while ctime is the last time the file is changed, not only regarding content, but also the meta information such as owner, group and permissions.



    REFERENCES
    http://www.kavoir.com/2009/04/linux-the-differences-between-file-times-atime-accessed-time-ctime-changed-time-and-mtime-modified-time.html