Search code examples
linuxrm

Linux: how to remove all the files in a folder except ones with specific names?


I have a folder in linux that contains .csv files of the results of some simulations.

The name of the files are like:

run_0_0.020000_0.010000_15.0_10.0_T0_RealNet.csv
run_0_0.030000_0.090000_10.0_10.0_T0_RealNet.csv
run_0_0.030000_0.080000_12.0_10.0_T0_RealNet.csv

I want to remove all the files except the ones with 15.0_10.0_T0_


Solution

  • You could use the find command with its built in -delete feature but probably simpler if you just $ cp /path/to/dir/*15.0_10.0_T0_* /other/dir then remove the original directory. You can then move the new directory in place of the original. You can remove the old directory with all its contents at once with $ rm -rf /path/to/dir.