Copying a Directory with Progress Indicator
To duplicate a directory and its contents to a new location, use the cp command with the recursive flag. This ensures all files and subdirectories within the source folder are copied to the destination.
The pv (pipe viewer) utility provides a visual progress bar for data transfers. When combined with cp, it displays real-time progress during the copy operation.
# Basic recursive copy operation
cp -r /home/user/documents /mnt/backup/documents
# Copy with progress bar using pv
cp -r /home/user/documents /mnt/backup/documents | pv -lep -s $(du -sb /home/user/documents | awk '{print $1}') >/dev/null
Key parameters explained:
-lep - Displays estimated time remaining during transfer -s - Shows total data size being transferred du -sb - Calculates the exact byte count of the source directory for accurate progress tracking
Moving a Directory with Progress Indicator
The mv commmand relocates files and directories rather than duplicating them. When the destination already contains files with the same names, the system will prompt for confirmation before overwriting.
# Basic move operation
mv /home/user/projects /var/www/projects
# Move with progress bar
mv /home/user/projects /var/www/projects | pv -lep -s $(du -sb /home/user/projects | awk '{print $1}') >/dev/null
Troubleshooting
If pv is not available on your system, you may encounter the following error:
bash: pv: command not found
This typically occurs on older Linux distributions. Install pv using your package manager:
# For Debian-based systems (Ubuntu, Linux Mint)
sudo apt-get install pv
# For Red Hat-based systems (CentOS, Fedora)
sudo yum install pv
Note: The pv command is included in most modern Linux distributions but may require manual installation on older systems. Command behavior and output formatting may vary slight between different desktop environments and shell configurations. Adjust paths and parameters according to your specific environment.