There are often situations when you need to view recently created, new files in the operating system. This is often needed by a system administrator who created a configuration file and then simply forgot where it was saved. Also, the need to look for new files may arise because you need to see if a file has changed recently or not. In general, situations can be very different. The advantage of the Linux operating system is that it traditionally provides several ways to accomplish a particular task.
The easiest and most common way to find new files in Linux is the Find
utility. Depending on your current needs, different parameters are passed to it, for example you can search for files only within a certain range of calendar months or days, see below for details on how to do this.
Files are displayed in directories and subdirectories and sorted:
$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p ' | sort –r
But as you can see, the result is quite impressive, it is not possible to recognize new files from the whole list. Therefore, we need to use another command:
$ find /etc -type f -mmin -120
This is how we specify that the system should be searched only for files that were created within the last hour.
If it is necessary to cover the last two days, then enter the following command:
$ find /etc -type f -mtime -2
Sometimes there is no need to delve into the third level subdirectories, so to exclude this action we need to use the special option maxdepth
:
$ find /etc -maxdepth 3 -mtime -2 -type f
We can see all new files created on the Linux system in the last seven days, but with the exception that the last three days should not be taken into account:
$ find /etc -type f -mtime -7 ! -mtime -3
These commands output the path to the file, but to view the attributes we have to use the special option --exec
. Next, we will output the attributes of each new file using the ls
utility:
$ find /etc -type f -mmin -120 -exec ls -al {} ;
If you find this command too confusing and complicated, you can use another one:
$ find /etc -type f -mmin -120 | xargs ls -l
Here we already use another utility, xargs
. The find utility also allows you to find files newer than a certain file. For example, let's create a reference file:
$ touch /tmp/test
Now find all newer files in the root directory that were created after it:
$ find / -type f -newer /tmp/test
Let's move on to another method.
This method of searching for new files is much simpler than the previous one, so it can be recommended to Linux beginners. The ls
command can also sort files in a directory by creation date. To do this, we only need to perform a few things:
$ ls -ltr
There may be too many files, so it is wise to run this command as well:
$ ls -ltr | tail
Here are not all the ways to find new files, but the listed methods are enough to accomplish the task quickly and efficiently. As you can see, the basic find and ls search commands can be made even more flexible when combined with sort, tail
, and grep
.