[英]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.我在 linux 中有一个文件夹,其中包含一些模拟结果的.csv
文件。
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_
我想删除除15.0_10.0_T0_
之外的所有文件
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.您可以使用具有内置 -delete 功能的find
命令,但如果您只需$ cp /path/to/dir/*15.0_10.0_T0_* /other/dir
然后删除原始目录,则可能更简单。 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
.您可以使用$ rm -rf /path/to/dir
一次性删除旧目录及其所有内容。
例如,如果您不想删除包含 15.0_10.0_T0_ 的文件,则可以使用 find:
find . -type f ! -name '*15.0_10.0_T0_*' -delete
Another variant to remove everything but the requested files:删除除请求文件之外的所有内容的另一种变体:
ls --ignore="*15.0_10.0_T0_*" | xargs rm
Or a shortened, don't ask and remove also dir version (take with care :) ):或者缩短,不要询问并删除 dir 版本(小心:)):
ls -I "*15.0_10.0_T0_*" | xargs rm -rf
use mv /dire/15.0_10.0_T0_* to destination dir then delete whole directory by using sudo rm -rf directory name.使用 mv /dire/15.0_10.0_T0_* 到目标目录,然后使用 sudo rm -rf 目录名删除整个目录。 Hope it works.希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.