0

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.

7
  • 1
    What /Where is the error? Commented Dec 6, 2016 at 11:38
  • You could combine statements like find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \; ; echo "Hello" (or) find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \; && echo "Hello" Commented Dec 6, 2016 at 11:41
  • The second piece of code in the question where there is a echo after the find command gives error.Strange thing id if i remove the echo command at last it works fine. Commented Dec 6, 2016 at 11:41
  • You wan to know if the truncate has happened and log a message for each file? Is that right? Commented Dec 6, 2016 at 11:43
  • find . -type f -newermt 2011-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \; ; echo "Hello" ===> This worked for me.Thanks a lot for the help.It was eating my head for about 1 hr. Commented Dec 6, 2016 at 11:46

1 Answer 1

1

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' \;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.