Archive for the ‘shell tips’ Category

Bash: Selectively Tar-ing files from a web root (back up your website)

June 15th, 2012 No comments

nice -n 19 find . -type f \( -iname “*.php” -o -iname “*.js” -o -iname “*.inc” \) -print0 | xargs -0 tar rvf dev$(date ‘+%d-%m-%Y’).tar

I had terrible trouble getting this to work the tar wasn’t capturing all the files I eventually found out that I required the print0 and the xargs -0. This would have been trivial with zsh but I didn’t have it this server.

Why the selective backup? because there are huge images and PDFs etc which would have slowed down the backup enormously.

Categories: shell tips Tags:

bash: Find specifying certain file extensions

November 2nd, 2011 No comments
I had terrible trouble finding a way to do this in bash, doesn’t seem to be possible using -name or -iname :-

find /var/www/html/ -regex “.*\(php\|js\|inc\)” -exec grep -i ‘tbl_title’ {} +

find /var/www/html/ -regex “.*\(php\|js\|inc\)” -exec grep -i ‘tbl_title’ {} \;

NB: the \; and + endings are different the + ending usefully displays the matching file name

Of course if you have a recent bash you will likely prefer:-

grep -ilR –include=*.{php,inc,js} ‘fn_main’ c:/inetpub/wwwroot


grep -iR –include=*.{php,inc,js} ‘fn_main’ c:/inetpub/wwwroot

Categories: shell tips Tags:

Wonderful bindkey : assign anything to any key

February 11th, 2011 No comments

I have been working on a laptop a lot recently but it was bugging me that I didn’t have the * key on its own key as you do on a full size keyboard. I realized that I wasn’t using the function keys F1 to F12 at the shell level but didn’t know how to configure. Now my situation is a little complicated as I’m using zsh on cygwin on a Windows 7 PC.

Now I’m used to using bindkey to launch widgets eg control-O to duplicate previous word on the command line , and control-L the hyper-useful suspend current command line entry to allow execution of a single command ie do a quick ls or change a permission etc.
here they are:-

bindkey -M viins ‘^O’ copy-prev-shell-word # copy previous shell word

bindkey ‘^L’ push-line # push current command into a buffer,
These use built-in widgets however the really clever amongst can develop their own.

Anyway I just wanted to reprogram the function keys to output another character or string. I posted a question into the zsh newsgroup and quickly got a reply. I needed the -s option

What you have to do, type the following at your prompt yes actually type Control-V followed by F7

> bindkey -s “^vF7” “ls -l” # replace the ls -l with whatever you like

Now heres the rub, what you actually see generated is (will vary from system to system)
bindkey -s “^[[18~” ‘ls -l’

this is what you must copy and paste into your shell startup file

This is actually simple enough for me to type these from the CLI for a short-term use. Boy am I going to enjoy using bindkey!

Categories: Cygwin, shell tips Tags:

zsh wonderful use of dirs,popd,pushp, cd ~5

February 20th, 2010 No comments

I’ve never got the hang of popd & pushd they seemed a bit pointless. zsh however has extended their abilities eg:-

> dirs -v    # number and list your recent directories one per line

> cd ~5     # cd to directory 5 in the directory stack

> cd -<tab>    # the tab complete lists the directory stack then type a digit to select the one you require (saves you having to do a dirs -v in the first place

I have to work in very complicated directory structures so this will be fantastically useful.

#don’t forget the zsh cd substitute command

> cd olddir newdir

eg you are in


> cd live test


Categories: shell tips Tags: