Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-08-2008, 06:38 AM   #1
Registered: May 2008
Posts: 155

Rep: Reputation: 30
how to copy a file to a directory ,where file and dir are sent as args to a function?

Hi all,

I wanted to know how i can copy a file to a directory and then verify if that file is completely copied or not?
Now the issues here is that the dir and the source file are to be sent as arguments to a function( this function should actually copy the files to a dir, then check if its completely copied)

I dont want to use the

if [ -e <filename>]
option then figure out if the file exists in the dir or not....
Reason being i am giving a progress bar that actually calculates the % of completion....
I have written scripts that do the job ,but a pre-requisite is that a file already needs to exist in the dir, then to this file i can copy my source file.

But my idea is to have no files in the targeted dir ,and when the cp is done the source file should exist in the destination dir!!!!

please help!!
Old 06-08-2008, 07:00 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Using shell script, right?

All you really need to do is check the return status of cp - if there is a problem, it will return non-0. I do a little paranoid checking of the second arg too in this example (not tested)

mycp () {
    # arg 1 is filename, arg #2 is target directory
    # verify $2 is actually a directory, just for paranoia
    if [ ! -d "$2" ]; then
        echo "Error in mycp... $2 is not a directory" 1>&2
        return 1

    cp "$1" "$2"
    if [ "$status" -ne 0 ]; then
        echo "Error in mycp copying file $1 to directory $2 returned status $status"
        return $status
        return 0
If you want to be uber-paranoid in your verification, you could checksum the file before copying, and after, and compare the checksums.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
alsamixer: function snd_ctl_open failed for default: No such file or directory agts Linux - Newbie 16 04-01-2011 08:12 AM
Why won't it let me copy or move a file from one directory to another? dewjunkie Linux - Newbie 4 01-23-2008 12:31 AM
copy file to another directory johnrage Linux - Newbie 1 02-25-2006 01:30 AM
Please help, how do I copy one file to another directory lexington Linux - Newbie 4 04-04-2004 10:17 AM
copy 1 file to multiple dir script carpman Linux - General 14 02-07-2002 09:29 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration