从文件名中删除单词列表 [英] Remove a list of words from filename

查看:32
本文介绍了从文件名中删除单词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从某个目录中的所有文件中删除特定单词的列表,并将它们替换为任何内容.

I am trying to remove a list of specific words from all my files with a certain directory and replace them with nothing.

所以:

这个很棒的内容 720p BLAH FOO BANG OOO - 30.9.2013.mp4

This Awesome Content 720p BLAH FOO BANG OOO - 30.9.2013.mp4

变成:

这个很棒的内容 - 30.9.2013.mp4

This Awesome Content - 30.9.2013.mp4

现在以下内容非常适合单个查找和替换一个单词.

Now the following works great for a single find and replace one word.

find path/to/folder/ -maxdepth 3 -name '*.*' -execdir bash -c 'mv -i "$1" "${1//foo/}"' bash {} ;

我也尝试过多次发现,但这似乎是一个很长的路要走,而且我似乎遇到了这样的问题.

I have also tried just multiple finds but this seems like a very long way of doing it and i seem to run into issues this way.

我有几个问题:

  • 希望它不区分大小写
  • 需要${1//foo/}"来引用列表
  • 如果大于 1,则删除空格

尝试在 cronjob 上将其作为 bash 脚本运行.

Trying to run this as a bash script on a cronjob.

除非有更好的方法来删除This Awesome Content" - 30.9.2013.mp4"之间的所有内容.

Unless there is a better to way to remove everything between "This Awesome Content" - "30.9.2013.mp4".

非常感谢.

推荐答案

您可以使用echo"命令将文件名作为变量访问.完成后,进行所需更改的最有效方法是使用sed".您可以使用-e"标志将 sed 命令串在一起.作为 bash 中 for 循环的一部分,这一行为您提供了一个开始.您也可以在查找"语句中使用这样的一行.

You can access a file's name as a variable using the 'echo' command. Once that is done, the most powerful way to make your desired changes is to use 'sed'. You can string together sed commands with the '-e' flag. As part of a for loop in bash, this line gives you a start. You can also use a line like this as part of your 'find' statement.

echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI'

获得所需的文件名后,您可以将它们移回原始名称.如果您需要更具体的说明,请告诉我.

Once you have your desired file names, you can then move them back to the original name. Let me know if you need more specific instructions.

更新:这是一个更完整的解决方案.您必须将脚本调整为您自己的文件名等.

UPDATE: Here is a more complete solution. You'll have to tune the script to your own file names, etc.

for fyle in $(find . -name "*.*")
do 
   mv -i $fyle `echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI' `
done

最后,要用一个空白字符替换多个空白字符,您可以添加另一个 sed 命令.这是一个有效的命令:

Finally, to replace more than one whitespace character with one whitespace character, you can add another sed command. Here is a working command:

echo "file    input.txt" | sed 's/  */ /g'

这篇关于从文件名中删除单词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆