bash - 使用 Bash 替换目录/子目录名称中的点或字符

标签 bash ubuntu find ubuntu-14.04 rename

我一直在寻找正确的解决方案。我找到了一些相近的例子。 Bash script to replace spaces in file names 但我正在寻找的是如何替换当前目录/子目录名称中的多个 . 点,然后“递归地”替换 FILENAMES 中的多个 . 点(不包括 *.extension) .

这个例子很接近,但不正确:

find . -maxdepth 2 -name "*.*" -execdir rename 's/./-/g' "{}" \;

另一个例子,但也不正确:

for f in *; do mv "$f" "${f//./-}"; done

所以

dir.dir.dir/dir.dir.dir/file.file.file.ext

会变成

dir-dir-dir/dir-dir-dir/file-file-file.ext

最佳答案

您可以像这样分配给变量和管道:

x="dir.dir.dir/dir.dir.dir/file.file.file.ext"
echo "$(echo "Result: ${x%.*}" | sed 's/\./_/g').${x##*.}"
Result: dir_dir_dir/dir_dir_dir/file_file_file.ext

关于bash - 使用 Bash 替换目录/子目录名称中的点或字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925147/

相关文章:

algorithm - 使用 bash 生成 cron 配置?

Bash:意外标记 `else' 附近的语法错误,使用 ssh 的 grep 终端

linux - Linux 下 find 实用程序中的独占参数

node.js - 您将 $NVM_DIR 设置为 "/root/.nvm",但该目录不存在。检查您的配置文件和环境

regex - linux 查找名称中包含可选字符的文件

bash scp 没有那个文件或目录

apache - 在调用 CGI 并且 URI 包含没有值的单个查询字符串参数时,Apache2 中是否存在特殊情况?

Apache 未遵循符号链接(symbolic link) (403) : Creating Symlinks Within Vagrant : Windows Host, Ubuntu guest

string - LUA:在双引号中查找并返回字符串

C++ 在固定大小为 9 的右填充空终止字符数组中查找第一个空格的最快方法