Update (2/20/09): Check out Useful unix tricks – part 3 and Useful unix tricks – part 4

I covered a bunch of unix command line tricks in my previous blog post: Useful unix tricks. Here are some more.

Learn vi

Even if you prefer another text editor (such as emacs), it is worth your time to learn vi. vi is installed on every unix box, whereas emacs and others are not. Furthermore, many other unix tools use vi shortcuts (such as less), so learning the shortcuts will help out with other commands.

Use less instead of tail

I find that I often want to see the end of a file. The first command that jumps to mind is tail:

% tail /var/log/messages

Inevitably, I realize that I need more than the last 10 lines. So my next command prints more lines from the bottom:

% tail -n 100 /var/log/messages

However, an easier approach is to use less to display the whole file and then jump to the bottom (using Shift+g):

% less /var/log/messages
Shfit+g

Now, I can scroll up and down at will.

mkdir -p creates nested directories

By default, mkdir will only create a top level folder. So the following command will fail:

% mkdir some/nested/folder
mkdir: cannot create directory `some/nested/folder': No such file or directory

However, if you pass the -p parameter, mkdir will create all of the nested directories.

% mkdir -p some/nested/folder

ps will show process trees with the f flag

The ps command shows a list of running processes. The f flag will show the processes in a tree, so it is evident which processes started other processes. For example, a normal ps output looks like:

% ps
  PID TTY          TIME CMD
 6071 pts/2    00:00:00 zsh
 6287 pts/2    00:00:00 ps

The f flag output looks like:

% ps f
  PID TTY      STAT   TIME COMMAND
 6071 pts/2    Ss     0:00 /bin/zsh
 6288 pts/2    R+     0:00  \_ ps f
 5946 pts/1    Ss     0:00 /bin/zsh
 6126 pts/1    Sl+    0:18  \_ ruby ./script/server

Now, it is clear that I have two terminals (with zsh shell) running. One of them ran the ps command, and the other is running ruby script/server.

Normally, I use “ps afxww” which shows even more. The ax flags show all of the processes, not just the ones that I started. The ww flags tell ps to display long lines instead of truncating them.

Note: The mac version of ps does not support the f flag. In this case, use the program pstree instead. It can be installed via MacPorts.

lsof shows open files

lsof lists all open files on the system. This is useful for determining what is reading or writing to a certain file. For example, if you can’t unmount a drive because it is busy, run the following command to see what is still using the drive:

% lsof | grep /media/usbdisk

lsof can even be used to see what is listening on a given port. For example:

% lsof | grep 3000
ruby      6126       paul    5u     IPv4      25036              TCP *:3000 (LISTEN)

There is a ruby process (with process id 6126) listening on port 3000. Now, we can use ps to see the full process information:

% ps ax | grep 6126
 6126 pts/1    Sl+    0:26 ruby ./script/server

16 Responses to “Useful unix tricks – part 2”

  1. Huh, I didn’t know about that last one. Very useful! Thanks

  2. Is it real?

  3. lsof is a great tool, but it does not come with solaris by default. :-(

  4. to find out which program is listening on a port you could also use
    lsof -i :

    lsof -i :80 will list all programs that are using port 80 and which ip they are listening on
    (you have to be root to find ports below 1024)

  5. use of tail is still better .

    tail -f log/access_log

  6. Good one.

    but i want some more on cygwin…
    i am winXP user and using cygwin for my scripting needs…
    so it will be very good if you can provide detailed one on cygwin and which packages i need to install on windows.

    Techdudes.

  7. the ps -f flag doesnt work in most unix clones. it doesnt work in FreeBSD for example.

  8. Ghettohaxor: There’s no dash (-) in the command. Just “ps f” without the quotes.

    As for lsof, that’s one powerful command. There’s very few things you can’t find out about your machine using that command.

  9. Good examples, thanks. I did not know about the last one :)

    The above user is correct, there is no dash in “ps f” command. the “ps -f” command is basically the same as “ps”; it prints some extra info.

  10. On example one, using `less` instead of `tail`:

    I don’t agree that it should be considered a good practice. The number one reason is the line tracking functionality of `less`. When you `less` a very large file – as /var/log/messages tends to get to be – you may then have to wait on the system to catch up while it numbers all the lines in the file.

    Instead, my recommendation is to create a `tail` alias by doing this:
    echo ‘alias tail=”tail -n 1000″‘ >> /etc/profile

  11. Luis: Just about everything doesn’t come with Solaris by default.

  12. Try opening a logfile with less and hit shift-f to tail it.

  13. For finding out the programs currently using a TCP/IP port you can also use:

    netstat -tap

    The ‘t’ is for tcp/ip connections only (no unix sockets), the ‘a’ is for all (shows connections that aren’t quite closed yet) and the ‘p’ shows the name/PID of the process. Of course you can grep if you don’t want to look at all of the output.

  14. Good examples, thanks. I did not know about the last one :) The above user is correct, there is no dash in “ps f” command. the “ps -f” command is basically the same as “ps”; it prints some extra info.

  15. thanks for all the information

  16. Very nice. I just found this info today, and added it to my personal “notes and tips on Linux”. Thanks a lot

Sorry, the comment form is closed at this time.

© 2012 Paul Gross's Blog Suffusion theme by Sayontan Sinha