Wednesday, November 25, 2009

find a file based on date timestamp

find . -type f -newer guidedog | xargs -i mv {} going2dogs

With the above command, we are
1) looking for files (type -f) with date and timestamp newer than file guidedog.
2) then moving those files to directory going2dogs

guidedog is a dummy file which has the reference time we need. It can be created as -
touch -t 200911251340 guidedog

So all the files that were created after 1:40 PM on 11/25/2009 will move to directory doing2dogs.

Also, between two timestamps will work as

find . -type f -newer guidedog -a ! -newer anotherguidedog | xargs -i mv {} going2dogs

Update : Above command will move anotherguidedog also. To avoid that additional condition should be added before passing output to xargs:
-a ! -name
anotherguidedog

changing case within vi

1)
If you have to change one or two letters, use ~ over the letters while in command mode. vi toggles the cases with ~

2) Between a range, say marks a and b

Change to lower case
'b,'a,s/[aA-zZ]/\L&/g

Change to upper case
'b,'a,s/[aA-zZ]/\U&/g

3) Change to upper case thru out the file
%s/[aA-zZ]/\U&/g

Running shell commands from within vi

Without leaving vi, we can run shell commands from within vi.

e.g. we need to insert contents of a file within our current file opened in vi.

1) Insert will begin immediately after the cursor. So we place cursor where we need the insert.
2) enter vi command mode
3) :r! cat filename

:r! can be used to insert output of a unix command also.
e.g. :r! ls
the above command inserts list of the files in the current directory (pwd)

:sh can be used to enter shell from within vi.
to return to vi, use exit.