check if file finished to copy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if file finished to copy
# 1  
Old 01-28-2011
check if file finished to copy

Hi all,

I have a script that is monitoring a hot folder. This script works fine with one exception when the script is executed while a file is being copied to the hot folder.

What is the easiest method to check if the copy file is completed? I'd like to get the solution in bash Smilie
# 2  
Old 01-28-2011
If possible, after copy rename file and check only that name.
Code:
    cp somefile  tofile.tmp
    mv tofile.tmp tofile
   # check file tofile and when it's - file is ready because mv only edit content of directory, file
   # is same

# 3  
Old 01-28-2011
I found a way through checking the file size
prev_size=-1 # initialize variable
new_size=`/usr/bin/du -sk "$file_i" | awk '{print $1}'` # get current file size

while [ $prev_size != $new_size ] # repeat until these values are the same
do
/bin/sleep 5 # check every 5 seconds
tmp_size=$new_size # move to intermediate value
new_size=`/usr/bin/du -sk "$file_i" | awk '{print $1}'` # get new file size
prev_size=$tmp_size
done
Anybody sees anything wrong with above script? Smilie
# 4  
Old 01-28-2011
Quote:
Originally Posted by gigagigosu
Hi all,

I have a script that is monitoring a hot folder. This script works fine with one exception when the script is executed while a file is being copied to the hot folder.

What is the easiest method to check if the copy file is completed? I'd like to get the solution in bash Smilie
We get asked this all the time. You simply can't tell if a file is done from just looking at it. The upload script has to tell you somehow.

Besides, what if an upload happens while your script is running? It was clear when you started, but halfway through it might screw up. You might have to force it to handle it gracefully.

Can your monitor script be made to skip names beginning with ._? The upload script could create it as ._filename and rename it to filename when its done. This will also help you find broken uploads.
# 5  
Old 01-28-2011
i have no control over incoming files. Different users will connect to this server and copy files into my hot folder.
Since clients and server are within the same subnet, so speed is not an issue, i was focusing on file size... are there any downsides to monitor file size?
# 6  
Old 01-28-2011
I have used sum before so something like this
x=`sum $<file name> |awk '{print $1}'`
So depending on what is needed you can put it to a var then loop and do the sum again and see if it changed.
# 7  
Old 01-28-2011
The "cksum" is safer than the "sum" command in this context. If you have Linux rather than unix there are more different commands.

The best simple design option is to send files under a temporary filename and rename them after successful transmission.
Another technique is to send a suitable "finished" dummy file after a successful main transmission.

A belt-and-braces approach is to send a uniquely numbered batch header file containing filenames and checksums and after the transmission of files prefixed with names containing the unique batch number send a matching trailer file containing the same information. After checking that the header and trailer exist and agree, the process should then check the checksums of all files in the batch before allowing processing of the data.
It is actually easier to implement than it is to describe.
Btw. This is better than BACS.

If you cannot modify the process then testing whether the file is open with the "fuser" command is one option and others have suggested checking for file growth by various methods.
Checking with "fuser" or testing for file growth is no guard against failed transmissions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print 1 file then when finished another file prints beside it?

I have 2 big files over 4Gbs each. I'm looking for a way to print 1 file, then when that file finish printing another file proceeds to print beside it and merge the lines together. How would to cmd or code this? from itertools import izip_longest with open("file1") as textfile1,... (14 Replies)
Discussion started by: bigvito19
14 Replies

2. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

3. Shell Programming and Scripting

Automation, Copy, File Status Check --> Script(s)

All, I have to write a script to do the following requirement. There is a file called BUSINESS_DATE.TXT. This file get updated once the oracle partition created. In Oracle, Partition will be created every day. There is a seperate script scheduled to take care ORACLE partition creation. The... (3 Replies)
Discussion started by: karthi_mrkg
3 Replies

4. Shell Programming and Scripting

Need help to check the copy % from following output

I have a scenario where I need to wait for the copy to be 100% in the following output There can be multiple devices in the copy session so I need to wait till everything becomes 100%, also we can look for the protected tracks t0 become 0 opey # symclone -sid 822 -v -f... (2 Replies)
Discussion started by: rajsan
2 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. Shell Programming and Scripting

check if some file is in copy process, then transfer it

my user copy large files, and it's take 10min for file to be copied to the server (/tmp/user/ files/), if in the meantime start my scheduled script, then it will copy a part of some file to server1 my idea is to check the file size twice in a short period (1-2 seconds) of time, then compare, if... (5 Replies)
Discussion started by: waso
5 Replies

7. Shell Programming and Scripting

Need a script to copy files and check

Hi all, I need a script in ksh: 1: Copy files from directory (A) to directory (B) 2: Check if files that will be copied in directory (A) have never been yet copied to (B) 3: Never copy the last created file of directory (A) This script will run on crontab. Thanks in advance for your... (1 Reply)
Discussion started by: Camaro
1 Replies

8. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

9. Shell Programming and Scripting

How to check a file exist and do a copy of other files

Hi, I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt. Please help. Thanks. -Jason (6 Replies)
Discussion started by: ahjiefreak
6 Replies

10. UNIX for Advanced & Expert Users

How can I tell when an SFTP has finished copying a file?

If I download a file via SFTP how can I tell when the process is completed? Is there a process that I can monitor? Thanks (3 Replies)
Discussion started by: goodmis
3 Replies
Login or Register to Ask a Question