2

I found the following line here:

find * -maxdepth 0 -type d -exec tar czvf {}.tar.gz {} \; \;

It is meant to tar gzip each folder in the current directory individually. But running it returns the error:

find: paths must precede expression:

There are a few posts on this error, but they don't relate to the syntax used here.

Does anyone know what is causing this error?

3
  • Where did you find it? The {} in front of tar.gz seem odd to me. I would assume { and } are not valid chars when creating a file (unless you put the name in between backticks/double qoutes).
    – Rinzwind
    Aug 23, 2014 at 23:12
  • Posted AU thread above. Aug 23, 2014 at 23:13
  • @Rinzwind {} in an -exec expression is expanded (by find, not by the shell) to the name of each file found. See man find for details. (It's common to see \{\} or '{}'. In some expressions, {} has to be quoted to prevent actual or attempted shell expansion of it before it's passed to find. But {} doesn't need to be quoted as it's used in this question. For example, find . -maxdepth 1 -exec echo {}.blah \; works fine from bash.) As muru says, the problem here is instead the extra \;. Aug 26, 2014 at 6:11

1 Answer 1

4

I tested this out, and the error is due to the second \;:

# Works fine
find * -maxdepth 0 -type d -exec tar czvf {}.tar.gz {} \;
# Errors out
find * -maxdepth 0 -type d -exec tar czvf {}.tar.gz {} \; \;

This might because the first escaped ; ends the command for the -exec option, and the second escaped ; now becomes part of the the find command, and is treated as a path. This throws an error since the -type expression has already been read.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .