Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Advanced Copy Command with Progress Bar While Copying/Moving Files in Linux
As a Linux user, you may have found yourself in situations where you need to copy or move large files or directories from one location to another. This process can take some time, and it can be challenging to monitor the progress of copy or move operations.
Fortunately, Linux provides several built-in commands that can help you copy or move files efficiently while also monitoring the operation's progress. In this article, we will discuss advanced copy commands with progress bars for copying/moving files in Linux.
What is Advanced Copy Command?
The Advanced Copy (or cp) command is a built-in Linux command used to copy files or directories from one location to another. This command comes pre-installed on most Linux systems, and it is widely used by system administrators, developers, and Linux enthusiasts.
The cp command provides several options that allow you to customize the copy operation, such as preserving file attributes, recursive copying, and more. However, by default, the cp command does not provide any progress bar while copying or moving files.
Why do we need a Progress Bar?
When you are copying or moving large files or directories, it can take a considerable amount of time, and it may be challenging to determine how much time is left for the operation to complete. This is where a progress bar becomes useful.
A progress bar is a visual representation of the copy or move operation's progress. It shows the percentage of the operation that has been completed and estimates how much time is left for the operation to finish. This can be extremely helpful when you need to monitor the progress of lengthy file operations.
Using "pv" Command for Progress Bar
The pv (pipe viewer) command is a Linux utility that monitors the progress of data through a pipeline. This command can be used in conjunction with other Linux commands, such as cp, to provide a progress bar while copying files.
To use pv with file copying operations, you can use it in several ways. Here's an example for copying a single file
$ pv /path/to/source/file > /path/to/destination/file
For copying directories with progress monitoring
$ tar cf - /path/to/source | pv -s $(du -sb /path/to/source | awk '{print $1}') | tar xf - -C /path/to/destination
Let's break down the command options
pvdisplays the progress bar with transfer rate and ETA-sspecifies the size of the data being transferred for accurate progress calculation$(du -sb /path/to/source | awk '{print $1}')calculates the total size in bytes
Using "rsync" Command for Progress Bar
The rsync command is a powerful utility for synchronizing files between locations and includes built-in progress display options. It's often preferred for large file transfers due to its efficiency and resume capability.
To use rsync with a progress bar
$ rsync -aP /path/to/source/ /path/to/destination/
For more detailed progress information
$ rsync -av --progress /path/to/source/ /path/to/destination/
Let's break down the command options
-aenables archive mode, preserving file attributes, permissions, timestamps-Pcombines--partialand--progressoptions--progressshows detailed transfer progress, rate, and ETA-venables verbose output showing files being transferred
Using "gcp" Command (GNU cp with Progress)
The gcp command is an enhanced version of cp that includes a built-in progress bar. It needs to be installed separately on most systems.
To install gcp on Ubuntu/Debian
$ sudo apt install gcp
Using gcp with progress bar
$ gcp -r /path/to/source /path/to/destination
Comparison of Progress Bar Methods
| Method | Pros | Cons | Best Use Case |
|---|---|---|---|
pv |
Flexible, works with pipes | Requires additional setup | Single files, custom pipelines |
rsync |
Built-in progress, resume capability | Slightly slower for local copies | Large directories, network transfers |
gcp |
Drop-in replacement for cp | Requires separate installation | Simple file copying with progress |
Additional Tools and Methods
Several other utilities can provide progress bars for file operations
"progress" command Monitors running processes and displays progress for
cp,mv,dd, and other commands"cv" (Coreutils Viewer) Shows progress for core utilities like
cp,mv, andddwhile they're running"bar" command A simple progress bar utility that works with pipes
Example using the progress command to monitor an existing copy operation
$ progress -m
Conclusion
Linux provides several excellent options for copying and moving files with progress bars, each suited to different scenarios. The rsync command offers the most comprehensive solution with built-in progress display, while pv provides flexibility for custom operations. Choose the method that best fits your specific needs and workflow requirements.
