Skip to content
Home Ā» How to Display the Progress Bar in Linux Commands?

How to Display the Progress Bar in Linux Commands?

  • by

The previous article has explained how to display progress in a process, but unfortunately, this application is limited to displaying the copy and move process. This article will explain how to display a progress that not only displays the copy and move process, but can also display the backup process and restore a database.

 

Problem

How to display the progress bar in Linux commands?

 

Solution

By default, Linux commands do not display a progress bar, so you don’t know when the process is complete, like in the image below:

Copy the file without using pv application

 

Therefore, Andrew Wood, An Experienced Unix Sysadmin, created an application to display a progress bar named Pipe Viewer or PV. To install the application, use the command below:

RockyLinux/AlmaLinux/CentOS

yum install epel-release
yum install pv

 

Ubuntu/Debian

sudo apt update
sudo apt install pv

 

OpenSUSE

sudo zypper install pv 

 

If you want to install pv applications in addition to the operating system shown above, you can go to this page. Here are some methods when using the pv application:

A. Copy

1. Copy the file

Use the format below to copy the file:

pv file1 > /folder/filename

So, if you want to copy an instances.sql to the /tmp folder, use the command below:

pv instances.sql > /tmp/instance.sql
Copy the file using pv application

 

2. Copy more than one file

If you want to copy more than one file to the folder, use the format below:

tar cf - file1 file2 file3 | pv | tar xf - -C /folder

 

Here is the command to copy more than one file to the /tmp folder

tar cf - babel.sql babel.sql.gz babel.sql.tar.gz  | pv | tar xf - -C /tmp
Copy more than one file in pv application

 

3. Copy the folder

If you want to copy the folder, use the format below:

tar cf - folder_name/ | pv | tar xf - -C /folder

 

If you want to copy the example folder to the /tmp folder, use the command below:

tar cf - example/ | pv | tar xf - -C /tmp
Copy some files using pv application

 

4. Copy more than one folder

If you want to copy more than one folder, use the format below:

tar cf - folder1/ folder2/ | pv | tar xf - -C /folder

 

So, if you want to copy more than one folder to the /tmp directory, use the command below:

tar cf - example/ test-project/ | pv | tar xf - -C /tmp
Copy some folders using pv application

B. Move

If you want to use the move command on the PV application, then you can actually use the command to copy number 1, but add the command && rm -rf file1/folder1 behind it.

1. Move the file

So, if you want to move an instances.sql to the /tmp folder, use the command below:

pv instances.sql > /tmp/instance.sql && rm -rf instances.sql 
Move the file in pv application

 

2. Move more than one file

If you want to move some files to the /tmp folder, use the command below:

tar cf - babel.sql babel.sql.gz babel.sql.tar.gz  | pv | tar xf - -C /tmp && rm -rf babel.sql babel.sql.gz babel.sql.tar.gz 
Move more than one file in pv application

 

3. Move the folder

If you want to move a folder to the /tmp folder, use the command below:

tar cf - example/ | pv | tar xf - -C /tmp && rm -rf example/
Move the folder in pv application

 

4. Move more than one folder

If you want to move some folders to the /tmp folder, use the command below:

tar cf - example/ test-project/ | pv | tar xf - -C /tmp && rm -rf example/ test-project/ 
Move more than one folder in pv application

 

C. Compress

1. Using gz
Use the format below to run the gz command in pv application:

pv filename | gzip > filename.gz

For example, you want to compress babel.sql using gz, so use the command below:

pv babel.sql | gzip > babel.sql.gz
Compress the file using gz in pv application


2. Using tar

Use the format below to run the tar command in pv application:

tar cf - filename | pv | gzip > filename.tar.gz

For example, you want to compress babel.sql using tar, use the command below:

tar cf - babel.sql | pv | gzip > babel.sql.tar.gz
Compress the file using tar in pv application


3. Using bz2

Use the format below to run the tar command in pv application:

pv filename | bzip2 > filename.bz2

For example, you want to compress babel.sql using bz2, use the command below:

pv babel.sql | bzip2 > babel.sql.bz2
Compress the file using bzip2 in pv application


4. Using zip

Use the format below to run the tar command in pv application:

pv filename | zip filename.zip -q -

For example, you want to compress babel.sql using bz2, use the command below:

pv babel.sql | zip babel.sql.zip -q -
Compress the file using zip in pv application

D. Extract

1. Using gunzip
Use the format below to extract the gz compression in pv application:

pv filename.gz | gunzip > filename

For example, you want to extract babel.sql.gz, so use the command below:

pv babel.sql.gz | gunzip > babel.sql
Extract the file using gunzip in pv application


2. Using tar.gz

Use the format below to extract the gz compression in pv application:

pv filename.tar.gz | tar zxf -

For example, you want to extract babel.sql.tar.gz, so use the command below:

pv babel.sql.tar.gz | tar xzf -
Extract the file using tar in pv application


3. Using bunzip2

Use the format below to extract the bz2 compression in pv application:

pv filename.sql.bz2 | bunzip2 > filename.sql

For example, you want to extract babel.sql.bz2, so use the command below:

pv babel.sql.bz2 | bunzip2 > babel.sql
Extract using bunzip2 in pv application


4. Using unzip

Use the format below to extract the zip compression in pv application:

unzip filename.zip | pv

For example, you want to extract babel.sql.zip, so use the command below:

unzip babel.sql.zip | pv
Extract the file in pv application

E. Backup DB

If you use MariaDB, you can use the commands below:
1. Without compressing the databaseĀ 
Use the format below to back up the database without compression in pv application:

mariadb-dump -u username -p dbname | pv > dbname.sql

So, use the command below to back up the database without compression in pv application:

mariadb-dump -uroot -p babel | pv > babel.sql
Backup database without compression in pv application


2. Back up the database using gz

Use the format below to back up the database using gz compression in pv application:

mariadb-dump -u username -p dbname | pv | gzip > dbname.sql.gz

Use the command below to back up the database using gz compression in pv application:

mariadb-dump -uroot -p babel | pv | gzip > babel.sql.gz
Backup using gz compression in pv application


3. Backup the database using bz2

Use the format below to back up the database using bz2 compression in pv application:

mariadb-dump -u username -p dbname | pv | bzip2 > dbname.sql.bz2

Use the command below to back up the database using bz2 compression in pv application:

mysqldump -uroot -p babel | pv | bzip2 > babel.sql.bz2
Backup using bz2 compression in pv application

 

F. Restore DB

If you use MariaDB, you can use the commands below:
1. Restore the database without compression
Use the format below to restore the database without compression in pv application:

pv backup_file.sql | mariadb -u username -p

So, use the command below to restore the database without compression in pv application:

pv babel.sql | mariadb -uroot -p
Restore the database without compression in pv application


2. Restore the database with gz compression

Use the format below to restore the database using gz compression in pv application:

pv backup_file.gz | gunzip | mysql -u username -p

Use the command below to restore the database using gz compression in pv application:

pv babel.sql.gz | gunzip | mariadb -uroot -p 
Restore the database using gz in pv application


3. Restore the database with bz2 compression

Use the format below to restore the database using bz2 compression in pv application:

pv backup_file.sql.bz2 | mariadb -u username -p

Use the command below to restore the database using bz2 compression in pv application:

pv babel.sql.bz2 | bunzip2 | mariadb -uroot -p babel
Restore the database using bz2 in pv application

Note

If you are using a MySQL database, then you can use the commands in point E to back up the database and the commands in point F to restore the database by changing the mariadb-dump command to mysqldump and changing the mariadb command to mysql.

 

References

superuser.com
howtogeek.com
dba.stackexchange.com
tecmint.com
catonmat.net

image_pdfimage_print
Visited 13 times, 1 visit(s) today
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *