Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PV to Count lines and show total lines rather then total bytes in the pipe

I am trying to make a little script that shows me total storage used on disk by a directory. To figure it out I am using the du command. However, in order to give some feedback to the user while DU works away on a really large directory, I would like to run the output through a pipe and show the line count, so the user can also get an idea about how many folders and files there are in the directory. Here is my code:

du -ah | pv -l | tail -n 1 | sed 's/\.$//'

However, though the pv commands uses lines, it still shows total data through the pipe in Kilobytes rather then lines. Is there a way to show it total number of lines piped through, rather then bytes. Maybe a different command?

Thanks!

like image 249
Bogdan Avatar asked Mar 22 '23 11:03

Bogdan


2 Answers

du comes with the -s flag to only display the total, so just do e.g. this instead:

$ du -sh /tmp | cut -f1
4.9M

Regarding pv:

However, though the pv commands uses lines, it still shows total data through the pipe in Kilobytes rather then lines.

Are you sure?

$ find /tmp/ | pv >/dev/null
44.6kiB 0:00:00 [3.19MiB/s] [   <=>  
^^^^^^^

$ find /tmp/ | pv -l >/dev/null
1.24k 0:00:00 [86.5k/s] [   <=>  
^^^^^

$ find /tmp/ | wc -l
1237
^^^^

Looks like lines to me (as in, it's working as expected)?

like image 168
Adrian Frühwirth Avatar answered Mar 25 '23 02:03

Adrian Frühwirth


I wanted to monitor MP3 conversion of a bulk of files. This helped me, hope helps researchers on this issue:

$ find . -type f | grep "wav$" | 
  while read f; do
  ffmpeg -loglevel panic -n -i "$f" -codec:a libmp3lame -qscale:a 1 "${f%.wav}.mp3";
  echo "OK."; 
  done | 
  pv -l -s `find . -type f | grep "wav$" | wc -l` > /dev/null

It is a one-liner.

Also archiving for myself

like image 27
JSBach Avatar answered Mar 25 '23 02:03

JSBach