Copying files in Linux is a common task for system administrators, developers, and everyday users. While basic tools like cp can easily copy files, they don’t provide any real-time visual feedback on the progress of the file transfer. This is particularly inconvenient when copying large files or directories where knowing the remaining time and transfer speed can be essential.
In this article, we’ll explore several ways to copy files in Linux while getting real-time visual feedback using tools like rsync, pv, and combining these tools for more advanced use cases.
Basic File Copying in Linux
The cp command is the standard method for copying files in Linux. It’s simple, efficient, but it lacks any progress indicator, which makes it hard to know how long the process will take for larger files.
For example, the following command copy a file1.txt to the /opt directory.
cp file1.txt /opt/
Copying Files with Visual Progress Using rsync
rsync is a handy tool that copies files efficiently and offers an option to display a progress bar. It’s especially useful for large transfers or network-based transfers, as it provides visual progress, file size, transfer speed, and estimated time remaining.
Run the rsync command with the –progress option for copying file with progress.
rsync --progress file1.txt /opt
This command shows the file name, the file size being copied, the percentage completed, the current transfer speed, and the estimated time to completion.
file1.txt
123,456,789 100% 1.23MB/s 0:00:02 (xfr#1, to-chk=0/1)
Using pv (Pipe Viewer) for Real-Time Progress
You can also use pv (Pipe Viewer) command-line tool to display real-time progress bar during file copying process. It can display transfer speed, estimated time remaining, and the amount of data transferred. You can use it with other commands like dd or tar to visualize the progress of file transfers or other data operations.
Using pv and dd:
Let’s use the pv command with dd to copy a file1.txt.
pv file1.txt | dd of=/opt/file1.txt
Here, pv displays the file size, time elapsed, and transfer speed. This method is useful when copying files directly or creating disk images using dd.
123MB 0:00:01 [123MB/s] [================================================>] 100%
Using tar and pv:
You can also use the pv with tar command to copy a directory with real-time progress bar.
tar -cf - my-directory | pv | tar -xf - -C /opt
In this example, pv helps monitor the progress of copying a directory using tar. It’s especially useful for archiving large directories.
345MB 0:00:03 [115MB/s] [================================================>] 100%
Copying Large Files With rsync and pv Combined
You can combine rsync with pv for maximum efficiency and detailed progress reporting. This setup gives you the best of both worlds: rsync for robust file transfer, and pv for real-time progress feedback.
Let’s use the rsync with pv to copy a directory named test-directory:
rsync -av --progress /mnt/test-directory /opt | pv
This combination allows rsync to handle the file transfer while pv provides a unified progress display. You get the advanced features of rsync, such as file comparison and incremental copying, along with a visual progress bar.
sending incremental file list
file1.txt
123,456,789 100% 1.23MB/s 0:00:02 (xfr#1, to-chk=0/1)
file2.txt
234,567,890 100% 2.34MB/s 0:00:01 (xfr#2, to-chk=0/1)
345MB 0:00:04 [86MB/s] [================================================>] 100%
Using scp for Network Transfers With Progress
scp is a standard tool for securely copying files over a network. While it does not offer a built-in progress bar, it can be combined with pv to achieve the same result.
The following command copies a file1.txt to the remote machine with a progress bar.
pv file1.txt | scp -C - root@remote-server-ip:/opt/file1.txt
Output:
file1.txt
345MB 0:00:05 [69MB/s] [================================================>] 100%
Conclusion
In Linux, copying files efficiently with visual progress can greatly enhance the user experience, especially when handling large files or directories. By using tools like rsync, pv, and combinations of commands, you can get real-time feedback on file transfers, ensuring you always know how long your task will take.
Try to explore more advanced combinations to suit your needs on VPS hosting from Atlantic.Net!