ctime


Linux: Delete all files that are older than X days

The command find /data/ -type f -mtime +15 -exec rm -f '{}' \; is used to search and delete all the files in the “/data/” directory that have a modification time of more than 15 days old. The following is an explanation of each part of the command:

  1. “find /data/” – This specifies the directory that the search will start from; in this case, it’s the “/data/” directory.
  2. “-type f” – This option specifies that the search should be limited to files, not directories.
  3. “-mtime +15” – This option specifies that the files should be older than 15 days based on the modification time. The “+” sign indicates that we are looking for files older than 15 days.
  4. “-exec rm -f ‘{}’ \;” – This option is used to execute a command on the files found. The command “rm -f ‘{}'” is used to delete the files and the “{}” is a placeholder for the files that are found. The “” at the end of the line is used to escape the semicolon and avoid a syntax error.

The “find /data/ -type f -ctime +15 -exec rm -f ‘{}’ \;” command is similar to the above command, but it searches for files based on their creation time instead of modification time. The “ctime” option specifies that the search should be based on the file creation time instead of the modification time.

In conclusion, both commands are used to delete files in the “/data/” directory that are older than 15 days. Still, the difference is that the first command searches for files based on their modification time, while the second command searches for files based on their creation time.


File permissions change date

Recently we wanted to check when did the permissions of a specific file changed.
Unfortunately, there exists no such flag and we do not have a 100% working solution for it.

What we did was to check the last modification time of the file status information (ctime) using the ls -lc command.
This command could indicate the last permissions change time but it is not a reliable source as it represents the modification time of other elements as well.

The modification time of the file status information (ctime) gets updated when any inode information regarding the file changes.
This means that the modification time of the file status information (ctime) will get updated when any of the following changes:

  • owner – The numeric user ID (UID) of the file’s owner.
  • group – The numeric group ID (GID) of the file’s group.
  • link count – The number of links to the file.
  • mode – The bit string that indicated the permissions and privileges
  • serial – The serial number of the file.
  • device – The numeric ID of the device containing the file.

Explanation of ls parameters

  • The parameter -c of the ls command when used with the -l will show ctime and sort by name.
  • The parameter -c of the ls command when used with the -l and the -t will show ctime and sort by ctime (newest first).

Example that demonstrates that we get different values in the time column of -l when -c is used

$ ls -lc ~/.ssh/
total 28
-rwx------. 1 george george  225 May 16 17:05 config
-rwx------. 1 george george 1743 Jun  2 13:36 id_rsa
-rwxrwx---. 1 george george  405 May 16 17:05 id_rsa.pub
-rwxrwx---. 1 george george   32 May 16 17:05 Details.txt
-rw-r--r--. 1 george george 9155 May 30 14:32 known_hosts

$ ls -l ~/.ssh/
total 28
-rwx------. 1 george george  225 Mar 22 11:36 config
-rwx------. 1 george george 1743 Jan 25 10:22 id_rsa
-rwxrwx---. 1 george george  405 Jan 25 10:22 id_rsa.pub
-rwxrwx---. 1 george george   32 Jan 25 10:22 Details.txt
-rw-r--r--. 1 george george 9155 May 30 14:32 known_hosts