-
bzip2 "$file"
This will run bzip2
on the filename saved in a variable "$file". bzip2
will compress the file to a new file named $file.bz2
.
-
stat -c %s "$file.bz2"
This runs stat
on the newly created compress $file.bz2
:
From man stat
:
stat - display file or file system status
-c --format=FORMAT
use the specified FORMAT instead of the default
%s total size, in bytes
So, this stat
command will return the file size in bytes of the new file.
-
$(some_command)
This is called command substitution
Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command.
So, var=$(some_command)
saves the output of some_command into a variable var
.
In total:
compress_size_bzip2=$(bzip2 "$file" ; stat -c %s "$file.bz2")
This runs bzip2
and stat
in a subshell. The output of the subshell is the size of the compressed file in bytes, which will be saved in a variable $compress_size_bzip2
.
However, there is room for improvement:
You should combine the commands in the subshell with &&
, so stat
only runs when bzip2
was successful.
If you don't need a compressed file, you should tell bzip2
to compress to standard output with -c
flag, and use wc -c
to tell its size:
compress_size_bzip2=$(bzip2 -c "$file" | wc -c)