简体   繁体   English

Linux:如何删除文件夹中除具有特定名称的文件之外的所有文件?

[英]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.

相关问题 Linux:每天午夜执行脚本,并删除除最近20个文件夹外的所有文件 - Linux: Execute script every day at midnight and remove all files in folder except latest 20 如何更改 linux 上包含特定字符串的所有目录/文件的名称 - how to change names of all directories / files containing a specific string on linux Linux如何删除组为Contoso的所有文件和文件夹 - Linux How to remove all files and folder where group is Contoso Linux命令删除除.git文件夹以外的所有文件? - Linux command to delete all files except .git folder? 如何从文件夹中的所有文件中删除带有特定字符串的行 - How to remove from all files in folder the lines with specific string 除了linux终端中的一种类型之外,删除某种类型的所有文件 - Remove all files of a certain type except for one type in linux terminal 在Linux中删除所有但很少的文件特定模式 - Remove all but few specific pattern of files in Linux 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder 当文件名中包含Linux文件夹中的特定字符时,如何删除文件 - how do I remove files when the filename has specific characters from linux folder 如何删除Linux中除少数提到的文件夹以外的所有文件夹 - How to remove all folders except a few mentioned one in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM