Search code examples
bashloopsfor-loopprogress-bartqdm

How to use tqdm in bash for loops?


I have this for loop for example that takes so much time to finish so I want to use tqdm to have a nice progress bar like in python. But I can't find any way to do it?

for f in `find mydir -name *.jpg -print`; do cp $f images/${f//\//_}; done

How can I get a progress bar for this loop?


Solution

  • This is a really old question, but currently the readme for tqdm says that using it as a command in bash works fine, e.g.:

    $ seq 9999999 | tqdm --bytes | wc -l
    75.2MB [00:00, 217MB/s]
    9999999
    
    $ tar -zcf - docs/ | tqdm --bytes --total `du -sb docs/ | cut -f1` \
        > backup.tgz
     32%|██████████▍                      | 8.89G/27.9G [00:42<01:31, 223MB/s]
    

    However, tqdm showing a progress bar is dependent on the command giving it information to build a progress bar from, which cp doesn't (even cp -v doesn't send buffered info, so you'll only get the progress bar once cp completes).

    That said, if you want a progress bar for copying files you should like into either rsync --progress or the pv command.

    Using rsync, the command to solve your question could be something like:

    rsync -avzh --progress **/*.jpg images/