Tuesday, June 22, 2010

How to use regex in find command

Today I discovered how to use regex in unixs' find command. It has a very simple syntax as below:

Syntax: find \path\ -regex '\regex pattern\'
e.g. find ./ -regex 'file[0-9]+.txt'

In above example, we are looking for files with name like file1.txt or file23.txt or file567.txt. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'.

According to find man page, it is also possible to specify the type of regex to be used, like emacs regex, posix-awk, posix-basic, posix-egrep and posix-extended. Default is emacs regex.

Another option is -iregex. It is just like -regex, but the match is case insensitive.

Monday, June 21, 2010

How to specify destination for tar extraction

Specify a destination to extract a tar archive
It is very simple, say:
/root/file.tar.gz - gzipped tar archive file
/usr/local/tmp - destination location where above file needs to be extracted to.

command:
tar -xzf /root/file.tar.gz -C /usr/local/tmp

You might have already noticed '-C' option. This can be used to explicitly specify destination location if it is other than PWD.