<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Check progress of file copying in linux with cp command

Published on
15,421 Points
9,221 Views
2 Endorsements
Last Modified:
Approved
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
}

Open 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

Open in new window


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

Open in new window


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

Open 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
2
Comment
Author:boon86
2 Comments
LVL 1

Expert Comment

by:asterinux
wow...nice information above sir.. i really appreciate people doing things just for helping others to easy their job. great tutorial sir.. thanks....
0
LVL 1

Expert Comment

by:ganesh4282
It is really nice and useful.. Thanks dude..
0

Featured Post

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Join & Write a Comment

Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month