Wednesday, October 31, 2007

Useful VIM commands and Linux Tips

These are a random "how to"s collected over a long period of time. Some I figured out, others I read/found/googled. Some may have become outdated now. Uploading it here so that its easily accessible:

----------------------------------
Autoindenting a file:
:0
=:$

Removing blank lines:
:g/^$/d

with tabs and spaces:
:g/^\s*$/d
-----------------------------

1. Finding and removing of files using find command:

find . -name -print -exec rm '{}' \;

2. Grep'ing through files
grep -r

3. How to search for a string in a selection of files (-exec grep ...)
find . -exec grep "www.athabasca" '{}' \; -print

4. Find and Replace an Expression

# This one liner will search all text files in the current
hierarchy looking for an expression:

# 1) find all files

# 2) xargs ensures standard input doesn't overflow. I use xargs
instead of -exec because -exec has been very unreliable across
different vendor Unix implementations.

# 3) file determines file types

# 4) grep traps for text files

# 5) cut the file name from output of the grep

# 6) egrep the text files for expression $*

find . -type f -print | xargs file | grep -i text | cut -f1 -d: |
xargs egrep "$*"



# The above one-liner can be easily modified to other kinds of
# operations to text files.

# Consider the need to replace all instances of Linux with GNU/Linux....
#
find . -type f -print | xargs file | grep -i text | cut -f1 -d: | while read
file; do

vi $file >/dev/null 2>&1 </dev/tape 2>bckup.out
# The -newer option only finds files newer than bckup.out... that is,
only files that have been modified since the last backup. This makes
the command useful for doing incremental backups.

# Assuming you are root, and you do this from the / directory, this
should backup up everything on /. The / is preceded with a "." to
make sure that we can restore the tape to any directory... not just /.

-----------------------------------------------------------------------

Disabling ctl-alt-del
- comment out the trap CTRL-ALT-DELETE section of /etc/inittab
- the entry looks like
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
- uncomment the ca line

-----------------------------------------------------------------------

Disabling ctrl-alt-backspace base X shutdown
- add DontZap to ServerFlags section of /etc/X11/XF86Config file

-----------------------------------------------------------------------
SMB mounting windows share

Only root can mount smb partitions.
For mounting g: drive (shared as user on WinNT) on girnar on
/mnt/girnar/g, use the following command:
mount -t smbfs -o username=persist,password=persistent //girnar/users /mnt/girnar/g

Mounting using Persistent domain account name
mount -t smbfs -o username=persistent\\penguin,password=penguin,uid=500,gid=100
//osd3nas/Users /mnt/nas/
-----------------------------------------------------------------------
Converting plain text files to html using vim

The following command would create an html file out of file_name
input. This conversion would also retain the syntax coloring.

gvim -f +"syn on" +"so \$VIMRUNTIME/syntax/2html.vim" +"wq" +"q"


e.g.
gvim -f +"syn on" +"so /usr/share/vim/vim58/syntax/2html.vim" +"wq"
+"q" Glossary

would convert Glossary into Glossary.html
-----------------------------------------------------------------------
Adding a Role in Pine
http://www.ii.com/internet/messaging/pine/changing_from/#vars

-----------------------------------------------------------------------
Creating multi-volume tape archive

E.g. tar -cMf kishore.tar -L 1024 /opt/music/mp3/kishore/
would create a tar file kishore.tar from /opt/music/mp3/kishore till
it reaches a size of (approx. 1024*1024) and if the directory is still
not completely archived, it will prompt for next volume:
Prepare volume #2 for `kishore.tar' and hit return:

To this question you should supply the name of the next archive like:
n kishore1.tar

Now tar will respond with:
Prepare volume #2 for `kishore1.tar' and hit return:

To this you respond with y (for yes) and

It would go on to prompt you for volume#3 and so on.

-----------------------------------------------------------------------
If the TERM env variable is not =vt100 and is =xterm, then the
coloring used is very weird. Change the TERM to vt100 in
.bash_profile.

-----------------------------------------------------------------------
Using tcpdump

To capture all packets originating or destined to host "girnar",

tcpdump -N host girnar > /tmp/girnar.dump

The -N option omits the domain name (intranet.pspl.co.in) in the
dump. We are redirecting the output to /tmp/girnar.dump here.

-----------------------------------------------------------------------
Using Ctags in vi

Generate tag file on all the sources by using ctags * in the directory.
A file 'tags' would be created in the current directory. An easy way to do
this is execute ctags -R in the directory. It will recurse through the
entire directory and generate tags for all the files.

To use the tags from vi:
- to jump to the definition of the keyword, place the cursor over it and
press ctrl-] (escape key for telnet)
- to jump back to the reference, press ctrl-t

If using telnet, then the escape key can be suppressed by using
telnet -E

-----------------------------------------------------------------------

To restore MBR from Windows

From command prompt, run
fdisk /mbr

Running /sbin/lilo should re-install LILO on MBR again after doing the
above step

-----------------------------------------------------------------------

For viewing RTF documents, use Ted. Its in /usr/local/bin/
Note the capital T in Ted. 'ted' won't work.

-----------------------------------------------------------------------

For enabling access control on any directory served by Apache:
- Create a .htaccess file in that directory with following contents
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local/apache2/conf/passwords
Require user persist

where 'persist' user is allowed to access this directory only if he
supplies proper password. The encrypted password is stored in the
'AuthUserFile'. The username/passwords can be created by using
htpassword utility that comes with apache and is in bin/ directory
of Apache config home.
- The Webserver user (nobody) has to have read permissions on
.htaccess and the password file

There is another way to enable this, through httpd.conf file. But I
have not tried it out.

-----------------------------------------------------------------------
How to get .rhosts working?

- Create .rhosts file in $HOME
- Make sure it has 644 permissions (rw-r--r), OTHERWISE it give
permission denied error
- Make sure rsh is enabled through inetd (/etc/xinetd.d/rsh should
have disable=no)
- Restart xinted if required
- rsh hostname -l loginname command (DONT'T forget the command)

-----------------------------------------------------------------------
To run apache as root
- set environment variable CFLAGS to -DBIG_SECURITY_HOLE
- compile apache and install it
- change USERS in httpd.conf to root (nobody is default)

-----------------------------------------------------------------------
SSH based port forwarding
ssh -L port:host:hostport
-----------------------------------------------------------------------
Adding a route for specific host
For e.g. to add a route to smtp machine through bhageerath gateway:
/sbin/route add smtp.intranet.pspl.co.in gw bhageerath

----------------------------------------------------------------------
Adding authentication to Twiki
- Follow the documentation
- Pay attention to "AllowOverride All" option for the
section of twiki/bin folder
- Modify .htaccess appropriately

----------------------------------------------------------------------
Compiling gaim on RH 7.3
- Upgrade Glib to 2.0, GTK to 2.4
- GTK upgrade requires Pango1.4 with Xft which requires Freetype2.1.4,
fontconfig-2.2.2 and Xft-2.1.2
- For Xft, your require xrender-0.8 which requires render-8.0 which
requires one file (extutil.h) to be copied to
/usr/X11/include/extensions (I got the file from
http://mail.gnome.org/archives/garnome-list/2003-May/msg00234.html
after googling)

----------------------------------------------------------------------
Renaming several files at the same time

The following line would rename all the *.C files to *.cc
\ls *.C* | sed 's/\(.*\).C\(.*\)/mv & \1.cc\2/' |sh
----------------------------------------------------------------------
Configuring Linux client to use DHCP

In the file ifcfg-eth0 under /etc/sysconfig/network-scripts, set the
value of BOOTPROTO to dhcp:
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
----------------------------------------------------------------------
Encrypting files in VIM
Use the :X option. It asks for the encryption key.

No comments: