Bash – Remove everything except csv file Bash Script

bashlinuxrmwildcards

I would like to delete everything from a folder except csv files

I am trying with a bash script and I am getting this error:

syntax error near unexpected token `('

This is my script :

 PATH=/tmp/

 run_spark_local
 rm -v !($PATH*.csv)

 cp -r $PATH /data/logs/

I have also tried

rm -v !("$PATH*.csv")

Best Answer

You should avoid setting the PATH variable. This is used by your shell to find valid commands, setting it to /tmp/ is going to prevent the script from being able to find the rm and cp commands altogether.

You can accomplish what you want with the following find command:

find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr {} \;

Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:

find /tmp -not -name '*.csv' -type f -exec rm -v {} \;

Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth argument:

find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v {} \;

Extra note: I would never run a find ... -exec command that you find online without verifying it will do what you need it to do first. You should run:

find /tmp -not -name '*.csv' -not -path /tmp

And verify it is finding only the files you want before adding the -exec rm -vr {} \; bit.

Related Question