tldr-pages / tldr

📚 Collaborative cheatsheets for console commands
https://tldr.sh
Other
50.3k stars 4.11k forks source link

Page request: tqdm #12398

Open AndyDralle opened 6 months ago

AndyDralle commented 6 months ago

Command description

Tqdm can be used to obtain a progress bar for shell commands.

Command details

No response

Documentation

https://tqdm.github.io/docs/cli

Platform

Common

VCS repository link (e.g. GitHub, GitLab)

https://github.com/tqdm/tqdm

Additional information

No response

osbm commented 1 month ago

It is very hard for me to find some useful and short usage examples but I just want to leave this here. A false progress bar that takes 10 seconds to run and shows 100 steps.

You can play around with amount of time and tqdm labels

$ for i in {1..100}; do echo $i && sleep 0.1; done | tqdm --total 100 --update_to --null
100%|████████████████████████████████████████████████████████████████| 100/100 [00:10<00:00,  9.97it/s]

Or there could be some elaborate looping that reruns some command until some condition is met so that we would see how many tries it took (here the condition is RANDOM variable being less than 6000):

#!/bin/bash
function random_check {
  while :; do
    RANDOM_NUMBER=$RANDOM
    if [ "$RANDOM_NUMBER" -lt 6000 ]; then
      echo 1
      break
    fi
    echo 1
    sleep 1
  done
}

# Use the function and pipe its output to tqdm for progress bar
random_check | tqdm --null

And the output is

6it [00:04,  1.21it/s]

I am sure you some of you guys can figure out more beautiful and useful examples than can be used when searching a file or unarchiving or moving files around.