In a Linux file system, files are stored within a single directory structure, called a virtual directory. The virtual directory contains files from all the computer’s storage devices and merges them into a single directory structure. This structure has a single base directory called the root directory (/) that is often simply called root. Locating files and directories is a common operation for any Linux SysAdmin.
You can view, create, copy, move and delete files in the virtual directory structure from the command line or using graphical interface utilities. In this article we describe how you can locate files and directories older than an x number of days or minutes. The results from the search can be subjected to delete operation if that’s the desired action.
Using the find Command
With the find command you can locate files based on data, such as ownership, time modification, file permissions, and so on. The basic command syntax is as follows:
$ find [ PATH ...] [ OPTION ] [ EXPRESSION ]
When using find you designate a starting point directory using the PATH argument. find will then search through that directory and all its subdirectories (recursively) for the file or files you seek. To see all supported command options use:
$ man find
$ find -help
The find command’s commonly used options and expressions are:
Find option | Expression | Description |
-name | pattern | Locate files whose name matches pattern |
-size | n | Locate files whose size matches n |
-user | name | Locate files whose owner is name. |
-group | name | Locate files whose group is name |
-gid | n | Locate files whose group ID is equal to n. |
-cmin | n | Locate files whose status changed n minutes ago. |
-mmin | n | Locate files whose data changed n minutes ago. |
-perm | mode | Locate files whose permissions matches mode (octal or symbolic) |
-nouser | N/A | Locate files where no username exists for the file’s user ID |
-maxdepth | n | When searching for files, traverse down only n levels. |
-inum | n | Locate files whose inode number is equal to n. |
-empty | N/A | Locate files that are empty and are a regular text file or a directory |
-mtime | n | Locate files whose data was last modified n*24 hours ago |
Files files older than x minutes in Linux
We’ll use -mmin to locate files whose data changed n minutes ago.
Example below finds files older than 5 minutes in the current working directory:
$ find ./ -type f -mmin +5
./file1.txt
./file2.txt
./file3.txt
We can delete the files using the find and -delete option:
find ./ -mmin +5 -delete
Confirm files are deleted using
$ ls
The following command will yield same results:
$ find ./ -mmin +5 -type f -exec rm -fv {} \;
removed './file1.txt'
removed './file2.txt'
removed './file3.txt'
For directories replace -type f with -type d
Find files older than x days in Linux
Find files older than 7 days:
$ find ./ -mtime +7 -type f
./nu_0_28_0_linux/nushell-0.28.0/libssl.so.1.1
./nu_0_28_0_linux/nushell-0.28.0/README.txt
./nu_0_28_0_linux/nushell-0.28.0/LICENSE
./nu_0_28_0_linux.tar.gz
$ find /some-dir -mtime +7 -type f
Only directories:
$ find ./ -mtime +7 -type d
./nu_0_28_0_linux
./nu_0_28_0_linux/nushell-0.28.0
Delete files older than 7 days:
find ./ -mtime +7 -type f -delete
You can use more filters e.g file permissions, user/group ownership.
Here are some of our recent similar guides: