Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash循环以删除与名称结构匹配的文件_Bash_File_Loops_Terminal - Fatal编程技术网

Bash循环以删除与名称结构匹配的文件

Bash循环以删除与名称结构匹配的文件,bash,file,loops,terminal,Bash,File,Loops,Terminal,使用bash循环,我想删除文件夹中三种类型的文件名之一 1) L4.mp3 2) L4 with music.mp3 3) L4 noIntro with music.mp3 这三种类型的文件有很多,唯一改变的是L后面的数字 我想: 保留所有名为1的文件(原始文件) 保留所有名为3的文件(添加音乐和删除简介) 但是删除所有名为2的文件(添加了音乐,但没有介绍) 最好的方法是什么 我最初的想法(无论如何都没有编译)是: 但产出是: -bash: name: command not found -

使用bash循环,我想删除文件夹中三种类型的文件名之一

1) L4.mp3

2) L4 with music.mp3

3) L4 noIntro with music.mp3

这三种类型的文件有很多,唯一改变的是L后面的数字

我想: 保留所有名为1的文件(原始文件)

保留所有名为3的文件(添加音乐和删除简介)

但是删除所有名为2的文件(添加了音乐,但没有介绍)

最好的方法是什么

我最初的想法(无论如何都没有编译)是:

但产出是:

-bash: name: command not found

-bash: name: command not found

-bash: name: command not found

-bash: name: command not found

不确定为什么需要循环,但以下是一些建议:

ls *.mp3 | grep '(1)' | xargs -d"\n" rm
这将获取所有包含表达式(1)的.mp3文件,例如abc(1).mp3等

如果您希望在您的方法中具有更广泛的影响范围或更大的动态性,我建议您在grep中使用regex:

ls *.mp3 | grep -P "regex-here" | xargs -d"\n" rm
如果要在同一命令中删除少数其他类型的文件,如mp4等,请使用:

ls *.{mp3,mp4,mpeg} | grep -P "regex-here" | xargs -d"\n" rm
您还可以使用regex并在查找的帮助下删除:

find -iregex 'regex-here' -delete

Bash不编译。曾经所以您可能在终端上收到错误消息,请将其添加到问题中。添加此文件时,请格式化代码部分(使用编辑器中的
{}
图标)。
rm*“noIntro withMusic.mp3”
?使用。@funky future这将非常好。在Python中如何实现这一点?if语句的语法应该是
if[“$name”==“$somevariable”]
find -iregex 'regex-here' -delete