1

I want to delete .txt and .csv files from a particular folder. Below is the shell script I am using. But its not deleting the files, if I use only of the file extension then it works. Can you please help here. I also used ||, but did not work.

function purge_inbox_donefile(){
 cd $INBOX_DIR
 find . -path "*/done*" -iname ["*.txt" -o "*.csv"]  -exec rm -f {} \; -print
}

1 Answer 1

1

You can use -o between -iname parameters rather than within the pattern. Such as:

-iname "*.txt" -o -iname "*.csv"

If you need to bracket your expression to keep it distinct from your other parameters, do not forget to escape your brackets, ie:

\( -iname "*.txt" -o -iname "*.csv" \)
4
  • 1
    Also note that find has the -delete flag
    – msfoster
    Apr 7, 2015 at 9:38
  • And using cd within the function instead of passing the directory to find is inelegant and potentially problematic.
    – tripleee
    Apr 7, 2015 at 9:57
  • @borrible ,I tried the script -iname ".txt" -o -iname ".csv" , but its deleting only one type of file( either .csv or .txt), but my folder has both files and I want to delete all files with these extension
    – Swanand
    Apr 7, 2015 at 11:52
  • @Swanand, go in to the directory that contains the file and try find . \( -iname '*.txt' -o -iname '*.csv' \) and see if you're getting all the files listed. If you are (which should be the case, it's certainly what I see on my own setup) then you've an issue with the rest of your script.
    – borrible
    Apr 7, 2015 at 13:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.