unix mv --backup=编号 [英] unix mv --backup=numbered

查看:27
本文介绍了unix mv --backup=编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 php 中移动一个文件夹,但如果存在重复,则将两个文件都保存在 dest 文件夹中.我尝试在递归中做到这一点,但它太复杂了,所以很多事情都会出错,例如文件权限和重复的文件\文件夹.

I'm trying in php to move a folder but keep both files in the dest folder if exist duplicate. i tried todo that in recursion but its too complicated so many things can go wrong for example file premissions and duplicate files\folders.

我正在尝试使用 system() 命令,但我无法弄清楚如何移动文件,但如果重复而不破坏扩展名,请保留备份

im trying to work with system() command and i cant figure out how to move files but keep backup if duplicate without destroying the extension

$last_line = system('mv --backup=t websites/test/ websites/test2/', $retval);

如果文件在两个目录中都存在,则给出以下内容:

gives the following if file exist in both dirs:

ajax.html~
ajax.html~1
ajax.html~2

我要找的是:

ajax~.html
ajax~1.html
ajax~2.html

或任何其他类似 (1), (2) ... 但不会破坏文件的扩展名.有任何想法吗?请.

or any other like (1), (2) ... but without ruining the extension of the file. any ideas? please.

ps 必须使用 system() 命令.

p.s must use the system() command.

推荐答案

对于这个问题,我在下面的这个函数中找到并交换这些扩展名(将我的目标目录作为我的参数):

For this problem, I get sed to find and swap those extensions after the fact in this function below (passing my target directory as my argument):

swap_file_extension_and_backup_number ()
{
IFS=$'\n'
for y in $(ls $1)
do
    mv $1/`echo $y | sed 's/ /\\ /g'` $1/`echo "$y" | sed 's/\(\.[^~]\{3\}\)\(\.~[0-9]\{1,2\}~\)$/\2\1/g'`
done
}

该函数假定您的文件扩展名将是正常的 3 个字符长,这将找到长达两位数的备份,即 .~99~

The function assumes that your file extensions will be the normal 3 characters long, and this will find backups up to two digits long i.e. .~99~

说明:

这部分 $1/`echo $y |sed 's//\\/g'` $1/`echo "$y"表示 mv 的第一个参数(原始文件),但通过添加转义来保护您免受空格字符的影响.

This part $1/`echo $y | sed 's/ /\\ /g'` $1/`echo "$y" represents the first argument (the original file) of mv but protects you from space characters by adding an escape.

最后一部分 $1/`echo "$y" |sed 's/\(\.[^~]\{3\}\)\(\.~[0-9]\{1,2\}~\)$/\2\1/g'当然是两个括号组交换的目标文件.ie/\2\1/

The last part $1/`echo "$y" | sed 's/\(\.[^~]\{3\}\)\(\.~[0-9]\{1,2\}~\)$/\2\1/g' is of course the target file where two parenthetic groups are swapped .i.e. /\2\1/

这篇关于unix mv --backup=编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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