Home > UNIX > Useful UNIX Tips and Tricks

Useful UNIX Tips and Tricks

Here are a few pieces of Unix wisdom, I picked up during my 10 years of experience…

You’ve done something that has completely screwed up your terminal. Everything you type is either invisible or incomprehensible. Try the following:

$ ^C

$ stty sane^J

If this doesn’t work, try:

$ echo ^V^O

To determine what program dumped an anonymous core file, use gdb:

$ gdb -core core

Core was generated by `a.out’.

If you are on a system without gdb, try the file command:

$ file core

core: ELF 32-bit LSB core file of ‘a.out’ (signal 6)

Some keyboard shortcuts for editing

There are some pretty useful keyboard shortcuts for editing in bash. They might appear familiar to Emacs users:

Ctrl + a => Return to the start of the command you’re typing

Ctrl + e => Go to the end of the command you’re typing

Ctrl + u => Cut everything before the cursor to a special clipboard

Ctrl + k => Cut everything after the cursor to a special clipboard

Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to

Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)

Ctrl + w => Delete the word / argument left of the cursor

Ctrl + l => Clear the screen

To create a vi macro that will wrap the current paragraph to a reasonable number of characters, add the following to your .exrc or .vimrc file:

map Q {!} fmt -c -u^M

Now, when you press ‘Q’ in visual mode, the current paragraph will be wrapped to approximately 70 characters.

When recursive copying, cp (cp -Rip, etc.) may not be the best tool for the job. For example, cp copies hard links as separate files, which is probably not what you want. To get a true copy of a directory, try:

$ tar cf – <dir> | (cd <destdir>; tar xf -)

This will create an exact copy of ‘dir’ in ‘destdir’. The same principle can be used to create a recursive copy on a remote machine:

$ tar cf – <dir> | ssh remotehost “(cd <destdir>; tar xf -)”

To list the files in another directory that match more than one pattern, it is easiest to do:

$ ls -l /usr/local/foo/{*.conf,*.local,*.rc}

which is equivalent to:

$ ls -l /usr/local/foo/*.conf /usr/local/foo/*.local /usr/local/foo/*.rc

This syntax is supported by (at least) bash, ksh, csh and sh.

You can extend this idea to make renaming files in another directory, for example, a little easier:

$ mv -i /usr/local/foo/bar/baz/{stuff,stuff~}

A little insurance against running ‘rm -rf *’ in the wrong directory — create an empty file called -i in any critical directory:

$ >-i

or…

$ touch — -i

If the ‘rm -rf *’ command is issued in that directory, the shell will expand the ‘-i’ early on and go into interactive mode, thus giving you a chance to say ‘Whoa, that was close!’, which always sounds better than ‘Oh fsck!’.

This tip works because the -i option to rm will override any previous -f.

Be forewarned that this tip only protects against ‘rm -fr *’, i.e., files and directories in the current directory.

To remove the -i file (or any other file beginning with ‘-’):

$ rm ./-i

or…

$ rm — -i

To return to the previous directory in ksh or bash, use:

$ cd -

To use the last argument of the previous command line as an argument, use $_. For example:

$ ls -l /usr/home/dmr/somefile

$ vi $_

To make the up and down arrow keys work and thereby enable command line editing and recall in ksh, include the following lines in your ~/.profile:

set -o emacs

alias __A=’^P’

alias __D=’^B’

alias __B=’^N’

alias __C=’^F’

alias __H=’^A’

You need to enter the actual control characters ^P, ^B, etc.

You can kill any process with ‘kill -9′ as root, right? Not necessarily. It’s quite easy to write a process that cannot be terminated, even with ‘kill -9′.

This was a hard-learned lesson that will only be given out via email request, if at all.

Advertisement
Categories: UNIX
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.