Sam
Sam

Reputation: 798

bash - find: missing argument to `-exec' error

I am trying to clean some files with the help of find command but getting a strange error in the below scenario.

#!/bin/bash
find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \;

Works fine without any error. But when i put a simple completion message throws the below error.See the code below

#!/bin/bash
find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \;
echo "completed"

Is there any syntax error i am making.

Upvotes: 0

Views: 118

Answers (1)

Inian
Inian

Reputation: 85580

Use find command's exit-code and print the error message based on that.

find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \; && echo "File truncation done"

(or) just run the commands sequentially as

find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \; ; echo "File truncation done"

(or) you can use an echo message after truncation of each file as

find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec bash -c 'file="{}"; truncate -s 0 "$file"; echo "$file" is truncated' \;

Upvotes: 1

Related Questions