InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Finding Files by Date in bash
Bash

Finding Files by Date in bash

InfinityCoder February 20, 2017

Suppose someone sent you a JPEG image file that you saved on your filesystem a few months ago. Now you don’t remember where you put it.

How can you find it?

Use a find command with the -mtime predicate, which checks the date of last modification.
For example:

1
find . -name '*.jpg' -mtime +90 -print

The -mtime predicate takes an argument to specify the timeframe for the search.

The 90 stands for 90 days. By using a plus sign on the number (+90) we indicate that we’re looking for a file modified more than 90 days ago.

Write -90 (using a minus sign) for less than 90 days. Use neither a plus nor minus to mean exactly 90 days.
There are several predicates for searching based on file modification times and each take a quantity argument.

Using a plus, minus, or no sign indicates greater than, less than, or equals, respectively, for all of those predicates.
The find utility also has logical AND, OR, and NOT constructs so if you know that the file was at least one week old (7 days) but not more than 14 days old, you can combine the predicates like this:

1
$ find . -mtime +7 -a -mtime -14 -print

You can get even more complicated using ORas well as AND and even NOT to combine conditions, as in:

1
$ find . -mtime +14 -name '*.text' -o \( -mtime -14 -name '*.txt' \) -print

This will print out the names of files ending in .text that are older than 14 days, as well as those that are newer than 14 days but have .txt as their last 4 characters.
You will likely need parentheses to get the precedence right. Two predicates in sequence are like a logical AND, which binds tighter than an OR(in find as in most languages).

Use parentheses as much as you need to make it unambiguous.
Parentheses have a special meaning to bash, so we need to escape that meaning, and write them as \( and \) or inside of single quotes as ‘(‘ and ‘)’.

You cannot use single quotes around the entire expression though, as that will confuse the find command.
It wants each predicate as its own word.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Indenting Here-Documents in bash
The here-document is great, but it’s messing up your shell …

Indenting Here-Documents in bash

Processing Files with No Line Breaks in bash
You have a large file with no line breaks, and …

Processing Files with No Line Breaks in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap