Sometimes we don't know which file exactly changed upon execution, usually it's something in the /var/log/ and may be in the tmp or even next to the application executable.
To handle such a situation, we may get the logical size (not physical size) of all the non-zero files
then run the script.sh
then get the new non-zero sizes
and diff the outputs to get the specific files that got a change in size
Put the following script in .bashrc
, and use it like that diff_sizes script.sh
# Use the file names, not the relative path to them, as we grep like that ./PRE_DU
PRE_DU=sizes_before.du
POST_DU=sizes_after.du
diff_sizes() { du -ab | grep -Pv "^0\t" | grep -Pv "[0-9]+\t./(${PRE_DU})|(${POST_DU})" >${PRE_DU}; $1; du -ab | grep -Pv "^0\t" | grep -Pv "[0-9]+\t./(${PRE_DU})|(${POST_DU})+" >${POST_DU}; diff ${PRE_DU} ${POST_DU}; };
-a
get all the files alongside with the directories-b
get the logical size (in bytes) instead of physical size (in blocks)
-P
Perl style-v
Inverse searchgrep -Pv "^0\t"
skip all the zero sized filesgrep -Pv "[0-9]+\t./(${PRE_DU})|(${POST_DU})"
skip thesizes_before.du
andsizes_after.du
>${PRE_DU}
save the output tosizes_before.du
>${POST_DU}
save the output tosizes_after.du
a Cleaner not tested yet version is
PRE_DU=sizes_before.du
POST_DU=sizes_after.du
diff_sizes() {
du -ab | \
grep -Pv "^0\t" | \
grep -Pv "[0-9]+\t./(${PRE_DU})|(${POST_DU})" >${PRE_DU}; \
$1; \
du -ab | \
grep -Pv "^0\t" | \
grep -Pv "[0-9]+\t./(${PRE_DU})|(${POST_DU})+" >${POST_DU}; \
diff ${PRE_DU} ${POST_DU}; \
};
In case of requiring the date too, we'll have to use some other tool than du
, specifically we'll be using ls
however ls
won't iterate over all the files by itself, so we'll combine both find
and ls
to replace du
, such command will be find . -type f -exec ls -al {} +
.
Selecting the size, date and name will take place using awk
, we'll need to be careful with the escaping (we need to escape both $
and "
to be able to set an alias).
For awk
we could use simple print
method with many quotations, however the need of making tabular format with tabs and sufficient spaces moves us to the use of printf
.
Columns $5
till $9
are known based on the output of find . -type f -exec ls -al {} +
.
Also we'll need to modify the grep -Pv "[0-9]\t./(${PRE_DU})|(${POST_DU})"
to be grep -Pv " ./(${PRE_DU})|(${POST_DU})$"
so it's more generic to accept the date too
Following is the updated script with all those changes
alias file_sizes_and_dates="find . -type f -exec ls -al {} + | awk {'printf(\"%8s\t%s %2s %5s %s\n\",\$5,\$6,\$7,\$8,\$9)'}"
PRE_DU=sizes_before.du
POST_DU=sizes_after.du
diff_sizes_and_dates() { file_sizes_and_dates | grep -Pv "^\s*0\t" | grep -Pv " ./(${PRE_DU})|(${POST_DU})$" >${PRE_DU}; $1; file_sizes_and_dates | grep -Pv "^\s*0\t" | grep -Pv " ./(${PRE_DU})|(${POST_DU})$" >${POST_DU}; diff ${PRE_DU} ${POST_DU}; };