gpt4 book ai didi

linux - 在 Bash 中复制和重命名脚本

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:25 25 4
gpt4 key购买 nike

我正在尝试创建一个复制和重命名文件脚本。这是我目前所拥有的。

read COPYDIR

read DESTINATION

for file in `ls $COPYDIR`;do
if [ -f $COPYDIR/$file ]; then
cp $file $DESTINATION
fi
done

我正在尝试为复制的文件添加 .bak 扩展名。如何将其添加到脚本中?

最佳答案

您不需要解析 ls 的输出。你可以这样做:

for file in "$COPYDIR"/*; do 
f="${file##*/}"
[[ -f "$file" ]] && cp "$file" "$BAK_DEST_DIR1/$f.bak"
done

关于linux - 在 Bash 中复制和重命名脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670829/

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