Friday, June 29, 2007

Summary Execution

I find myself coming across strange files on occasion. Crowded folders, shady thrice nested system dirs and the like are filled with them. Usually system files, files whose nature and purpose I might not know. To get to know such individuals, there are several built-in tools I might employ. I could run file on them, cat them, tail them, head them, run ls -lh in the folder, et al. But this takes too many steps. I want one command. And if I can get what I want in 5 steps, I can get it in one. It is the Linux way.

Thus I created the "summarize" function. It's a simple script that basically just grabs data using the methods I listed above, and formats them into an easy-to-read summary. Right now, it consists of:
# A command to provide lots of
# info about a file at glance!
FILE=`file $1`
echo "$FILE has `wc -l $1 | cut -d" " -f1` lines." &&
echo -e "\e[1;34mOwned by: \e[0m`ls -l $1 | cut -d" " -f3`" &&
echo -e "\e[1;34mGroup: \e[0m`ls -l $1 | cut -d" " -f4`" &&
echo -e "\e[1;34mSize: \e[0m`ls -lh $1 | cut -d" " -f5`" &&
echo "**********************************"
echo -e "\e[1;34mTop bits: \n \e[0m" &&
head -n 5 $1 &&
echo "" &&
echo -e "\e[1;34mBottom bits: \n \e[0m" &&
tail -n 5 $1
exit 0
One handy thing I learned while writing this was how to display text in chosen colors in bash, using the "echo -e" syntax. I find this makes it easier for my eye to quickly move to different fields.

You can also add something like this to your .bashrc:
alias summarize='/location/of/script/'

Example output on a test .txt file (screenshot to show colors. Your base text color will be different):

[EDIT, 07/03/07]: It is also useful to add contextual lines to the head and tail output. This can be done via:

head -n 5 $1 | nl -b a
nl -b a $1 | tail -n 5
This prints the first and last five lines, numbered according to the number of lines in the entire file. I find this makes it much easier to immediately see how long the file is. (Additional note: without the "-b a" option, nl will print lines for each textual line, skipping blank ones. I myself would rather the blank lines also be included, as this is standard practice.)


Post a Comment

Subscribe to Post Comments [Atom]

<< Home