在bash备份文件循环 [英] Backup file loop in bash

查看:157
本文介绍了在bash备份文件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个脚本,需要一个目录,将所有文件复制从它一个新的,然后重命名新目录中的所有文件,包括在他们的名字-Backup分机号之前。

I'm writing a script that takes a directory, copies all the files from it to a new one, then renames all the files in the new directory to include a -BACKUP in their name before an extension.

到目前为止,我有:

dir1=$1
dir2=$2

mkdir $dir2
cp -r $dir1/. $dir2

for i in $dir2
do
file=$(basename "$i")
b=${file%.*}
c=${file#"$stub"}
d=-BACKUP
e=$b$d$c
mv $i $e

done

虽然这只是改变了目录,而不是在目录中的所有文件的名称。

Although this only changes the name of the directory, not all the files within the directory.

我在做什么错了,你会怎么处理这个?

What am I doing wrong, how would you approach this?

推荐答案

我会使用找到改名上支持它的系统​​:

I would use find and rename on systems that support it:

cp -R "$dir1" "$dir2"
find "$dir2" | xargs rename 's/$/-BACKUP/'

这篇关于在bash备份文件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆