在Bash中复制和重命名脚本

时间:2021-03-12 10:46:20

I am trying to create a copy and rename files script. Here is what I have so far.

我正在尝试创建一个副本并重命名文件脚本。这是我到目前为止所拥有的。

read COPYDIR

read DESTINATION

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

I'm trying to add a .bak extension to the files that are copied. How can I add this to the script?

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

1 个解决方案

#1


1  

You don't need to parse ls's output. You can do:

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

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

#1


1  

You don't need to parse ls's output. You can do:

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

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