bash 使用 cut 从一堆文件名中删除前 n 个字符

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

Remove first n character from bunch of file names with cut

bashcut

提问by Ridalgo

I am using

我在用

ls | cut -c 5-

ls | cut -c 5-

This does return a list of the file names in the format i want them, but doesn't actually perform the action. Please advise.

这确实以我想要的格式返回文件名列表,但实际上并不执行操作。请指教。

回答by Aman

rename -n 's/.{5}(.*)//' *

The -nis for simulating; remove it to get the actual result.

-n是用于模拟; 删除它以获得实际结果。

回答by armtatoo

you can use the following command when you are in the folder where you want to make the renaming:

当您在要进行重命名的文件夹中时,可以使用以下命令:

rename -n -v  's/^(.{5})//' *

-nis for no action and -vto show what will be the changes. if you are satisfied with the results you can remove both of them

-n是为了不采取行动并-v显示将发生什么变化。如果您对结果感到满意,您可以将它们都删除

rename 's/^(.{5})//' *

回答by legoscia

Something like this should work:

这样的事情应该工作:

for x in *; do
    echo mv $x `echo $x | cut -c 5-`
done

Note that this could be destructive, so run it this way first, and then remove the leading echoonce you're confident that it does what you want.

请注意,这可能具有破坏性,因此请先以这种方式运行它,然后在echo您确信它可以满足您的要求时删除前导。

回答by Saumil

If you get an error message saying,

如果您收到一条错误消息,说明

rename is not recognized as the name of a cmdlet

重命名未被识别为 cmdlet 的名称

This might work for you,

这可能对你有用,

get-childitem * | rename-item -newname { [string]($_.name).substring(5) }