gpt4 book ai didi

linux - 在 BASH 中使用特殊字符从文件名中批量删除子字符串

转载 作者:可可西里 更新时间:2023-11-01 11:45:44 25 4
gpt4 key购买 nike

我的目录中有一个文件列表:

opencv_calib3d.so2410.so
opencv_contrib.so2410.so
opencv_core.so2410.so
opencv_features2d.so2410.so
opencv_flann.so2410.so
opencv_highgui.so2410.so
opencv_imgproc.so2410.so
opencv_legacy.so2410.so
opencv_ml.so2410.so
opencv_objdetect.so2410.so
opencv_ocl.so2410.so
opencv_photo.so2410.so

它们是批量重命名的一系列错误的产物,现在我不知道如何从它们中删除中间的“.so”。例如:

opencv_ocl.so2410.so 应该是 opencv_ocl2410.so

这是我试过的:

# attempt 1, replace the (first) occurrence of `.so` from the filename
for f in opencv_*; do mv "$f" "${f#.so}"; done

# attempt 2, escape the dot
for f in opencv_*; do mv "$f" "${f#\.so}"; done

# attempt 3, try to make the substring a string
for f in opencv_*; do mv "$f" "${f#'.so'}"; done

# attempt 4, combine 2 and 3
for f in opencv_*; do mv "$f" "${f#'\.so'}"; done

但是所有都没有效果,产生了错误信息:

mv: ‘opencv_calib3d.so2410.so’ and ‘opencv_calib3d.so2410.so’ are the same file
mv: ‘opencv_contrib.so2410.so’ and ‘opencv_contrib.so2410.so’ are the same file
mv: ‘opencv_core.so2410.so’ and ‘opencv_core.so2410.so’ are the same file
...

最佳答案

在你的 mv 命令中试试这个:

mv "$f" "${f/.so/}"

.so 的第一个匹配项被替换为空字符串。

关于linux - 在 BASH 中使用特殊字符从文件名中批量删除子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252873/

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