gpt4 book ai didi

Linux shell 脚本查找和重命名文件以删除后缀?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:30 24 4
gpt4 key购买 nike

我有一些这样命名的文件:

file1.c.keep.apple

file2.c.keep.apple

我正在尝试编写一个 shell 脚本,以便我将后缀作为参数传递(在本例中为 apple),它将重命名所有文件,删除 .keep .apple.

执行示例:

script.sh 苹果

导致上面的文件被重命名为

文件1.c

文件2.c

到目前为止,我已经

 #! /bin/sh
find . -type f -name \'*.keep.$1\' -print0 | xargs -0 rename 's/\(.keep.*)$//'

并且文件不会重命名。我知道 find 部分是正确的。我认为我重命名的正则表达式是错误的。我怎样才能让脚本按我想要的方式工作?

最佳答案

I know the find portion is correct

除了它不是。

find . -type f -name "*.keep.$1" -print0 | ...

关于Linux shell 脚本查找和重命名文件以删除后缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14163922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com