您的位置:首页 > 运维架构 > Shell

shell实现文件名相同路径不同的批量复制

2012-11-11 22:05 274 查看
如果系统存在文件名相同,但路径不同的文件,如果单纯用find来批量复制到一个地方的话会被覆盖掉,下面的脚本是实现根据文件名的路径来进行存放复制。为能更好的测试,脚本中加了在不同路径创建相同文件名的程序。

#!/bin/sh

. /etc/profile

# define
tf=testfile
destpath=/root/found
[ ! -d $destpath ] && mkdir -p $destpath

# touch some the same file for test
TouchFile()
{
echo "/tmp" > /tmp/$tf
echo "/home" > /home/$tf
echo "/root" > /root/$tf
echo "/var/tmp" > /var/tmp/$tf
}

# find the file and copy to the dest dir
FindCopy()
{
TouchFile
if [ $? -eq 0 ];then
for i in $(find / -name $tf);do
[ ! -d $destpath/$(dirname $i) ] && mkdir -p $destpath$(dirname $i)
cp -rf $i $destpath$(dirname $i)
#echo $i
done
else
echo "please touch some test file first..."
fi
}
FindCopy


本文出自 “[reed@卢伟开~]#rm -rf /” 博客,请务必保留此出处http://luweikai.blog.51cto.com/1705672/1057518
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐