bash bash删除文件名的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21691864/
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-09-10 00:39:46  来源:igfitidea点击:

bash removing part of a file name

bashshellsedrenamemv

提问by HattrickNZ

I have the following files in the following format:

我有以下格式的文件:

$ ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv

I would like this part removed:
20140211144020(this is the timestamp the reports were run so this will vary)

我希望删除这部分:(
20140211144020这是运行报告的时间戳,因此会有所不同)

and end up with something like:

并最终得到类似的东西:

CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv

I was thinking simply along the lines of the mvcommand, maybe something like this:

我只是按照mv命令的思路思考,也许是这样的:

$ ls CombinedReports_LLL-*'('*.csv

but maybe a sedcommand or other would be better

但也许sed命令或其他命令会更好

回答by John1024

renameis part of the perlpackage. It renames files according to perl-style regular expressions. To remove the dates from your file names:

renameperl包的一部分。它根据 perl 样式的正则表达式重命名文件。要从文件名中删除日期:

rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv

If renameis not available, sed+shellcan be used:

如果rename不可用,可以使用sed+ shell

for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

The above loops over each of your files. For each file, it performs a mvcommand: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')"where, in this case, sedis able to use the same regular expression as the renamecommand above. s/[0-9]{14}//tells sedto look for 14 digits in a row and replace them with an empty string.

以上循环遍历您的每个文件。对于每个文件,它执行一个mv命令: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')"在这种情况下,sed可以使用与上述rename命令相同的正则表达式。 s/[0-9]{14}//告诉sed在一行中查找 14 位数字并用空字符串替换它们。

回答by Alfe

Without using an other tools like renameor sedand sticking strictly to bashalone:

不使用其他工具,如renamesed并严格坚持bash单独使用:

for f in CombinedReports_LLL-*.csv
do
  newName=${f/LLL-*\(/LLL-(}
  mv -i "$f" "$newName"
done

回答by Pedro Nunes

for f in CombinedReports_LLL-* ; do
    b=${f:0:20}${f:34:500}
    mv "$f" "$b"
done

You can try line by line on shell:

您可以在 shell 上逐行尝试:

f="CombinedReports_LLL-20140211144020(Untitled_11).csv"
b=${f:0:20}${f:34:500}
echo $b

回答by user5318429

I'm using the advice given in the top response and have put the following line into a shell script:

我正在使用顶部响应中给出的建议,并将以下行放入 shell 脚本中:

ls *.nii | xargs rename 's/[f_]{2}//' f_0*.nii

In terminal, this line works perfectly, but in my script it will not execute and reads * as a literal part of the file name.

在终端中,这一行工作得很好,但在我的脚本中,它不会执行并将 * 作为文件名的文字部分读取。

回答by Kevin

You can use the renameutility for this. It uses syntax much like sed to change filenames. The following example (from the rename man-page) shows how to remove the trailing '.bak' extension from a list of backup files in the local directory:

您可以rename为此使用该实用程序。它使用类似于 sed 的语法来更改文件名。以下示例(来自重命名手册页)显示了如何从本地目录中的备份文件列表中删除尾随的“.bak”扩展名:

rename 's/\.bak$//' *.bak