'How to join two bash commands (creating tar archive and print the size of the tar file) to run in the same time? [closed]
I want to do a bash script which will create a big tar archive of files and will print the size of the tar file every 5 seconds. For example, the archive will take 60 seconds to create and on the screen the size of tar file printing screen every 5 seconds.
Solution 1:[1]
watch is the relevant command. An example script:
#!/bin/bash
refresh=5 # seconds
archive=$1
shift
watch -n "$refresh" du -h "$archive" &
tar czf "$archive" "$@"
kill $!
Usage: ./watch-tar my-archive-name.tar.gz /my/path/1 ...
This repeats du -h to get the file size, until tar completes, and watch is killed.
There's also tar caf to set compression format based on the extension given in the archive name.
You might want to use a loop to avoid a file not found error (because watch starts before the file exists), although I didn't get one when testing.
# replace watch ... & with:
until
test -e "$archive" &&
watch -n "$refresh" du -h "$archive"
do
sleep 0.2
done &
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 | dan |
