1

I'm trying to run this script:

alias logs="cd L:/"
find $logs -name '*system.log*' -mtime +14 -exec rm {} \;

But get this error: find: missing argument to `-exec'. I've tried looking at other posts on this but can't get it working. I'm using cygwin to run this script on Windows.

OSU222
  • 75
  • 1
  • 2
  • 11
  • Did you find a solution for your latest question that you have deleted? https://stackoverflow.com/q/53381852/6309 – VonC Nov 20 '18 at 20:35

2 Answers2

0

If you have created any alias with name log, then to use it, you should be using $logs and not logs

Also, maybe the find you are using is of Windows and not of Cygwin. This is made clear if you type 'which find'

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • Oops thank you for that I didn't notice as I was trying to figure out the error. I updated my code as I'm still getting the same error. – OSU222 Aug 15 '18 at 13:38
  • You were right it wasn't the Cygwin find. Thank you for your answer. That fixed it! – OSU222 Aug 15 '18 at 13:46
  • 1
    `alias` isn't needed. What's probably happening is `$logs` (as the unquoted expansion of an undefined variable) is disappearing and `find` is searching the current working directory. – chepner Aug 15 '18 at 14:34
  • This error is one emitted by a UNIX-style `find`, so I don't believe the claim of a PATH problem causing the wrong version to be used. Much more likely the script has DOS-style newlines so the last argument is coming out as `;$'\r'` rather than just `;`, which the `-exec` action expects/requires as its terminator. – Charles Duffy Aug 15 '18 at 16:59
  • @chepner, ...why would `find` searching the current directory result in the specific error the OP reports? – Charles Duffy Aug 15 '18 at 17:00
  • @AshishSingh, ...btw, `which find` is less reliable than `type find`, if we know the shell to be bash. `which` tells you what's first in the PATH, but it doesn't know about shell functions, variables, cached lookups, or other internal-shell state that can change the way a command is parsed. – Charles Duffy Aug 15 '18 at 17:05
  • @OSU222, `missing argument to -exec` isn't emitted by Microsoft's version of `find`. How did you determine a different version to be in use, and which version was it? – Charles Duffy Aug 15 '18 at 17:06
  • @CharlesDuffy On cygwin, both, `which` and `type` command gave save result. So may be you are right. Can you elaborate more as to why one should use `type` and not `which` – Ashishkumar Singh Aug 15 '18 at 17:09
  • @AshishSingh, they only *sometimes* give the same command. Run `alias find=echo`, and `type find` will know the difference but `which find` will not. – Charles Duffy Aug 15 '18 at 17:11
  • @CharlesDuffy It wouldn't; I'm simply pointing out that the alias isn't being used in the call to `find` at all. – chepner Aug 15 '18 at 17:11
  • @CharlesDuffy Why the downvote? Using `/usr/bin/find` on cygwin on my system gave no error. So I guessed that using cygwin find will also not give error to filer – Ashishkumar Singh Aug 15 '18 at 17:12
  • @AshishSingh, ...the downvote is because nothing in this answer fixes a ```missing argument to `-exec'``` problem. If the OP claimed that it fixed it for them, they changed something else at the same time without knowing it (for instance, by recreating their script with a UNIX-native editor, thus fixing the newlines). – Charles Duffy Aug 15 '18 at 17:13
  • ...that error only happens when (1) you're running UNIX `find`; (2) it sees a `-exec` primitive; (3) it **does not** see either the two consecutive arguments `{}` `+` later in the command line, or a single `;` argument. Which is to say that the error in question happens only under very specific and narrow circumstances, and this answer is not tailored to those circumstances. – Charles Duffy Aug 15 '18 at 17:17
  • @CharlesDuffy I am no expert in unix, so maybe you're right. Thanks for explaining your point :). – Ashishkumar Singh Aug 15 '18 at 17:18
  • @CharlesDuffy - Based on your comments I think you're right. The issue was a new line at the end of the script causing the error. – OSU222 Aug 15 '18 at 18:14
0

First, some minor fixes:

#!/bin/sh
logs=/cygdrive/l
find "$logs" -name '*system.log*' -mtime +14 -exec rm -- {} +

...but those won't address your real problem (the one causing -exec to report an error), which is almost certainly the presence of DOS newlines in your script.

find -exec reports the error in question when it doesn't see an argument containing only the exact string ;. Outside quotes, \; should be that argument -- but it can be different if your file has hidden characters. And a DOS text file will appear to have hidden characters when opened by a program expecting a UNIX text file, because the two formats have different line separators.

To fix this, open the file in a native-UNIX editor such as vim, run :set fileformat=unix, and save it; or use dos2unix to convert it in-place.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441