Bash:如何替换文件夹的前缀

时间:2018-05-17 08:07:33

标签: bash replace

我有一堆文件夹:

test_001
test_002

我想用 ftp 替换前缀 test 以获取:

ftp_001
ftp_002

一个问题:我可以通过最少的安装访问Linux服务器。例如,未安装重命名,甚至可能未安装sed。那么,如何使用纯bash替换前缀?

2 个答案:

答案 0 :(得分:2)

由于您的安装程度最低,我尝试制作一个不需要trsedfind的命令。

<强> INPUT:

$ tree .
.
├── a
├── b
├── c
├── test_001
└── test_002

2 directories, 3 files

<强> CMD:

for d in */; do mv "${d:0:-1}" "ftp"${d:4:-1}; done

<强>输出:

tree .
.
├── a
├── b
├── c
├── ftp_001
└── ftp_002

2 directories, 3 files

关于bash中substrings的解释:https://www.tldp.org/LDP/abs/html/string-manipulation.html

答案 1 :(得分:1)

这个小脚本可能会有所帮助:

for dir in */
do
    mv "$dir" "${dir/test/ftp}"
done

test_00x目录的父级下执行。

它可以用紧凑的单行编写:

for dir in */; do mv "$dir" "${dir/test/ftp}"; done
相关问题