0

In a bash script, is the following syntactically correct?:

touch /home/test
find /home -type d -exec rm test {} +;
# Command following {} +;
touch foo;

Thank you, I know there is an abundance of information on this subject, I just need to impress this into my mind differently I suppose.

The Dude man
  • 383
  • 6
  • 19
  • The `;`s are *valid*, but not *necessary*. BTW, you could have gotten this same answer (wrt. syntactical validity or lack thereof) from http://shellcheck.net/. – Charles Duffy Feb 06 '18 at 01:58
  • 3
    First **be careful** you are potentially asking it remove all directories in `/home`. The correct command is `find /home -type d -name "test" -exec rm -r '{}' \;` Without `-name "test"` you would potentially match all directories under `/home`. – David C. Rankin Feb 06 '18 at 01:58
  • @DavidC.Rankin I think **no** directory will be removed by the code the OP presents. - `rm` will refuse to do so without `-r` option. – iBug Feb 06 '18 at 02:00
  • That is valid, but let's 'take the **be careful** route first (heaven forbid he has `alias rm='rm -r'` in his `.bashrc` -- `:)` – David C. Rankin Feb 06 '18 at 02:01
  • Essentially what I am asking is, is the + acting as a terminator, do I still have to end that command with ";" or "&&"? – The Dude man Feb 06 '18 at 02:14
  • 1
    They are mutually exclusive, it's an either/or. The `'+'` will build a command line similar to `xargs` the `';'` just acts as a line termination, **but it must be escaped**, so use `'\;'`. See [find(1) - Linux man page](https://linux.die.net/man/1/find) for details. – David C. Rankin Feb 06 '18 at 02:42
  • Thank you so much! – The Dude man Feb 17 '18 at 17:33

0 Answers0