Posts

Showing posts with the label shell scripting

TIL: Keeping parent directory structure while copying

There had been instances when I wanted to copy "/home/roy" to a target directory "/target" as "/target/home/roy". I used to do some scripting foo to get this done. A few days back, I learned this cool option called "--parents" in cp command that will preserve the parent directory structure while copying. Made my life a lot easier on one of my backup tasks.

TIL: Sort human friendly values

Sorting numeric values in the input is easy. It just takes "sort -n". But what if the input contains human friendly units. For intance, "5G", "3M", "4K", etc. There is a flag to recognize and sort based on the human friendly units: "-h". Incorrect: du -h | sort -nr Correct: du -h | sort -rh Caveat: "sort -h" works only on upper-case units. "K" will be treated as kilos, but "k" will not be!

Setting the terminal window title - version 2

I had earlier written a small snippet about setting the window title from command line. Based on my experience, I felt that I could make it a lot simpler. Here is version of the same function. function wtitle {     if [ -z "$ORIG_PS1" ] ; then         ORIG_PS1=$PS1     fi     export PS1="\[\033]0;$1 - \u@\h:\w\007\]$ORIG_PS1" }

A rarely used but very useful option in grep

I usually do a lot of log analysis. I search for patterns in multiple files in multiple hosts. Then I collate the result and do processing on the result lines. A sample output looks like: /tmp/input1.txt: line containing pattern /tmp/input2.txt: another line containing pattern  As it turns out, I don't need the file names in the grep result. I always used to remove the file name prefixes using a Perl one-liner. Silly me! I was pretty sure that this was a common problem and it must have been solved already. When I referred to the man page of grep , I came across this gem: -h . If you specify this option, grep will not prefix each line with a the file name. There is also -H option which will prefix each line with a filename even if you are searching in only one file.