0

I am trying to write a script that remove files ending .csv from an input folder. I try to write this:

# the direction path of the folder
dirPath="$1"
# remove the files ending .csv
find $dirPath -type f -name "*.csv" | while read filename; do
rm "$filename"
     done

But it's not working, how can we fix it?

2
  • 1
    What is not working, what did you expect to happen or see that did not? Is there any error? Commented Apr 14, 2022 at 9:36
  • There is no error but it's not deleted the files Commented Apr 14, 2022 at 9:45

3 Answers 3

4

Why make it so complicated, while find has a -delete switch?

find $dirPath -type f -name "*.csv" -delete
Sign up to request clarification or add additional context in comments.

1 Comment

Minor note: While it seems to be rather portable, -delete is not POSIX. Related comparison with -exec rm: unix.stackexchange.com/questions/167823/finds-exec-rm-vs-delete
4

Try this

find /path/to/directory -type f -name "*.csv" -exec rm -f {} \;

Comments

2

you can do find $dirPath -name \*.csv -exec rm -f {}

exec will let you to execute rm on each file found.

Refer Here

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.