59

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).

codeforester
  • 39,467
  • 16
  • 112
  • 140
octosquidopus
  • 831
  • 3
  • 8
  • 9
  • 11
    You've hit [Bash FAQ # 44](http://mywiki.wooledge.org/BashFAQ/044). Take a look and see if any of the suggestions there are useful. – jw013 Aug 20 '11 at 00:47

11 Answers11

77

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.

bersanri
  • 107
  • 5
SteveLambert
  • 919
  • 7
  • 11
  • 7
    Right, `rsync --progress /path/to/origin /path/to/destination` is awesome and is available on all systems. – Adrien Nov 10 '13 at 18:32
  • 1
    Works when copying a single file as well. – Nathan Jan 15 '14 at 19:04
  • 2
    Happen to know the speed comparison between rsync and cp? – KevinHJ Feb 01 '16 at 21:16
  • @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 https://superuser.com/questions/109780/how-to-speed-up-rsync or https://superuser.com/questions/153176/how-to-rsync-a-large-file-with-as-little-cpu-and-bandwidth-expense-as-possible/1020737#1020737 – SteveLambert Feb 15 '16 at 17:48
  • If copying several files, consider using `gcp` or `advcpmv`, with more useful progress bar. – fuujuhi Dec 05 '20 at 18:52
26

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
Shervin Emami
  • 2,695
  • 25
  • 17
6

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.

mcandre
  • 22,868
  • 20
  • 88
  • 147
Thomas Berger
  • 1,860
  • 13
  • 26
  • 1
    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? – octosquidopus Aug 20 '11 at 00:21
  • @Anonymouse added a second answer, maybe this would be an alternative for you. But thats my last idea :( – Thomas Berger Aug 20 '11 at 00:48
  • 1
    As said below, rsync is available on all systems (even Mac OS), as opposed to vcp. – Adrien Nov 10 '13 at 18:32
  • 1
    The http://members.iinet.net.au/~lynx/vcp/ link is broken. There seems to be a current fork at https://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 https://github.com/lynix/vcp but I have not tried it. – Setaa Feb 08 '17 at 21:14
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.

Thomas Berger
  • 1,860
  • 13
  • 26
  • 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! – octosquidopus Aug 20 '11 at 01:49
3

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
elboletaire
  • 5,180
  • 2
  • 36
  • 46
  • The link to the patch at zwicke.org is broken but it's on github at https://github.com/atdt/advcpmv. Perhaps someone with more skills than I will update @elboletaire's steps. – Setaa Feb 08 '17 at 21:43
  • 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. – fuujuhi Dec 05 '20 at 18:58
  • 1
    Would be worth mentioning that you need to use `-g` option with this patched `cp` in order to get the progress bar. – dawid Mar 04 '21 at 11:43
2

A simple unix way is to go to the destination directory and do watch -n 5 du -s . Perhaps make it more pretty by showing as a bar . This can help in environments where you have just the standard unix utils and no scope of installing additional files . du-sh is the key , watch is to just do every 5 seconds. Pros : Works on any unix system Cons : No Progress Bar

Nishant
  • 20,354
  • 18
  • 69
  • 101
2

To add another option, you can use cpv. It uses pv to imitate the usage of cp.

It works like pv but you can use it to recursively copy directories

enter image description here

You can get it here

nachoparker
  • 1,678
  • 18
  • 14
0

How about something like

find . -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /DEST/$(dirname {})

It finds all the files in the current directory, pipes that through PV while giving PV an estimated size so the progress meter works and then piping that to a CP command with the --parents flag so the DEST path matches the SRC path.

One problem I have yet to overcome is that if you issue this command

find /home/user/test -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /www/test/$(dirname {})

the destination path becomes /www/test/home/user/test/....FILES... and I am unsure how to tell the command to get rid of the '/home/user/test' part. That why I have to run it from inside the SRC directory.

Ken Brill
  • 21
  • 3
0

Check the source code for progress_bar in the below git repository of mine

https://github.com/Kiran-Bose/supreme

Also try custom bash script package supreme to verify how progress bar work with cp and mv comands

Functionality overview

(1)Open Apps ----Firefox ----Calculator ----Settings

(2)Manage Files ----Search ----Navigate ----Quick access

            |----Select File(s)
            |----Inverse Selection
            |----Make directory
            |----Make file
                                          |----Open
                                          |----Copy
                                          |----Move
                                          |----Delete
                                          |----Rename
                                          |----Send to Device
                                          |----Properties

(3)Manage Phone ----Move/Copy from phone ----Move/Copy to phone ----Sync folders

(4)Manage USB ----Move/Copy from USB ----Move/Copy to USB

KIRAN B
  • 1
  • 2
0

There is command progress, https://github.com/Xfennec/progress, coreutils progress viewer.

Just run progress in another terminal to see the copy/move progress. For continuous monitoring use -M flag.

sharad gor
  • 156
  • 1
  • 3
  • 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](/help/how-to-answer). – Community Nov 17 '21 at 05:03
0

There's a tool pv to do this exact thing: http://www.ivarch.com/programs/pv.shtml

There's a ubuntu version in apt

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • 1
    `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. – shrewmouse Dec 09 '20 at 22:54