I ran into a need to use the Linux find command newer option this weekend, and I thought I’d share a quick example of how to use it.
In short, you typically use the “newer” argument when you want to find all files that are newer than some date or time. You do this in a two-step process:
- You create a new file with the desired date and time.
- You issue a ‘find newer’ command, specifying that new file.
For instance, let’s assume that I want to find all files beneath the current directory that are newer than December 4, 2011, at 10:40am.
First, I create a temporary file using the touch command, which conveniently lets me control the time stamp on that file when I create it:
touch -t 201112041040 foo
If you look at this new file with the ls command, you’ll see that it is created with the timestamp we specified:
# ls -l foo-rw-r--r-- 1 root root 0 Dec 4 10:40 foo
Next, I issue my Linux find command with the newer option, saying that I want to find all files that are newer than this file I just created:
find . -newer foo
If you try these commands on your own Linux system, you’ll see that this works like a charm. This find command will give you a list of all files whose timestamp is newer than the ‘foo’ file you created.
Details about the touch command timestamp
The touch command timestamp format looks like this:
[[CC]YY]MMDDhhmm[.ss]
As a result, I didn’t have to include the year in my earlier example, I just did that to be clear. As long as the file you’re creating is in the current year, you can just issue the touch command like this with the month, day, hour, and minute, which are required:
touch -t 12041040 foo
You can also optionally add “seconds” to your timestamp, like this:
touch -t 12041040.30 foo
In my experience I’ve never had to use the seconds field, but if you’re dealing with a really bad situation where files are being created very rapidly, I can see where that would be useful.
My Linux find command newer option examples
I hope my Linux find command newer option examples have been helpful. As you can see, you can combine the find command and touch command to create a really powerful way to search for files on Unix and Linux systems.
For much more information on the find command, please see my Linux find command examples and tutorials on the devdaily.com website.