Saturday, January 28, 2012

Usefull Unix commands, tips and tricks...

Have you seen this question on Hacker News? It's great and actually there are really some useful commands and tips for use of the command line more efficiently. Here I'll list some of them in case you want a quick glance, but be aware that something mentioned there might already be known to me and/or I decided that it's not so important and excluded it.

So, without further ado, here's my list of favorites:

  • How many times did you use commands jobs, fg and bg? Well, it turns out that you can "free" command from the current shell using disown command. Additionally, this command will protect all disowned commands from receiving SIGHUP when the shell exits. But, it could be that the shell option huponexit isn't set and commands in background are by definition protected. disown is specific to bash, zsh of course has its own variants.
  • It certainly happened to you that you had some command running whose standard output and/or standard error was redirected to /dev/null, or was otherwise inaccessible. Well, there is a way to redirect stderr and/or stdout of a running command without stopping it. But, it is a hack and you need to have gdb installed. Basicall, you have to execute the following commands:
    touch <log>
    gdb -p <pid>
    p close(1) # or 2 for stderr
    p open("fullpathtologfile", 1)
    Then, you'll be asked are you sure, and when answered y(es) gdb will exit and the process will continue to execute. Also there is a chance that you have a command on your system that can do it more cleanly, reptyr.
  • One commenter said that he's using Ctrl+L instead of clear. I might add Ctrl+D to exit current shell (instead of typing exit and/or logout). Note that Ctrl+L you can use no matter if you already typed something or not, while to use Ctrl+D command line has to be empty (apart from prompt, of course.)
  • Built in command fc starts editor (defined in EDITOR environment variable, or vi by default) and allows you to edit last command. To have what's currently typed on command line press Ctrl+x Ctrl+e. But note that if you don't have EDITOR variable defined the default editor for that command is emacs! Finally, the command edited in editor is always executed after exit. To skip execution prepend whatever you typed with # (i.e. mark it as a comment).
  • In case you started to type some command and then realized that you need to first execute/run something else, then you can press Ctrl+U, type and execute that another command, and then bring back what've you typed with Ctrl+Y.
  • bash, by default, uses emacs mode. To switch to vi mode use set -o vi, then in vi command mode pressing v will bring editor with the current content of the command line ready to be edited. Oh, and yes, to switch back to emacs mode use set -o emacs.
  • In vi, pressing q: (note, that's reverse from quit command!) opens a windows with a editable and searchable history of all typed commands. To exit from that window use normal exit command (i.e. :q). Similarly, q/ brings window with search history.
  • If you want to execute some command that you already typed and it is in your history, you can use Ctrl+R for incremental search. Just type the substring from the command you are searching for and the shell will display the most recent command that has that substring. In case you typed the whole substring but you want some older command, then press Ctrl+R again, as many times as it is necessary to find the command you wish. To execute command, just press RETURN, or any other key will start editing of the displayed command. This is actually feature of readline library so any command that uses this library will provide Ctrl+R.
  • Take a look at lsof command! It is a very capable command that can you give a lot of information about your system.
  • xargs command was made to allow running some command on a large number of arguments. Namely, there was (somewhere still is) a restriction on a number of arguments in a command line. When you hit that limit there is no way to run some command and that could be a problem (e.g. when you are using backtick to execute some other command whose output should be feeded to the some command). In that case you can use xargs that will read arguments from standard input and repeatedly execute some command with batches of arguments. The execution will be sequential. In case you wish to execute command in parallel use option --max-procs or command parallel.
  • I used so many time -exec option to find command only to now learn that I can end a command with plus (+) instead of a semicolon (;) and that plus sign will behave like xargs!
  • Have you ever needed to lookup something in ASCII table and you had to open browser or search for some book around you? Well, type 'man ascii' and go no further than the command line itself! :)
  • This one is funny, command sl. When user accidentally types sl instead of ls he gets harmless surprise. :)
  • Command vidir (part of moreutils package) will open a directory listing in vi (or whatever is defined in EDITOR variable) and allow you to change names of files and directories. When you quit, renames will be executed!
And, while not tip in the sense used above, it is very interesting nevertheless, so take a look at the useless use of cat command.

No comments:

About Me

scientist, consultant, security specialist, networking guy, system administrator, philosopher ;)

Blog Archive