Compare file size and then copy/overwrite


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare file size and then copy/overwrite
# 1  
Old 03-02-2016
Compare file size and then copy/overwrite

// Redhat

I have this code working, but need to add one more qualification so that I don't overwrite the files.

Code:
#!/bin/sh
cd /P2/log/cerner_prod/millennium/archive/
for f in *
do      [ -f "$f" ] || continue     #If this isn't a regular file, skip it.
        [ -e "/P2/backup/$f" ] && continue  #If a backup already exists, skip it.
        cp "$f" /P2/backup/          # Make a backup copy.
done

What I like to do is:

1) compare the size of the same file (i.e. p2_000001.gz).
2) if the file size (p2_000001.gz) under /P2/backup/ is larger than that under /P2/log/cerner_prod/millennium/archive/, then skip it
3) if if the file size (p2_000001.gz) under /P2/backup/ is smaller than that under /P2/log/cerner_prod/millennium/archive/, then copy or overwrite

In this case, /P2/backup/ will have always the latest file.

Please advise. Appreciate your help!
# 2  
Old 03-02-2016
It won't be the latest but the largest file.

Do you NEED to run that with sh? Does your system have the stat (or equivalent) command?

---------- Post updated at 22:58 ---------- Previous update was at 22:49 ----------

With bash and stat:
Code:
 (( ($(stat -c"%s-" "$f" /P2/backup/"$f") 0)  > 0 )) && cp "$f" /P2/backup/

---------- Post updated at 23:03 ---------- Previous update was at 22:58 ----------

No bash required:
Code:
expr $(stat -c"%s >" "$f") $(stat -c"%s" /P2/backup/"$f") && cp "$f" /P2/backup/

These 3 Users Gave Thanks to RudiC For This Post:
# 3  
Old 03-04-2016
Much appreciated!
Code:
 (( ($(stat -c"%s-" "$f" /P2/backup/"$f") 0)  > 0 )) && cp "$f" /P2/backup/

works absolutely charming.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file dates before copy

Sometimes when I boot, my system goes into emergency mode. I then use Clonezilla to restore an image. Usually the image is older than the current date. This is part of a backup script that runs as a startup program. cd /home/andy/bin/ zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py... (22 Replies)
Discussion started by: drew77
22 Replies

2. Shell Programming and Scripting

Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well #/bin/bash for i in `cat ip` do scp /etc/resolv.conf root@$ip done But I need to take backup of the file i will overwrite .. is there any simple way ? Kindly respond (5 Replies)
Discussion started by: heman96
5 Replies

3. Shell Programming and Scripting

Compare two files, then overwrite first file with only that in both files

I want to compare two files, and search for items that are in both. Then override the first file with that containing only elements which were in both files. I imagine something with diff, but not sure. File 1 One Two Three Four Five File 2 One Three Four Six Eight (2 Replies)
Discussion started by: castrojc
2 Replies

4. Shell Programming and Scripting

overwrite only if both files are the same size

Dear users, I've been looking for a way to overwrite files only if both have the same size, how could I do this? any help is very appreciated. Best regards, Gery (5 Replies)
Discussion started by: Gery
5 Replies

5. Shell Programming and Scripting

Script to Compare file size and delete the smaller

I am pretty new to scripting, so I appreciate your advice in advance. The problem: 100 directories each containing 2 files that have the same extension with random names. The only attribute that discriminates the files is size. I would like to write a script that compares the files for size... (6 Replies)
Discussion started by: JC_1
6 Replies

6. UNIX for Dummies Questions & Answers

PSFTP- Compare file size

Hi, I'm using PSFTP to transfer files from one machine to a virtual machine with UBUNTU OS installed on it. I'm trying to find a way to make sure the files that I'm uploading / downloading are being uploaded/ downloaded properly. I want to compare the size of the local file and the remote... (0 Replies)
Discussion started by: sessie
0 Replies

7. UNIX for Dummies Questions & Answers

Copy a file based on the size-Urgent

Hi, I need unix code to check the size of a file. for example if the size of the file in A folder is more than 1BM, then i have to move that particular file in to B folder whenever I run that particular script. regards, Srinivas. (7 Replies)
Discussion started by: vysrinivas
7 Replies

8. Shell Programming and Scripting

How to compare size of two file which is in one directory

I have two file in a Directory.I want a script which will compare the Size of Two file. Can Anyone Help me on this: linasplg11:/opt/dataout/kk/linasplg11 # cat size -rwxrwxrwx 1 root root 16658 Jan 8 13:58 lina_IP_SIP_1231325621210.xml -rwxr-xr-x 1 root root 16672 Jan 8 14:30... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

9. Shell Programming and Scripting

compare file size from a output file from a script

Hi guys, firstly I'm working on SunOS 5.10 Generic_125100-10 sun4u sparc SUNW,Sun-Fire-V240 I've made a script to compress two directory and then send them to an other server via ftp. This is working very well. Inside theis script I decide to log usefull data for troubleshooting in case of... (7 Replies)
Discussion started by: moustik
7 Replies

10. Shell Programming and Scripting

How to compare file size after ftp?

Is possible if I want to campare file size on source and destination after ftp transfer? If anybody know, please explain to me. (1 Reply)
Discussion started by: icemania
1 Replies
Login or Register to Ask a Question