As your Drupal application ages, it will inherently create stale data, including images and files. It will decrease the storage amount over time. However, using the -mtime
flag in the find
command will allow your team to see when a file was last accessed. For Apache to deliver content and the associated files to the content, it will always update the time the file was accessed in the server.
How find stale files and images
Log in to the server using SSH and navigate to the directory you want to review, this usually would be sites/default/files inside your Drupal application. The command below will recursively search and find files that have been accessed by the file system in the timeframe of your choice.
find . -name "*.jpg" -type f -mtime +60 -exec ls -al {} \; |less
After your team has audited the older files against the relevant content in your Drupal CMS, you can use a command to delete old stale files. (This needs to be done with caution and backed up where necessary as per the terms of service)find . -name "*.jpg" -type f -mtime +60 -exec rm -f {} \;
You can use the command to remove old database backups as wellfind . -name "*.sql.gz" -type f -mtime +60 -exec rm -f {} \;
How this command works
Below is a breakdown of how this command helps you find files that have not been accessed for a certain period of time. As a broad overview, the command finds a file that ends in jpg that is six months and one day old; when found, it performs a listing command against the file and adds that data to the future output. Once all the relevant files are collated, we pipe all the output into the less command to make it easier to read.
find . //Search this directory and all children. -name "*.jpg" // For all file types that end in jpg. -type f // Look for normal files. -mtime +180 // Has the file not accessed for six months and one day. -exec ls -al {} \; // Add to the output human-readable date and file path. |less // Don't give me all the results at once.
The -mtime
flag has the following definition:
File's data was last modified n*24 hours ago.
So "-mtime +180" means the file is strictly older than six months and one day as an example. This flag is what allows the user to see files that have not been used by Apache for a period of six months or more.
Using Status command
If you want to find out the details of a single file you can use the status
command. The stat command is defined as
The stat utility displays information about the file pointed to by file.
Here is an example below
$~ stat .htaccess
File: '.htaccess' Size: 5765 Blocks: 16 IO Block: 4096 regular file Device: 10305h/66309d Inode: 33576789 Links: 1 Access: (0440/-r--r-----) Uid: ( 33/www-data) Gid: (owner) Access: 2020-09-09 13:25:54.910728163 +0000 Modify: 2019-07-27 20:39:28.385676988 +0000 Change: 2019-07-27 20:39:28.645678259 +0000 Birth: -