overwrite only if both files are the same size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting overwrite only if both files are the same size
# 1  
Old 08-10-2012
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
# 2  
Old 08-10-2012
This is one method to determine if two files have the same size and then act upon it:

Code:
fs1=`wc -c test1`
fs1="${fs1##+([[:space:]])}"
fs1=`echo $fs1 | cut -d ' ' -f1`

fs2=`wc -c test2`
fs2="${fs2##+([[:space:]])}"
fs2=`echo $fs2 | cut -d ' ' -f1`

if ((fs1 != fs2)) then
  echo "files are not equal in size"
else
  echo "files are equal in size"
fi

This User Gave Thanks to spacebar For This Post:
# 3  
Old 08-11-2012
Thanks spacebar, nice trick, I'll try that Smilie
# 4  
Old 08-11-2012
If available on your system; stat --printf %s filename
will give you the size as recorded in the directory without opening and counting the file's contents.

Last edited by RudiC; 08-11-2012 at 04:02 PM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-11-2012
Agreed, RudiC. stat or even ls would be more efficient (much more so if the file's are large) than reading every byte.

Regards,
Alister
# 6  
Old 08-12-2012
Not knowing what your system is, and what options stat offers, this would do the job running stat only once:
Code:
if [  $(( $(stat --printf "%s - " new  old) 0 )) -eq 0 ]; then echo "equal"; else echo "NOT equal"; fi

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 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. #!/bin/sh cd /P2/log/cerner_prod/millennium/archive/ for f in * do || continue #If this isn't a regular file, skip it. && continue #If a backup already... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

2. 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

3. Shell Programming and Scripting

Files have same size

Hello, I want remove files have same size in a directory. this command only find this files. ls -l | awk '$1!~/^d/{if(size!=""){ print}size=$8}' I want to remove the files of the same size. samples: 5 files are same size. I want to keep only first file. Thank you very much for your help. (3 Replies)
Discussion started by: hoo
3 Replies

4. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

5. UNIX for Dummies Questions & Answers

copy only new files or files of a different size

hello i would like to copy files from 1 location to a nother, but it has only to copy files which are newer or have a different filesize. all has to be logged to a copy.log file (als skipped files should be in the log) is this possible with the cp command (1 Reply)
Discussion started by: arnoldg
1 Replies

6. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

7. Shell Programming and Scripting

Files overwrite in awk

Hi guys, I checked the knowledge base before posting this question. is there any way by which you can ALWAYS ALLOW file overwrite in AWK?. i.e. an option similar to noclobber in Korn shell. I don't to check for files existence and remove them. (1 Reply)
Discussion started by: Moon Noon
1 Replies

8. Shell Programming and Scripting

This script cut size of files "Lol" change string in files

Basic: find . -type f -name “*.txt” -print | awk '{gsub("Ontem", "AntesdeOntem", $0); print > FILENAME}' *.txt The idea is in folder /home/myapontamentos I have some files and i need to change in all them the word "ontem" to "antesdeontem". But bigger files are cut (size i mean)... (4 Replies)
Discussion started by: single
4 Replies

9. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies

10. UNIX for Dummies Questions & Answers

Overwrite

if i want to pipe output to a file, say, cat abc.dat > abc.txt, how do i make it replace the existing file? (9 Replies)
Discussion started by: Duckman
9 Replies
Login or Register to Ask a Question