progress information via pv for directory copy

28,572

Solution 1

Use rsync --progress [SRC] [DST]

Do check the man rsync page because it has a lot of very useful options. -a for archive is a good start, but it depends on your exact requirements.

Copying through a pipe will unnecessarily slow down the copy process, especially if it is files based.

Solution 2

You could use tar or pax or cpio:

mkdir -p dst &&
  (cd src && tar cf - .) | pv -trb | (cd dst && tar xpf -)

Solution 3

Here are some commands to copy directories with progress information.


If there are many small files:

cp -av sourcedir targetdir | pv -l -s filecount > logfile

This will report progress based on number of files that are copied.

You can redirect to /dev/null if you don't need logfile.

Use the following command to get filecount:

find sourcedir | wc -l

If there are few huge files:

tar c sourcedir | pv -s size | tar x -C targetdir

This will report progress based on bytes that are copied.

targetdir must exist.

Use the following command to get size:

du -sh sourcedir

If you want to use rsync:

rsync -ai sourcedir/ targetdir/ | pv -l -s filecount > logfile

Get filecount as shown above.

If you are copying on the same system then rsync -a is practically the same as cp -a . The advantages of rsync is when you are copying over the network or if you are updating (or comparing) a previous copy.

See here for more details:

Solution 4

Tar.

tar -cf - /var/log/ | pv | tar -C . -x

Example:

# tar -cf - /var/log/ | pv | tar -C . -x
tar: Removing leading `/' from member names
58MB 0:00:05 [ 2.2MB/s] [                   <=> 

Solution 5

I made a progress bar for rsync (in a wrapper):

rsyncy -a FROM/ TO

It looks like this:

rsyncy

More info on GitHub, install with pip3 install --user rsyncy

Share:
28,572

Related videos on Youtube

F.P
Author by

F.P

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.

Updated on September 18, 2022

Comments

  • F.P
    F.P almost 2 years

    I need to copy a very large directory (talking in terabytes here) and want to monitor the progress.

    I found that pv is a nice utility, but how can I use it for copying directories recursively? (pv src > dst doesn't work because they are directories)

    PS: Running on OS X Mountain Lion, pv was installed from Mac Ports

  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    Why would copying through a pipe slow down the process? The buffering provided by pipes and the multi-tasking associated with pipes is likely to help matters I would say. (and rsync is multi-processes and uses pipes internally IIRC). What's a copy-process that is not file-based?
  • jippie
    jippie over 11 years
    Bash is convenient, but not fast. Especially when piping one command to the other.
  • Zan Lynx
    Zan Lynx over 11 years
    @StephaneChazelas: Because a pipe's buffer is only 8KB. A program like rsync might want to use a much larger internal buffer.
  • peterph
    peterph over 10 years
    Additionally, you may add a v to the tar command to see the file names.
  • Tak
    Tak over 6 years
    So this will zip the source folder before doing the copy, right? My folder is 5 TB in size, so I'm not sure if this is the best way to do it? I really like using pv as it shows a really good progress status, any advice please?
  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    @Tak, no it doesn't zip anything as there's no compression. tar cf - outputs a stream containing the files and their metadata, which is fed through a pipe to pv which feeds it untouched to the other tar while printing a progress indication, and the other tar extracts the data in that stream. All three commands run concurrently and process data as it comes, there is nothing stored on disk, there is nothing stored in memory other than the pipe buffers and the commands internal buffers which just amounts to a few kilobytes.
  • Tak
    Tak over 6 years
    @StéphaneChazelas Thanks for the comment. I've tried it and it's working, the only thing is that the progress bar doesn't show the expected percentage it keeps going from left to right. Any idea why? I used pv with this parameters -petra which usually shows the progress bar where the bar kept getting filled until it finishes. I've upvoted your answer as well.
  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    @Tak, pv has no way to know how much data there is to transfer. If you have an estimate of that amount, you can pass it to it with the -s option (GNU du -sb src would give you a good approximation if the files are large enough)
  • benomatis
    benomatis about 4 years
    Note that --progress will only show you the per-file progress of each single file, not the overall process of a potentially larger file copy process of several files / folders.