Check progress of file copying in linux with cp command

AID: 7529
  • Status: Published

1550 points

  • Byboon86
  • TypeGeneral
  • Posted on2011-09-11 at 07:15:12
Little introduction about CP:

CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow:

cp /myfoder /pathto/destination/folder/
cp abc.tar.gz /pathto/destination/folder/abc.tar.gz

By default there is no progress bar showed when you copying files using command prompt by typing cp, so in this article I came across a few solutions for a progress bar with little simple tricks to play with.

So, many of you been used cp command on linux ssh to copy files, and folders. When you try copy small files are always easy to go but when comes across large files for example: 50GB, 100GB, 200GB. It usually takes time to copy the files from folder to folder or hard drive to another hard drive, but you want to know the status of copying progress instead of waiting. In SSH there is no progress bar on for cp command, instead of boring waiting here is the shell script that I show below to check your status of files or folder copying progress while copying your files and folders.

The code below has been inspired from a few different examples that can be found out there in the internet. More specifically, you can find examples at : http://chris-lamb.co.uk/2008/01/24/can-you-get-cp-to-give-a-progress-bar-like-wget/ and http://www.linuxquestions.org/questions/linux-general-1/cp-progress-bar-407381/#post2065404. They are not the only examples, or instances of very similar code - not sure where the true origins come from, but very grateful to whoever the original author really is. Anyway, I thought I would present the better methods here to share with you.

Version 1:
Much simple code but nice output like wget progress bar
#!/bin/sh
# SCRIPT:  copyx ver 1.0, 14-9-2011
# PURPOSE: Copies files and shows the progress of copying.
# Usage Example: ./copyx my100gbfiles.tar.gz /path/to/destination/my100gbfiles.tar.gz
copyx()
{
   strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
      | awk '{
        cal += $NF
            if (cal % 10 == 0) {
               percentage = cal / totalsize * 100
               printf "%3d%% [", percentage
               for (i=0;i<=percentage;i++)
                  printf "="
               printf ">"
               for (i=percentage;i<100;i++)
                  printf " "
               printf "]\r"
            }
         }
         END { print "" }' totalsize=$(stat -c '%s' "${1}") cal=0
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window



Version 2:
This code will print incremental of "#" and percentage to indicate the copying progress until
the copy progress is finished.
 
#!/bin/bash
# SCRIPT:  mycopy
# PURPOSE: Copies files and shows the progress of copying.
# Usage Example: ./mycopy my100gbfiles.tar.gz /path/to/destination/my100gbfiles.tar.gz

howtouse()
{
   echo "=========================================================\n"
   echo "Usage Example: $0 my100gbfiles.tar.gz /path/to/destination/my100gbfiles.tar.gz\n"
   echo "=========================================================\n"
   exit 1;
}

test $# == 2 || howtouse
echo "=========================================================\n"
echo "Preparing to copy\n"
echo "=========================================================\n"
original_file_size=$(stat -c %s $1)

>$2
destination_file_size=0
cp -f $1 $2 &

while [ $original_file_size -gt $destination_file_size ] ; do
   destination_file_size=$(stat -c %s $2)
   coyp_percentage=$((( 100 * $destination_file_size ) / $original_file_size ))

if [ $pct -lt 10 ] ; then
   echo -en "# $coyp_percentage%\b\b\b\b"
else
   echo -en "# $coyp_percentage%\b\b\b\b\b"
fi
sleep 1
done
echo
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:

Select allOpen in new window



Demo 1:
root@server:$ ./xcopy /tmp/redhad.iso /usr/local/src
93% [===============================================>       ]
                                    
1:
2:

Select allOpen in new window



Demo 2 :
root@server:$ ./xcopy /tmp/redhad.iso /usr/local/src
######################### 15%
                                    
1:
2:

Select allOpen in new window




This is a nice shell scripting tweak. It give you a progress bar that equivalent to wget  command that you use on linux to download files, you can check the progress bar of wget by typing: wget http://dl.google.com/android/android-sdk_r12-linux_x86.tgz  to see the similarity and differences between them.

Here is the instruction on how to copy my code and use it on your linux shell command prompt:

Step:
1. copy any version of above code.
2. create a file using your favorite editor like vi, pico, nano. File name  example: mycopy, xcopy, copyx
3. paste the code you copied just now and saved it to your favorite editor
4. change the permission of the files you created so that it become executable by typing: chmod +x mycopy
5. run the script by typing : ./mycopy myfiles.tar.gz /path/to/destination/myfiles.tar.gz
6. enjoy and watch the progress bar that you just created.

Hope my code will benefit to someone else that needed it.
Cheers
Asked On
2011-09-11 at 07:15:12ID7529
Tags

cp

,

progressbar

,

copy progress bar

,

linux files copy progressbar

Topic

Linux

Views
905

Comments

Expert Comment

by: asterinux on 2011-10-19 at 19:41:38ID: 32647

wow...nice information above sir.. i really appreciate people doing things just for helping others to easy their job. great tutorial sir.. thanks....

Expert Comment

by: ganesh4282 on 2011-11-10 at 08:11:22ID: 33108

It is really nice and useful.. Thanks dude..

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Linux Experts

  1. arnold

    331,375

    Wizard

    750 points yesterday

    Profile
    Rank: Genius
  2. woolmilkporc

    279,119

    Guru

    2,668 points yesterday

    Profile
    Rank: Genius
  3. farzanj

    132,548

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. KeremE

    109,627

    Master

    0 points yesterday

    Profile
    Rank: Genius
  5. duncan_roe

    105,116

    Master

    0 points yesterday

    Profile
    Rank: Genius
  6. hanccocka

    99,734

    Master

    1,500 points yesterday

    Profile
    Rank: Genius
  7. legolasthehansy

    74,904

    Master

    0 points yesterday

    Profile
    Rank: Guru
  8. xterm

    65,407

    Master

    0 points yesterday

    Profile
    Rank: Sage
  9. Papertrip

    59,708

    Master

    0 points yesterday

    Profile
    Rank: Sage
  10. DaveBaldwin

    55,260

    Master

    0 points yesterday

    Profile
    Rank: Genius
  11. TobiasHolm

    46,700

    0 points yesterday

    Profile
    Rank: Sage
  12. rindi

    43,404

    0 points yesterday

    Profile
    Rank: Savant
  13. ahoffmann

    41,621

    0 points yesterday

    Profile
    Rank: Genius
  14. noci

    40,754

    0 points yesterday

    Profile
    Rank: Genius
  15. gerwinjansen

    38,789

    0 points yesterday

    Profile
    Rank: Sage
  16. Darr247

    38,657

    0 points yesterday

    Profile
    Rank: Genius
  17. chandranjoy

    36,828

    0 points yesterday

    Profile
    Rank: Master
  18. torakeshb

    33,696

    0 points yesterday

    Profile
    Rank: Master
  19. pfrancois

    33,520

    0 points yesterday

    Profile
    Rank: Guru
  20. un1x86

    30,652

    0 points yesterday

    Profile
    Rank: Master
  21. maeltar

    30,550

    0 points yesterday

    Profile
    Rank: Guru
  22. ozo

    24,100

    0 points yesterday

    Profile
    Rank: Savant
  23. jgiordano

    23,700

    0 points yesterday

    Profile
    Rank: Guru
  24. for_yan

    23,600

    1,000 points yesterday

    Profile
    Rank: Genius
  25. bummerlord

    23,300

    0 points yesterday

    Profile
    Rank: Master

Hall Of Fame