Tips

ctime, mtime and atime; find command

Written in 2016. Software versions and commands may have changed since.

ctime, mtime  and atime are very useful commands when dealing with time related search commands in the linux Family of operating systems.

ctime; Linux find command
ctime; Linux find command

This post is about how to use them in

a simple but yet very powerful way. Let’s get to it:

Let say you want to find all files in a folder that follow/fulfill the following criteria:

  1. the folder is /vat/lib/mysql/data
  2. The file name contains: mysql-bin.
  3. The files are younger than 2 days.

In other words find all the mysql binary log files that are not older than 2 days.

I am using mysql binary logs of a server as an example here, but the same might apply to any type of files and folders.

ctime; Here is the command :

$ find /var/lib/mysql/data -ctime -2 -name 'mysql-bin.*' 
/var/lib/mysql/data/mysql-bin.007989
/var/lib/mysql/data/mysql-bin.007988
/var/lib/mysql/data/mysql-bin.007990
/var/lib/mysql/data/mysql-bin.007994
(...)
$

if you see the output it magically shows the files wanted. Isn’t that great? Not let’s break it in smaller pieces.

  • find is the command that says you will try to find something to the OS.
  • then next the path where you want to find stuff.
  • Then -ctime -2, this means creation time is younger than 2, here you can also say different stuff:
    1. For instance any integer positive number you add here will be the MAX age of the files to find. technically 2 means 3 days, but aside from that is regular find younger than x days.

      According to the docs:
                  -mtime n
                    File's data was last modified n*24 hours ago.  See the
                    comments for -atime to understand how rounding affects the
                    interpretation of file modification times.
                  -atime n
                    File was last accessed n*24 hours ago.  When find figures out
                    how many 24-hour periods ago the file was last accessed, any
                    fractional part is ignored, so to match -atime +1, a file has
                    to have been accessed at least two days ago.
      
    2. Then the last part is just to find by name. -name ‘mysql-bin.*’ any file or directory who’s name starts with mysql-bin., * means wildcard or anything.

Ok so we got a command now you should know the main parameters and be able to find help if needed:

  1. ctime is creation time in days.
  2. mtime is modification time in days.
  3. atime is access time in days.
  4. cmin same as -ctime, but in minutes
  5. mmin same as -mtime but in minutes
  6. amin same as -atime but in days.

Find has many other parameters that help you filter by other many parameters, you can just execute man and check them out or make s simple search in google. Another thing you need to keep present is the sign of the integer number, e.g.:

  1. mtime -3 means younger than 3+1 days.
  2. atime 5 means exactly between 5 and 6 days.
  3. ctime +4 means older than 4+1 days.
  4. Then you can combine statements like ‘-ctime +2 -ctime -4’ which means between those two numbers (remember the +1 so it would be between 3 and 5).

And with this you can create fun and useful commands that will ease your serch. e.g.:

Remove everything older than one month in the path recoursively:

find ~/abelworld/files -ctime +30 -exec rm -rf '{}' ';'

 Find everything create in the folder between 1 and 3 months and the name ends in .txt:

find ~/abelworld/files -mtime +30 -mtime -60  -name '*.txt'

And that is all for today. I hope you found it usefull and feel free to comment on any other cool usage for the find command.