I'd like to find the MAIN.TXT file in a directory folder. If it is older than 24hours then output YES to the MAIN.txt file. If it hasn't been modified in 24hours then leave the file alone.

I got as far as this:

find /directory -type f -mtime +1 -exec echo 'YES' > /directory/MAIN.txt \;

The MAIN.TXT file is older than 24 hours, so it works. This updates MAIN.TXT mod time to the current time and my MAIN.txt file has YES in it.

If MAIN.TXT is newer than 24hours old, it still modifies the MAIN.TXT with blank txt and changed the modification time.

I know I need a conditional, but I can't seem to get where it goes. Additionally, my mentor mentioned {} for my ECHO parameter but if I do that, then I get the filename with AVAILABLE appended.

  • 2
    What if a "bystander" file which happens to live in directory is +1 days old? For that to happen, MAIN.txt would be "punished" with a "YES", while not guilty. – Micha Mar 14 '19 at 22:20
  • It's unclear whether you want to add the line saying YES to the found file or to another separate file. Could you please clarify that? – Kusalananda Mar 15 '19 at 18:06
  • It is further unclear whether you want to find a particular file, or all files called MAIN.TXT. Could you please clarify that too? – Kusalananda Mar 15 '19 at 21:03

Mark's answer seems to fall a little short of OP's requirements.

Try this one liner at your terminal prompt. It is simpler and probably more robust.

[ $(find /path/to/directory/. -maxdepth 1 -type f -name MAIN.TXT -daystart -mtime +1) ] \
  && printf "YES" > /full/path/to/directory/MAIN.TXT

A word of explanation, first looking the larger blocks of the one liner, then getting into each block:

  • the one liner as the form [ block1 ] && block2
  • the construct [ block1 ] (with spaces!) is a boolean logical operator equivalent to the test built-in.
  • If block1's execution returns something (anything) then the result of [ block1 ] is TRUE. If it returns nothing, as in "", then the previous test's result is FALSE.
  • the logical operator && in A && B reflects the fact that B is executed if the exit status of A is 0 or TRUE. So if [ block1 ] executes to TRUE, then block2 is also executed.
  • block1 as the form $( ). This is a command substitution. Everything that is inside the parentheses is executed and the output replaces the command. That is the reason it is called "command substitution".
  • inside block1 the cmd being substituted consists in finding a file:
    • whose type is regular file
    • named MAIN.TXT
    • inside the directory /path/to/directory/.
    • no further down than the very directory level specified above (-maxdepth 1)
    • whose modification time is at least 24 hours (-mtime +1) starting from TODAY's time (-daystart). Without -daystart your search would look for files modified more that 24 hours ago, but starting to count backward from yesterday's 0 hour time.
| improve this answer | |
  • Hello! Thanks for the words of explanation. It's helping me understand your post. I had one observation. I'm using OSX to test this and I found that the -daystart positional option does not work. I am going to still include it, but I wonder why that is. – DizzyNYC Mar 15 '19 at 16:01
  • @DizzyNYC: This was tested w/ GNU find v4.6.0. If in yr version of find, -daystart does not show it's because yr OSX implementation doesn't include it. There may still be a similar option flag in yr version of find though. Just look at yr man page if in doubt. If not my solution should work with -maxdepth 1 and -name MAIN.TXT anyway. --- In future posts, I would advise that you specify your OS clearly. Another recourse for you in case of MacOS specific queries would be to post on SuperUser, another forum of SE that deals with Linux, Windows and Apple OSes. Hail NYC for us all ! – Cbhihe Mar 15 '19 at 17:38
  • Thanks for pointing that out. I found that Marks response was easier to understand and edit to try other tasks. – DizzyNYC Mar 19 '19 at 2:56
[ $(find /directory -type f -mtime +1 | wc -l) -gt 0 ] && echo 'YES' > /directory/MAIN.txt
| improve this answer | |
  • 3
    Same here. What if a "bystander" file which happens to live in directory is +1 days old? For that to happen, MAIN.txt would be "punished" with a "YES", while not guilty. – Micha Mar 14 '19 at 22:20
  • 1
    That's what I get for answering while in meeting. :| – Mark Wagner Mar 15 '19 at 15:18
  • I added a -name MAIN.txt and it appears to work. It looks like both answers work for my example. – DizzyNYC Mar 15 '19 at 16:03
  • @DizzyNYC: Don't just add -name MAIN.TXT. Make sure you add -maxdepth 1 as well, if not you might find yourself searching deep into directory trees where maybe one day you will encounter another altogether different MAIN.TXT. --- As a general rule and search strategy you want to limit your tree search to the strict minimum, because stat'ing files is costly and you might get false positive in trees that you don't know. (Do man stat for more info. ) – Cbhihe Mar 15 '19 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.