How to use the command `pv` (with examples)

How to use the command `pv` (with examples)

The pv command is used to monitor the progress of data through a pipe. It allows you to view the progress, speed, and amount of data flowing between different commands or processes.

Use case 1: Print the contents of the file and display a progress bar

Code:

pv path/to/file

Motivation: The pv command can be used to display a progress bar while printing the contents of a file. This can be helpful when you want to monitor the progress of a file transfer or any other operation involving reading a file.

Explanation: The pv command is followed by the path to the file you want to print. It will display a progress bar indicating the progress of the operation.

Example output:

15.6MB 0:00:10 [1.34MB/s] [====================>] 100%

Use case 2: Measure the speed and amount of data flow between pipes

Code:

command1 | pv --size expected_amount_of_data_for_eta | command2

Motivation: In some cases, you may want to measure the speed and amount of data flowing between different commands or processes. The pv command can be used to monitor the progress and provide you with an estimate of the time remaining for a specific amount of data.

Explanation: The pv command is used in the pipeline between command1 and command2. The --size option is provided to specify the expected amount of data to be processed. This allows pv to estimate the time remaining. It will display the progress and estimated time remaining for the data flow between the commands.

Example output:

10.5MB 0:00:07 [1.45MB/s] [====================>] 100% ETA 0:00:02

Use case 3: Filter a file, see both progress and amount of output data

Code:

pv -cN in big_text_file | grep pattern | pv -cN out > filtered_file

Motivation: When filtering a large file, it can be useful to see the progress and the amount of output data being generated. The pv command can be used to display the progress and provide insights into the amount of data being filtered.

Explanation: The pv command is used twice in this pipeline. The -cN option is provided to name the input and output streams for better understanding. The first pv command monitors the progress of reading the big_text_file, while the second pv command monitors the progress and amount of data being written to the filtered_file.

Example output:

857MB 0:01:23 [10.3MB/s] [========>           ] 50% ETA 0:01:20  in: 2.41MB/s  out: 5.27MB/s

Use case 4: Attach to an already running process and see its file reading progress

Code:

pv -d PID

Motivation: Sometimes it is necessary to attach to an already running process and monitor its progress, especially when it involves reading files. The pv command can be used to attach to a process and view its file reading progress.

Explanation: The pv command is used with the -d option followed by the process ID (PID) to attach to a running process. It will display the progress of the file reading operation performed by the process.

Example output:

123MB 0:00:25 [4.92MB/s] [========>           ] 50% ETA 0:01:12

Use case 5: Read an erroneous file, skip errors as dd conv=sync,noerror would

Code:

pv -EE path/to/faulty_media > image.img

Motivation: When encountering a faulty media or file with errors, it can be useful to read the file and skip any encountered errors. The pv command can be combined with the -EE option to handle erroneous files and skip errors as dd conv=sync,noerror would.

Explanation: The pv command is used with the -EE option followed by the path to the faulty media/file. It will read the file and skip any encountered errors, similar to the behavior of dd conv=sync,noerror.

Example output: (No output will be shown if there are no errors encountered)

Use case 6: Stop reading after reading specified amount of data, rate limit to 1K/s

Code:

pv -L 1K --stop-at --size maximum_file_size_to_be_read

Motivation: In certain scenarios, it may be necessary to limit the rate at which data is being read and stop the process after reading a specific amount of data. The pv command can be used to accomplish this by setting a rate limit and specifying the maximum file size to be read.

Explanation: The pv command is used with the -L option to specify the rate limit. In this example, the rate limit is set to 1K/s. The --stop-at option followed by the --size option is used to indicate the maximum file size to be read. It will control the rate at which data is read and automatically stop the process after reaching the specified file size.

Example output: (Process will stop after reading the specified amount of data without any additional output)

Conclusion:

The pv command proves to be a versatile tool for monitoring the progress of data through pipes. It allows you to view progress bars, measure speed and amount of data flow, filter files, monitor running processes, handle erroneous files, and control data rate with ease. By utilizing the different use cases of the pv command, you can gain valuable insights into the progress and performance of your command line operations.

Related Posts

How to use the command 'ntpdate' (with examples)

How to use the command 'ntpdate' (with examples)

The ’ntpdate’ command is used to synchronize and set the date and time via NTP (Network Time Protocol).

Read More
How to use the command chafa (with examples)

How to use the command chafa (with examples)

Chafa is a command-line tool that allows you to print images directly in the terminal.

Read More
How to use the command aircrack-ng (with examples)

How to use the command aircrack-ng (with examples)

Aircrack-ng is a command-line tool that is part of the Aircrack-ng network software suite.

Read More