在Linux终端中,如何删除目录中除一个或两个以外的所有文件

时间:2021-11-22 17:31:24

In a Linux terminal, how to delete all files from a folder except one or two?

在Linux终端中,如何从一个或两个文件夹中删除所有文件?

For example.

例如。

I have 100 image files in a directory and one .txt file. I want to delete all files except that .txt file.

我在目录中有100个图像文件和一个.txt文件。我想删除除.txt文件以外的所有文件。

5 个解决方案

#1


31  

From within the directory, list the files, filter out all not containing 'file-to-keep', and remove all files left on the list.

在目录中,列出文件,过滤掉所有不包含'file-to-keep'的文件,并删除列表中剩余的所有文件。

ls | grep -v 'file-to-keep' | xargs rm

To avoid issues with spaces in filenames (remember to never use spaces in filenames), use find and -0 option.

要避免文件名中的空格问题(请记住永远不要在文件名中使用空格),请使用find和-0选项。

find 'path' -maxdepth 1 -not -name 'file-to-keep' -print0 | xargs -0 rm

Or mixing both, use grep option -z to manage the -print0 names from find

或者混合两者,使用grep选项-z来管理来自find的-print0名称

#2


8  

In general, using an inverted pattern search with grep should do the job. As you didn't define any pattern, I'd just give you a general code example:

通常,使用grep的反向模式搜索应该可以完成这项工作。由于您没有定义任何模式,我只是给您一个通用代码示例:

ls -1 | grep -v 'name_of_file_to_keep.txt' | xargs rm -f

The ls -1 lists one file per line, so that grep can search line by line. grep -v is the inverted flag. So any pattern matched will NOT be deleted.

ls -1每行列出一个文件,以便grep可以逐行搜索。 grep -v是倒置标志。因此,任何匹配的模式都不会被删除。

For multiple files, you may use egrep:

对于多个文件,您可以使用egrep:

ls -1 | grep -E -v 'not_file1.txt|not_file2.txt' | xargs rm -f

Update after question was updated: I assume you are willing to delete all files except files in the current folder that do not end with .txt. So this should work too:

问题更新后更新:我假设您愿意删除除当前文件夹中不以.txt结尾的文件之外的所有文件。所以这也应该有效:

find . -maxdepth 1 -type f -not -name "*.txt" -exec rm -f {} \;

#3


4  

find supports a -delete option so you do not need to -exec. You can also pass multiple sets of -not -name somefile -not -name otherfile

find支持-delete选项,因此您不需要-exec。您还可以传递多组-not -name somefile -not -name otherfile

user@host$ ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt josh.pdf keepme

user@host$ find . -maxdepth 1 -type f -not -name keepme -not -name 8.txt -delete

user@host$ ls
8.txt  keepme

#4


3  

Use the not modifier to remove file(s) or pattern(s) you don't want to delete, you can modify the 1 passed to -maxdepth to specify how many sub directories deep you want to delete files from

使用not修饰符删除您不想删除的文件或模式,您可以修改传递给-maxdepth的1,以指定要从中删除文件的深度子目录数

find . -maxdepth 1 -not -name "*.txt" -exec rm -f {} \;

You can also do:

你也可以这样做:

find  -maxdepth 1 \! -name "*.txt" -exec rm -f {} \;

#5


2  

In bash, you can use:

在bash中,您可以使用:

$ shopt -s extglob  # Enable extended pattern matching features    
$ rm !(*.txt)       # Delete all files except .txt files

#1


31  

From within the directory, list the files, filter out all not containing 'file-to-keep', and remove all files left on the list.

在目录中,列出文件,过滤掉所有不包含'file-to-keep'的文件,并删除列表中剩余的所有文件。

ls | grep -v 'file-to-keep' | xargs rm

To avoid issues with spaces in filenames (remember to never use spaces in filenames), use find and -0 option.

要避免文件名中的空格问题(请记住永远不要在文件名中使用空格),请使用find和-0选项。

find 'path' -maxdepth 1 -not -name 'file-to-keep' -print0 | xargs -0 rm

Or mixing both, use grep option -z to manage the -print0 names from find

或者混合两者,使用grep选项-z来管理来自find的-print0名称

#2


8  

In general, using an inverted pattern search with grep should do the job. As you didn't define any pattern, I'd just give you a general code example:

通常,使用grep的反向模式搜索应该可以完成这项工作。由于您没有定义任何模式,我只是给您一个通用代码示例:

ls -1 | grep -v 'name_of_file_to_keep.txt' | xargs rm -f

The ls -1 lists one file per line, so that grep can search line by line. grep -v is the inverted flag. So any pattern matched will NOT be deleted.

ls -1每行列出一个文件,以便grep可以逐行搜索。 grep -v是倒置标志。因此,任何匹配的模式都不会被删除。

For multiple files, you may use egrep:

对于多个文件,您可以使用egrep:

ls -1 | grep -E -v 'not_file1.txt|not_file2.txt' | xargs rm -f

Update after question was updated: I assume you are willing to delete all files except files in the current folder that do not end with .txt. So this should work too:

问题更新后更新:我假设您愿意删除除当前文件夹中不以.txt结尾的文件之外的所有文件。所以这也应该有效:

find . -maxdepth 1 -type f -not -name "*.txt" -exec rm -f {} \;

#3


4  

find supports a -delete option so you do not need to -exec. You can also pass multiple sets of -not -name somefile -not -name otherfile

find支持-delete选项,因此您不需要-exec。您还可以传递多组-not -name somefile -not -name otherfile

user@host$ ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt josh.pdf keepme

user@host$ find . -maxdepth 1 -type f -not -name keepme -not -name 8.txt -delete

user@host$ ls
8.txt  keepme

#4


3  

Use the not modifier to remove file(s) or pattern(s) you don't want to delete, you can modify the 1 passed to -maxdepth to specify how many sub directories deep you want to delete files from

使用not修饰符删除您不想删除的文件或模式,您可以修改传递给-maxdepth的1,以指定要从中删除文件的深度子目录数

find . -maxdepth 1 -not -name "*.txt" -exec rm -f {} \;

You can also do:

你也可以这样做:

find  -maxdepth 1 \! -name "*.txt" -exec rm -f {} \;

#5


2  

In bash, you can use:

在bash中,您可以使用:

$ shopt -s extglob  # Enable extended pattern matching features    
$ rm !(*.txt)       # Delete all files except .txt files