'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 1:[1]
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/
Solution 2:[2]
To use tqdm
in a Bash loop, you need:
- to output something
- to know the total number of iterations (optional)
- to add tqdm after the
done
.
In your case:
total=1000
for f in `find mydir -name *.jpg -print`; do echo $f; cp $f images/${f//\//_}; done | tqdm --total $total >> /dev/null
See the answer: https://github.com/tqdm/tqdm/issues/436#issuecomment-330504048
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | APaul |
Solution 2 | guhur |