How can I make a progress bar while copying a directory with cp?

54,736

Solution 1

You can also use rsync instead of cp like this:

rsync -Pa source destination

Which will give you a progress bar and estimated time of completion. Very handy.

Solution 2

To show a progress bar while doing a recursive copy of files & folders & subfolders (including links and file attributes), you can use gcp (easily installed in Ubuntu and Debian by running "sudo apt-get install gcp"):

gcp -rf SRC DEST

Here is the typical output while copying a large folder of files:

Copying 1.33 GiB  73% |#####################      | 230.19 M/s ETA:  00:00:07

Notice that it shows just one progress bar for the whole operation, whereas if you want a single progress bar per file, you can use rsync:

rsync -ah --progress SRC DEST

Solution 3

You may have a look at the tool vcp. Thats a simple copy tool with two progress bars: One for the current file, and one for overall.

EDIT

Here is the link to the sources: http://members.iinet.net.au/~lynx/vcp/ Manpage can be found here: http://linux.die.net/man/1/vcp

Most distributions have a package for it.

Solution 4

Here another solution: Use the tool bar

You could invoke it like this:

#!/bin/bash
filesize=$(du -sb ${1} | awk '{ print $1 }')
tar -cf - -C ${1} ./ | bar --size ${filesize} | tar -xf - -C ${2}

You have to go the way over tar, and it will be inaccurate on small files. Also you must take care that the target directory exists. But it is a way.

Solution 5

My preferred option is Advanced Copy, as it uses the original cp source files.

$ wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.21.tar.xz
$ tar xvJf coreutils-8.21.tar.xz
$ cd coreutils-8.21/
$ wget --no-check-certificate wget https://raw.githubusercontent.com/jarun/advcpmv/master/advcpmv-0.8-8.32.patch
$ patch -p1 -i advcpmv-0.8-8.32.patch
$ ./configure
$ make

The new programs are now located in src/cp and src/mv. You may choose to replace your existing commands:

$ sudo cp src/cp /usr/local/bin/cp
$ sudo cp src/mv /usr/local/bin/mv

Then you can use cp as usual, or specify -g to show the progress bar:

$ cp -g src dest
Share:
54,736
octosquidopus
Author by

octosquidopus

Updated on July 22, 2022

Comments

  • octosquidopus
    octosquidopus almost 2 years

    I suppose I could compare the number of files in the source directory to the number of files in the target directory as cp progresses, or perhaps do it with folder size instead? I tried to find examples, but all bash progress bars seem to be written for copying single files. I want to copy a bunch of files (or a directory, if the former is not possible).

  • octosquidopus
    octosquidopus almost 13 years
    I was hoping for something that doesn't require compiling external tools. I just want to see my directory get copied. Is it really so difficult?
  • Thomas Berger
    Thomas Berger almost 13 years
    @Anonymouse added a second answer, maybe this would be an alternative for you. But thats my last idea :(
  • octosquidopus
    octosquidopus almost 13 years
    Early on, I thought about tarring the folder before moving it, but thought it would lack too much in elegance. I was wrong. It works as expected and might actually be a better solution in some cases. Thanks!
  • Adrien
    Adrien over 10 years
    Right, rsync --progress /path/to/origin /path/to/destination is awesome and is available on all systems.
  • Adrien
    Adrien over 10 years
    As said below, rsync is available on all systems (even Mac OS), as opposed to vcp.
  • Nathan
    Nathan over 10 years
    Works when copying a single file as well.
  • KevinHJ
    KevinHJ over 8 years
    Happen to know the speed comparison between rsync and cp?
  • SteveLambert
    SteveLambert over 8 years
    @Allasso Tough to say because there's so many options and situations which affect it. It can be slower for certain sets files, especially with a small CPU, because it's analyzing/syncing not copying, and encrypting files (when over a network, iirc). For example see superuser.com/questions/109780/how-to-speed-up-rsync or superuser.com/questions/153176/…
  • socketpair
    socketpair over 7 years
    gcp is Python-based. PLEASE DO NOT USE IN PRODUCTION. I did not check, but it seems does not support hardlinks, special attributes and so on .
  • Setaa
    Setaa over 7 years
    The members.iinet.net.au/~lynx/vcp link is broken. There seems to be a current fork at github.com/gdm85/curses-vcp with a last commit of May 29, 2016. I was able to compile it just fine on Fedora 23. An independent vcp with a similar concept is at github.com/lynix/vcp but I have not tried it.
  • Setaa
    Setaa over 7 years
    The link to the patch at zwicke.org is broken but it's on github at github.com/atdt/advcpmv. Perhaps someone with more skills than I will update @elboletaire's steps.
  • M.Moro
    M.Moro over 6 years
    gcp doesn't work in a remote terminal (for example via ssh).
  • fuujuhi
    fuujuhi over 3 years
    If copying several files, consider using gcp or advcpmv, with more useful progress bar.
  • fuujuhi
    fuujuhi over 3 years
    @M.Moro gcp through ssh works for me with: dbus-launch gcp -pR src dst
  • fuujuhi
    fuujuhi over 3 years
    This should be selected answer. Better progress bar than rsync if copying several files. Better than gcp because reusing standard cp code from coreutils. Advantage of gcp is that it is distributed as a standard package.
  • shrewmouse
    shrewmouse over 3 years
    pv is not an option because it won't save permissions or any other file attributes. pv source > dest will give you a nice progress bar but the file dest is not going to be what you want.
  • alexkubica
    alexkubica over 3 years
    how do i use it recursively for directories? i tried cpv -r src dst but it prints too few args
  • dawid
    dawid over 3 years
    Would be worth mentioning that you need to use -g option with this patched cp in order to get the progress bar.
  • jemand771
    jemand771 almost 3 years
    gcp immediately crashed when executing the command above on a raspberry pi 4. not sure why
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.