Shell function recursive call implements a comparative copy of the directory

Source: Internet
Author: User

Purpose and Requirements:

When porting, you need to integrate the new content in the root filesystem rootfs1 into the ROOTFS2, but not the things rootfs2. That is, you can only compare the similarities and differences of two file systems, moving more than 2 out of 1 to 2.

Difficulties:

If you have the same subdirectory in the directory, make the subdirectories meet the above requirements.

The finishing touches:

Shell recursive invocation, referring to binary tree depth-first traversal.

Real:

1#!/bin/Bash2 3 Cpdifffile ()4 {5 6dir1=$17dir2=$28Files1= 'ls$DIR 1 '9#files2 = 'ls$DIR 2 'Ten      forFile1inch$files 1 One      Do A         ls$DIR 2/$file 1 >/dev/NULL 2>&1 -         if[$?-eq0 ] -          Then the             Echo$DIR 2 havefile$file 1 -         Else -             EchoWouldCP$DIR 1/$file 1 to $DIR 2 -             CP-a $DIR 1/$file 1 $DIR 2/ +         fi -      Done + } A  at Cpsamedir () - { -Cpdifffile $1$2 -Subdirs1= 'ls-L $1|grep "^d"|awk-f\'{print $9}'` -Subdirs2= 'ls-L $2|grep "^d"|awk-f\'{print $9}'` - unset samedirs # #注意, here to empty the array, otherwise the result of the last call is saved here will form a ring, a dead loop inI=0 -      forSubDir1inch$subdirs 1 to      Do +         ls$2/$subdir 1 >/dev/NULL 2>&1 -         if[$?-eq0 ] the          Then *samedirs[i]=$subdir 1 $Let i++Panax Notoginseng  -         Else the             Echo$1Havedir$subdir 1, but $2Not . +         fi A      Done the     Echo "Bettenw $ and $, the same dir is:" +     Echo${samedirs[@]} -  $      forSamedirinch${samedirs[@]} $      Do -         Echo "Now oparate $1/$samedir and $2/$samedir" -Cpsamedir $1/$samedir $2/$samedir the      Done - Wuyi  the  - } Wu  - Main () About { $Cpsamedir $1$2 -  - } -  AMain $1$2

Shell function recursive call implements a comparative copy of the directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.