LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cp progress bar (https://www.linuxquestions.org/questions/linux-general-1/cp-progress-bar-407381/)

edwardsiow 01-24-2006 09:10 AM

cp progress bar
 
Hi,
I have to copy large files from a CD to the hard disk.

I want to use a progress bar to display the status of
copying the files.

Any one has ideas about implementing this in shell script.

hellodio 01-24-2006 09:19 AM

Quote:

Originally Posted by edwardsiow
Hi,
I have to copy large files from a CD to the hard disk.

I want to use a progress bar to display the status of
copying the files.

Any one has ideas about implementing this in shell script.

One may use a GUI based file manager for that. I.e. Nautilus (Gnome) or XFE (http://roland65.free.fr/xfe/), etc.

edwardsiow 01-24-2006 09:34 AM

thanks for your advice

i have found a sample code in this forum

Code:

#!/bin/bash

files=`ls -l | wc -l`
copied=1
clear
echo "                    Please wait, copy in progress"
while [ $files -ge $copied ] ;do
  pct=$((100 * copied / $files))
  copied=$((copied + 1))
  echo -en ".$pct%\b\b\b"  ## you can use # or anything instead of dots
  sleep 1
done
  echo -e "\n"
  echo "                    The operation is complete"
  echo -e "\n"

but i don't know how to modify this code so that i can use it to copy large files from a CD to the hard disk and at the same time showing the progress bar.

anyone can help me, please...

homey 01-24-2006 10:13 AM

Here's another sample which maybe abit more usefull.

Code:

#!/bin/bash
# File copy with progress indicators
# Example: ./test original_file destination_file

usage()
{
  echo "Usage: $0 original_file destination_file"
  exit 1;
}

test $# == 2 || usage

echo Preparing to copy
orig_size=$(stat -c %s $1)

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

while [ $orig_size -gt $dest_size ] ; do
  dest_size=$(stat -c %s $2)
  pct=$((( 100 * $dest_size ) / $orig_size ))

if [ $pct -lt 10 ] ; then
  echo -en "#  $pct%\b\b\b\b"
else
  echo -en "#  $pct%\b\b\b\b\b"
fi
sleep 1
done
echo

And here is another....
Code:

#!/bin/bash
# Example: ./test original_file destination_file
usage()
{
  echo "Usage: $0 original_file destination_file"
  exit 1;
}

test $# == 2 || usage
orig_size=$(stat -c %s $1)

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

while [ $orig_size -gt $dest_size ] ; do
  dest_size=$(stat -c %s $2)
  pct=$((( 69 * $dest_size ) / $orig_size ))

    echo -en "\r["
    for j in `seq 1 $pct`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $pct 68`; do
        echo -n "."
    done
    echo -n "] "
    echo -n $((( 100 * $pct ) / 69 ))
    echo -n "%"
done
echo


edwardsiow 01-24-2006 10:44 AM

thanks for your resources..

i am sorry to tell that i am newbie and not so understand the above sample codes..

maybe you will ask me to go back study hard..but i really need to solve this problem urgent and dont have time for me to study. believe or not depend on you...

below code is easy for me to understand:
Code:

#!/bin/bash
umount /mnt/cdrom
mount /mnt/cdrom
mkdir ~/Desktop/CDcontents
cp -R /mnt/cdrom/  ~/Desktop/CDcontents/
eject /mnt/cdrom

but i don't know how to modify n put into the sample codes you provided above..if you don't mind, please give me guidiance..

thanks again..

homey 01-24-2006 12:28 PM

Example: ./test original_file destination_file

This part is the name of my script ------> ./test
To use ./test , I made it execute with the command: chmod +x test
or you could use the method of : sh test

original_file ---------> Is actually a directory in your case.
that is /mnt/cdrom

destination_file --------> Is actually a directory in your case.
~/Desktop/CDcontents/


So, it might look like this...
./test /mnt/cdrom ~/Desktop/CDcontents/

Because it's a directory, you may need to play with the copy statement. Look into man cp for that. Maybe something like.....
cp -R $1 $2 &

edwardsiow 01-24-2006 12:31 PM

Quote:

Originally Posted by homey
Example: ./test original_file destination_file

This part is the name of my script ------> ./test
To use ./test , I made it execute with the command: chmod +x test
or you could use the method of : sh test

original_file ---------> Is actually a directory in your case.
that is /mnt/cdrom

destination_file --------> Is actually a directory in your case.
~/Desktop/CDcontents/


So, it might look like this...
./test /mnt/cdrom ~/Desktop/CDcontents/

Because it's a directory, you may need to play with the copy statement. Look into man cp for that. Maybe something like.....
cp -R $1 $2 &

wow~~thanks a lot!!! let's me try first...i am appreciate your help...

jlliagre 01-24-2006 02:09 PM

Quote:

Originally Posted by edwardsiow
Hi,
I have to copy large files from a CD to the hard disk.

I want to use a progress bar to display the status of
copying the files.

Any one has ideas about implementing this in shell script.

Here is something that look similar, "catenate with progress":

http://www.ex-parrot.com/~chris/software.html

keyvez 09-02-2006 05:03 PM

You can use MC (Midnight Commander). The worlds best console file manager.

To install mc on ubuntu, enable the universe repository from /etc/apt/sources.list by removing the # before the universe repository.

Then simply say
sudo apt-get install mc

and then type mc on the command line to open up the colsole app.

mk6032 03-24-2008 10:58 AM

Quote:

Originally Posted by homey (Post 2065404)
Here's another sample which maybe abit more usefull.

Code:

#!/bin/bash
# File copy with progress indicators
# Example: ./test original_file destination_file

usage()
{
  echo "Usage: $0 original_file destination_file"
  exit 1;
}

test $# == 2 || usage

echo Preparing to copy
orig_size=$(stat -c %s $1)

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

while [ $orig_size -gt $dest_size ] ; do
  dest_size=$(stat -c %s $2)
  pct=$((( 100 * $dest_size ) / $orig_size ))

if [ $pct -lt 10 ] ; then
  echo -en "#  $pct%\b\b\b\b"
else
  echo -en "#  $pct%\b\b\b\b\b"
fi
sleep 1
done
echo

And here is another....
Code:

#!/bin/bash
# Example: ./test original_file destination_file
usage()
{
  echo "Usage: $0 original_file destination_file"
  exit 1;
}

test $# == 2 || usage
orig_size=$(stat -c %s $1)

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

while [ $orig_size -gt $dest_size ] ; do
  dest_size=$(stat -c %s $2)
  pct=$((( 69 * $dest_size ) / $orig_size ))

    echo -en "\r["
    for j in `seq 1 $pct`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $pct 68`; do
        echo -n "."
    done
    echo -n "] "
    echo -n $((( 100 * $pct ) / 69 ))
    echo -n "%"
done
echo


This works well for file->file, but file->directory will result in an endless loop you have to ctrl-c out of:


Code:

[mk6032@linuxquestions]# cp movie.avi /srv/nfs/media/video/
Preparing to copy
/root/cp_progress.sh: line 16: /srv/nfs/media/video/: Is a directory
###########################################################  0%
[mk6032@linuxquestions]#

It needs to run a check on the destination first:
if [ -d $2 ] then ....

michelemase 11-07-2008 03:55 AM

code improved
 
Here it's a little improvement of the script; it should work if the second field is a directory now.


Code:

--- cp_p.org        2008-11-07 10:47:25.934417154 +0100
+++ cp_p2        2008-11-07 10:40:17.674417698 +0100
@@ -8,13 +8,17 @@
 
 test $# == 2 || usage
 orig_size=$(stat -c %s $1)
+if [ -d "$2" ]
+then d="$2/$1"
+else d="$2"
+fi
+>"$d"
 
->$2
 dest_size=0
-cp -f $1 $2 &
+cp -f $1 "$d" &
 
 while [ $orig_size -gt $dest_size ] ; do
-  dest_size=$(stat -c %s $2)
+  dest_size=$(stat -c %s "$d")
    pct=$((( 69 * $dest_size ) / $orig_size ))
 
    echo -en "\r["


adam2508 03-09-2009 02:58 PM

what does:
>"$d"

does?
I understand everything else, i just dont know what ">$d" by itself does.
can someone explain? :) thank you

michelemase 03-10-2009 03:26 AM

>d$
 
It should create an empty destination file; the script also works without it.
Try it!

nonzenze 04-28-2009 11:27 AM

rsync -rv <src> <dst> --progress:

-r for recursive (if you want to copy entire directories)
src for the source file (or wildcards)
dst for the destination
--progress to show a progress bar

Honestly, rsync is only a little slower than cp for local copies and is so much better featured.

treehead 05-07-2009 02:46 PM

pv
 
You can also look at pv (Pipe Viewer), but honestly, for the specific solution you're looking for--copying a tree of files off a CD-ROM with a progress bar--rsync is the best solution, as nonzenze said.

cRaig


All times are GMT -5. The time now is 02:07 PM.