15

Hope someone can help with this:

Am trying to delete session files on /tmp with this command:

find /tmp -name 'sess_*' -user Username -maxdepth 1 $CMD {} \;

but I got these errors:

find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]

I looked for solutions over the web but could not find any. I have deleted other tmp files with other commands and wonder if that affected some volume or socket.

Thank you in advance

MFIHRI
  • 311
  • 1
  • 3
  • 15

1 Answers1

26

find has three types of options: options that are used to match files (e.g. -name, -user), options that specify actions to perform on the matched files (-print, -exec), and options that control the overall behavior of the command (e.g. -maxdepth, -xdev). The third type must be put before the other two. So it should be:

find /tmp -maxdepth 1 -name 'sess_*' -user Username -exec $CMD {} \;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ah I see, I already tried that but missed the -exec part. Thanks Barmar.The command went thru but every file ends with Permission Denied. I think because am deleting sessions to my username account since /tmp has appropriate permissions: drwxrwxrwt 4 root root 1168384 Sep 15 17:21 – MFIHRI Sep 16 '12 at 06:27
  • Never mind! access was denied for files owned by root only, but others owned by admin were deleted silently.It does not echo anything when deletion is successful. Thank you all. – MFIHRI Sep 16 '12 at 06:46
  • 1
    If you want it to echo something when it finds a matching file, use the -print option. – Barmar Sep 16 '12 at 13:37
  • it's bizarre that I can take a working "find" with something "sh -c" in the exec and it works, but I change one command passed to the shell and suddenly it throws this error! – Michael Feb 22 '17 at 23:54
  • @Michael Sounds like a quoting problem to me. Note that quotes aren't processed when expanding `$CMD`, see http://stackoverflow.com/questions/13365553/setting-an-argument-with-bash – Barmar Feb 23 '17 at 00:04