如何从目录中删除除少数文件之外的所有文件?

时间:2023-01-24 16:48:31

I have a directory structure something like:

我有一个目录结构如下:

/var/www/html/pictures/media0/01/test_01.jpg
/var/www/html/pictures/media0/01/test_02.jpg
/var/www/html/pictures/media0/01/test_03.jpg
/var/www/html/pictures/media0/01/test_04.jpg
/var/www/html/pictures/media0/01/test_05.jpg

I would like to remove all files from 01 except test_03.jpg and test_05.jpg by using a linux command, maybe rm?

除了test_03.jpg和test_05.jpg之外,我想通过使用linux命令来删除所有的文件,可能是rm?

Any ideas?

什么好主意吗?

3 个解决方案

#1


2  

The easiest way is to move the files you want to keep somewhere else, then delete the rest and move them back.

最简单的方法是将你想保存的文件移到别的地方,然后删除其余的文件并将它们移回来。

If this is for a script and you need something fancier I can write something using find that will do it. Let me know.

如果这是一个脚本,你需要一些更高级的东西,我可以用find来写一些东西。让我知道。

#2


0  

shopt -s extglob
echo !(/var/www/html/pictures/media0/test_0[3,5].jpg)

If the output is the files you want to delete replace "echo" with "rm" and run it again.

如果输出是要删除的文件,则将“echo”替换为“rm”并再次运行它。

#3


0  

You can use this:

您可以使用:

rm !(path_to_file/test_0{3,5}.jpg)

This will exclude all files except test_03.jpg and test_05.jpg

这将排除除test_03.jpg和test_05.jpg之外的所有文件

#1


2  

The easiest way is to move the files you want to keep somewhere else, then delete the rest and move them back.

最简单的方法是将你想保存的文件移到别的地方,然后删除其余的文件并将它们移回来。

If this is for a script and you need something fancier I can write something using find that will do it. Let me know.

如果这是一个脚本,你需要一些更高级的东西,我可以用find来写一些东西。让我知道。

#2


0  

shopt -s extglob
echo !(/var/www/html/pictures/media0/test_0[3,5].jpg)

If the output is the files you want to delete replace "echo" with "rm" and run it again.

如果输出是要删除的文件,则将“echo”替换为“rm”并再次运行它。

#3


0  

You can use this:

您可以使用:

rm !(path_to_file/test_0{3,5}.jpg)

This will exclude all files except test_03.jpg and test_05.jpg

这将排除除test_03.jpg和test_05.jpg之外的所有文件