删除重复的文件扩展名 [英] Remove duplicate filename extensions

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

问题描述

我有成千上万个文件名,例如filename.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz

I have thousands of files named something like filename.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz.gz

我正在使用像 find这样的find命令.-name"* .gz *" 定位这些文件,并使用 -exec 或管道连接到xargs并使用一些魔术命令来清除此混乱情况,以便最终得到文件名.gz

I am using the find command like this find . -name "*.gz*" to locate these files and either use -exec or pipe to xargs and have some magic command to clean this mess, so that I end up with filename.gz

请有人帮我想出这个神奇的命令来删除不需要的 .gz 实例.我曾尝试使用 sed's/\.gz//' sed's/(\.gz)//'进行实验,但它们似乎不起作用(或者说老实话,我对sed不太熟悉.我不必顺便使用sed,欢迎任何有助于解决此问题的解决方案:-)

Someone please help me come up with this magic command that would remove the unneeded instances of .gz. I had tried experimenting with sed 's/\.gz//' and sed 's/(\.gz)//' but they do not seem to work (or to be more honest, I am not very familiar with sed). I do not have to use sed by the way, any solution that would help solve this problem would be welcome :-)

推荐答案

使用find和awk的一种方法:

one way with find and awk:

find $(pwd) -name '*.gz'|awk '{n=$0;sub(/(\.gz)+$/,".gz",n);print "mv",$0,n}'|sh 

注意:

  • 我认为您的文件名中没有特殊字符(例如空格...).如果有,则需要在mv命令中引用文件名.
  • 我添加了 $(pwd)以获取找到的名称的绝对路径.
  • 如果正确,您可以删除结尾的 | sh 来检查生成的 mv ... .... cmd.
  • 如果一切看起来不错,请添加 | sh 以执行 mv
  • I assume there is no special chars (like spaces...) in your filename. If there were, you need quote the filename in mv command.
  • I added a $(pwd) to get the absolute path of found name.
  • you can remove the ending |sh to check generated mv ... .... cmd, if it is correct.
  • If everything looks good, add the |sh to execute the mv

在此处查看示例:

这篇关于删除重复的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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