Chksum on two directories then copy if they are not identical or existing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Chksum on two directories then copy if they are not identical or existing
# 1  
Old 07-05-2017
Chksum on two directories then copy if they are not identical or existing

How can I have an intelligent script that will copy from source to destination directory if the file doesnt exist there or the chksum is not match.

Code:
SOURCE directory: 

 for i in `ls`
> do
> echo $i
> md5sum $i
> echo ""
> done
asdasda
00039a616135792fb609d04cf27aed95  asdasda

chksums
5053d1886cbb9a9c5e9399bbc187d030  chksums

zunauthftp.vf6
00039a616135792fb609d04cf27aed95  zunauthftp.vf6

zunauthimap.vf6
6b1815b8f75b651916549dafb5fa347c  zunauthimap.vf6

zunauthpop.vf6
8589e73b814c1241b1c2c887340f711a  zunauthpop.vf6

zunauth.vf6
5590c0992a894493ca126123aa958a2e  zunauth.vf6

zunauweb.vf6
64c5ca1dc4582da68818adae80507c8c  zunauweb.vf6

Code:
Destination:

for i in `ls`
> do
> echo $i
> md5sum $i
> echo ""
> done
chksums
5053d1886cbb9a9c5e9399bbc187d030  chksums

zunauthftp.vf6
00039a616135792fb609d04cf27aed95  zunauthftp.vf6

zunauthimap.vf6
6b1815b8f75b651916549dafb5fa347c  zunauthimap.vf6

zunauthpop.vf6
8589e73b814c1241b1c2c887340f711a  zunauthpop.vf6

zunauth.vf6
5590c0992a894493ca126123aa958a2e  zunauth.vf6

zunauweb.vf6
125993721bfdcc9f8de614a182da0a01  zunauweb.vf6

Code:
In this case, file asdasda(not existing) and zunauweb.vf6(different chksum) should be copied to the destination directory.

# 2  
Old 07-05-2017
Try (not thouroughly tested, so double check!), given SRCPATH and DSTPATH hold the respective directories, and diff offers the options needed:
Code:
cd $DSTPATH
md5sum * > /tmp/dstsum
cd $SRCPATH
md5sum * > /tmp/srcsum
diff -y -b --suppress-common-lines /tmp/srcsum /tmp/dstsum | awk -vdstpath="$DESTPATH" '{print "echo cp", $2, dstpath "/" $2}' | sh

It should print out what it would do in case. When happy with the result, remove the echo command.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-05-2017
It is also worth looking a rsync which has these features and more:

Code:
$ rsync -ac --dry-run $SRCPATH/ $DSTPATH

-a archive mode (see manual, basically recursive whole directory)
-c skip based on checksum, not mod-time & size


remove --dry-run above if everything looks OK to you
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 07-06-2017
After running diff.
thanks

cp: cannot stat ‘aa': No such file or directory
cp: cannot stat ‘asdasda': No such file or directory
cp: cannot create regular file ‘/zunauweb.vf6': Permission denied

---------- Post updated at 07:23 AM ---------- Previous update was at 07:19 AM ----------

Code:
diff -y -b --suppress-common-lines /tmp/srcsum /tmp/dstsum | awk -vdstpath="$DESTPATH" '{print "echo cp", $2, dstpath "/" $2}' | sh

IN that command, where is the sourcepath

Code:
diff -y -b --suppress-common-lines /tmp/src1 /tmp/dest1
3cdbca7c78ccd11f9314e4bf0da3b20d  aa                          <
00039a616135792fb609d04cf27aed95  asdasda                     <
64c5ca1dc4582da68818adae80507c8c  zunauweb.vf6                | 125993721bfdcc9f8de614a182da0a01  zunauweb.vf6

Code:
cat aa
#cd $DSTPATH
cd /tmp/rac
md5sum * > /tmp/src1
#cd $SRCPATH
cd  /tmp/rac1
md5sum * > /tmp/dest1
diff -y -b --suppress-common-lines /tmp/src1 /tmp/dest1 | awk -vdstpath="$DESTPATH" '{print "cp", $2, /tmp/rac1 "/" $2}' | sh

---------- Post updated at 01:05 PM ---------- Previous update was at 07:23 AM ----------

I figured it out. Thank you all.
# 5  
Old 07-06-2017
Figured out what? How did you fix it?
# 6  
Old 07-06-2017
Looking at it again, I think the last "/" $2 can be dropped...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding non-existing words in a list of files in a directory and its sub-directories

Hi All, I have a list of words (these are actually a list of database table names separated by comma). Now, I want to find only the non-existing list of words in the *.java files of current directory and/or its sub-directories. Sample list of words:... (8 Replies)
Discussion started by: Bhanu Dhulipudi
8 Replies

2. Ubuntu

Copy existing Ubuntu to boot from USB

Hello all, I am looking for a way to copy the existing Ubuntu server 12.04 to a USB (with all the packages and such) and make it boot from the USB. I have seen other threads about copying the CD image to the USB, which is not exactly I am looking for. Before I start diving into anything I... (4 Replies)
Discussion started by: br1an
4 Replies

3. UNIX Desktop Questions & Answers

Help for applying same actions to different directories with identical sub-directories

Hello all I have a situation and would be grateful if you will help me... I am not sure whether it is even possible I have few folders/directories ---e.g. A - B - C -in my MAIN directory for each of them I would get the data from folder called DATA and go through a long list of actions which... (5 Replies)
Discussion started by: A-V
5 Replies

4. UNIX for Dummies Questions & Answers

Setting up existing Directories using facls to recursively add an individual user

I've been working with Solaris/Linux for about 4 months now. Let me explain the scenario. There will be two users involved. The owner (curOwner) and the new user (newUser). The server in question is a Solaris 10 box. So curOwner runs an application that is constantly writing logs to lets say.... (2 Replies)
Discussion started by: amadont12
2 Replies

5. Shell Programming and Scripting

Multiple Variables in Array from Existing Directories

I would like to extract directories from a specific place and keep them into an array of variables to run functions into it. Example, bash-3.00$ls adrian bryan caren derrick I want to keep each directory names into a variable adrian --> document bryan --> document caren --> document... (3 Replies)
Discussion started by: lynxlee
3 Replies

6. UNIX for Dummies Questions & Answers

how can i copy those files into other directories have the same name

how can i copy those files into other directories have the same name but different in the end i have files in directory called test: 10_10_asdadfsdfad.txt 10_10_11_asdawqefwkjasd.txt 10_10_11_12_asdafjjhoqwd.txt i want to put them in exist directory thart i have on my system i have... (1 Reply)
Discussion started by: t17
1 Replies

7. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

8. Shell Programming and Scripting

copy substr in existing string in Perl

Any clue to write something to a particular location in Perl? Suppose $line = ‘abc cde 1234” How to write ( example string "test") on location 4 without parsing the whole line. Output should be $line = ‘abctest 1234” this is not search and replace. just to add substring into... (3 Replies)
Discussion started by: jaivipin
3 Replies

9. Shell Programming and Scripting

How to copy one folder to another with existing files

For example, /tmp/folder1 includes /tmp/folder1/a /tmp/folder1/b /tmp/folder2 includes /tmp/c Is there a command without removing files in /tmp/folder2 first to copy the /tmp/folder1 to /tmp/folder2? and the result should be /tmp/folder2 will include only /tmp/folder2/a... (2 Replies)
Discussion started by: lalelle
2 Replies

10. UNIX for Dummies Questions & Answers

du -s -k differences between two identical directories

I tarred a directory from a linux server to a solaris server. When I do a du -s -k on the directory, I get almost 150k difference in the file sizes. If I do a ls | wc -l, it is the same. If I look at the size of the individual files, it is the same. I did an ls on the 2 directories and... (6 Replies)
Discussion started by: csgonan
6 Replies
Login or Register to Ask a Question