我需要一个函数来添加以'nfcapd'开头的任何文件名
原始文件集:
/root/netflow3/nfcapd.201402090310 -> /a/b/nfcapd.201402090310
/root/netflow3/nfcapd.201402050400 -> /a/b/nfcapd.201402050400
/root/netflow3/nfcapd.201402100730 -> /a/b/nfcapd.201402100730
/root/netflow3/1.nfcapd.201402100730 -> /a/e/nfcapd.201402100730
/root/netflow3/2.nfcapd.201402100730 -> /a/f/nfcapd.201402100730
with prefix =“foo _”:
/root/netflow3/foo_nfcapd.201402090310 -> /a/b/nfcapd.201402090310
/root/netflow3/foo_nfcapd.201402050400 -> /a/b/nfcapd.201402050400
/root/netflow3/foo_nfcapd.201402100730 -> /a/b/nfcapd.201402100730
/root/netflow3/1.nfcapd.201402100730 -> /a/e/nfcapd.201402100730
/root/netflow3/2.nfcapd.201402100730 -> /a/f/nfcapd.201402100730
我想出了一个可以为任何符号链接添加前缀的函数:
readonly PATH_CURRENT_SCRIPT="$(dirname $0)"
sym_rename () {
local PATH_SOURCE_FILES=$1
local prefix=$2
[[ ! -d $PATH_SOURCE_FILES ]] && echo "path $dir not found! Exiting...." && exit 1
cd $PATH_SOURCE_FILES
for f in * ; do mv "$f" "${prefix}_$f" ; done
cd $PATH_CURRENT_SCRIPT
}
唯一的问题是它为所有内容添加前缀:
with prefix =“foo _”:
/root/netflow3/foo_nfcapd.201402090310 -> /a/b/nfcapd.201402090310
/root/netflow3/foo_nfcapd.201402050400 -> /a/b/nfcapd.201402050400
/root/netflow3/foo_nfcapd.201402100730 -> /a/b/nfcapd.201402100730
/root/netflow3/foo_1.nfcapd.201402100730 -> /a/e/nfcapd.201402100730
/root/netflow3/foo_2.nfcapd.201402100730 -> /a/f/nfcapd.201402100730
如何将函数修改为仅以“nfcapd”开头的符号链接文件名前缀?
答案 0 :(得分:2)
只需更改你的glob就可以匹配你想要的了:
cd $PATH_SOURCE_FILES
shopt -s nullglob
for f in nfcapd* ; do mv "$f" "${prefix}_$f" ; done
shopt -u nullglob
cd $PATH_CURRENT_SCRIPT