在 Linux 上删除多个文件的部分文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12174947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:11:59  来源:igfitidea点击:

Removing part of a filename for multiple files on Linux

linuxbashshell

提问by shaq

I want to remove test.extrafrom all of my file names in current directory

我想从当前目录中的所有文件名中删除test.extra

for filename in *.fasta;do 

    echo $filename | sed \e 's/test.extra//g'

done

but it complains about not founding file.echo is to be sure it list correctly.

但它抱怨没有创建 file.echo 是为了确保它正确列出。

采纳答案by hostmaster

First of all use 'sed -e' instead of '\e'

首先使用 'sed -e' 而不是 '\e'

And I would suggest you do it this way in bash

我建议你在 bash 中这样做

for filename in *.fasta; do 
    [ -f "$filename" ] || continue
    mv "$filename" "${filename//test.extra/}"

done

回答by Jon Lin

For one thing, you have a \einstead of -e.

一方面,您有一个\e而不是-e.

回答by Rody Oldenhuis

Try the renamecommand:

试试rename命令:

rename 's/test.extra//g' *.fasta

回答by theon

Try rename "extra.test" "" *

尝试 rename "extra.test" "" *

Or rename 's/extra.test//;' *

或者 rename 's/extra.test//;' *

$ find
./extra.test-eggs.txt
./extra.testbar
./fooextra.test
./ham-extra.test-blah

$ rename "extra.test" "" *
$ find
./-eggs.txt
./bar
./foo
./ham--blah

回答by bobbogo

$ mmv '*test.extra*.fasta' '#1#2.fasta'

This is safe in the sense that mmvwill not do anything at all if it would otherwise overwrite existing files (there are command-line options to turn this off).

这是安全的,mmv如果它会覆盖现有文件,它根本不会做任何事情(有命令行选项可以关闭它)。

回答by StephaneAG

I know this tread is old, but the following oneliner, inspired from the validated answer, helped me a lot ;)

我知道这种胎面很旧,但以下 oneliner 受验证答案的启发,对我帮助很大;)

for filename in ./*; do mv "./$filename" "./$(echo "$filename" | sed -e 's/test.extra//g')";  done

回答by Petrider

In Kali linuxrename command is rename.ul

Kali linux 中重命名命令是rename.ul

rename.ul 'string-to-remove' 'string-to-replace-with' *.jpg

rename.ul 'string-to-remove' 'string-to-replace-with' *.jpg

example: rename.ul 'useless-string' '' *.jpg This will delete useless-string from all the jpg image's filname.

例如: rename.ul 'useless-string' '' *.jpg 这将从所有 jpg 图像的文件名中删除无用字符串。

回答by Hakeem P Ali

 // EXTENSION - File extension of files
 // STRING - String to be Replace

      for filename in *.EXTENSION;
      do  [ -f "$filename" ] || continue;  
      mv "$filename" "${filename//STRING/}"; 
      done