Paco

Personal Content Organiser

Home / Linux

Navigation

Menu

Search

find exec

find /path/to/dir -type f -exec chmod 644 {} +

find /path/to/dir -type d -exec chmod 755 {} +



The Delimiter



We need to provide the find command with a delimiter so it’ll know where our -exec arguments stop.

Two types of delimiters can be provided to the -exec argument: the semi-colon(;) or the plus sign (+).

As we don’t want our shell to interpret the semi-colon, we need to escape it (\;).

The delimiter determines the way find handles the expression results. If we use the semi-colon (;), the -exec command will be repeated for each result separately. On the other hand, if we use the plus sign (+), all of the expressions’ results will be concatenated and passed as a whole to the -exec command, which will run only once.

Let’s see the use of the plus sign with another example:

$ find . -name "*.mp3" -exec echo {} + ./Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3 ./Gustav Mahler/02 - Der Einsame im Herbst.mp3 ./Gustav Mahler/03 - Von der Jugend.mp3 ./Gustav Mahler/04 - Von der Schönheit.mp3 ./Gustav Mahler/05 - Der Trunkene im Frühling.mp3 ./Gustav Mahler/06 - Der Abschied.mp3


When running echo, a newline is generated for every echo call, but since we used the plus-delimiter, only a single echo call was made. Let’s compare this result to the semi-colon version:

$ find . -name "*.mp3" -exec echo {} \; ./Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3 ./Gustav Mahler/02 - Der Einsame im Herbst.mp3 ./Gustav Mahler/03 - Von der Jugend.mp3 ./Gustav Mahler/04 - Von der Schönheit.mp3 ./Gustav Mahler/05 - Der Trunkene im Frühling.mp3 ./Gustav Mahler/06 - Der Abschied.mp3


From a performance point of view, we usually prefer to use the plus-sign delimiter, as running separate processes for each file can incur a serious penalty in both RAM and processing time.

However, we may prefer using the semi-colon delimiter in one of the following cases:

- The tool run by -exec doesn’t accept multiple files as an argument.
- Running the tool on so many files at once might use up too much memory.
- We want to start getting some results as soon as possible, even though it’ll take more time to get all the results.