Tips and Tricks - 2

Well, it's been a while since I posted the first Tips and Tricks article. Most of the following stuff was gathering dust (of the digital kind) ;-), waiting to be formalized into a post. So, I finally took pity of it and came up with this post.

1. Handling zombie processes

To reap (can't kill a zombie) a stubborn zombie process, you can do either of the following:

  • Using kill, send SIGCHLD (signal number 17 on Linux) to the parent of the zombie.
  • Send SIGCONT (signal number 18 on Linux) to the zombie process itself.
2. Safer output redirection in Bash

To prevent files from being overwritten by any redirection operator such as >, >& or <>, put either of the following in your ~/.bashrc:

set -o noclobber

-- or --

set -C

This can help you from accidentally overwriting files. If you decide that you do want to go ahead with the operation, you can use the >| operator instead.

3. Error codes

If you are often scratching your head wondering what a particular error code means, you should give errno a try. It is a part of moreutils. errno lets you look up what a particular error code means, by either specifying the name or the code of the error.

For example:

$ errno 13

-- or --

$ errno EACCES


EACCES 13 Permission denied

You can list all available error codes using:

$ errno -l

Note: That's a lowercase L.

You can even search for errors containing a given word (or words) using:

$ errno -s "search string"
4. Uptime

Apart from showing the actual running time of the system, the uptime command can also show when the system was powered on, using:

$ uptime -s
5. Display file contents preceded by the filename on stdout

We normally use cat to display contents of a file on the stdout. But when dealing with multiple files, for the sake of clarity it is sometimes desirable to show the filenames on the stdout as well. One of the easiest ways to do this is using head in the following manner:

$ head -n -0 filename1 filename2 filename3

Explanation: Using the -n switch, we can specify the number of lines (say K) to display, starting from the top. If we prepend - to the number, all but the last K lines of the file will be displayed. Using -0, it basically means, print all the lines of the file.

6. Scanning all ports using Nmap

To scan all the ports of a target machine, you can use (replace localhost with your desired target):

# nmap -p- localhost

-- or --

# nmap -p1-65535 localhost
7. Retrieving all types of DNS records using dig

By default dig only performs a query for an A record. To query all types of DNS records available with a domain, use:

$ dig any
8. Man pages

8.1 Checking source of a man page for errors

To print any problems with man page sources, use:

$ man --warnings <manpage> > /dev/null

This will redirect the normal output to /dev/null and the problems (if any) will be printed on stdout.

8.2 Sequentially viewing man pages

You can view multiple man pages sequentially by passing their names to the man command. For example:

$ man bash tmux xinit

After you quit viewing the first man page, you will be prompted by man to view or skip the next man page, or quit altogether.

Here's an example of how it looks like:

--Man-- next: tmux(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: xinit(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]


Comments powered by Disqus