Unix Commands and Tricks
These are some unix commands and tricks that can really increase productivity when using a command line. If you have any other such tips and tricks feel free to add them!
pushd and popd
Allow extremely fast navigation between different directories:
dmx:- ~ >cd research/docs/ dmx:- docs >pushd ~/ ~ ~/research/docs dmx:- ~ >cd tools dmx:- tools >popd ~/research/docs dmx:- docs >
pushd pushes a directory on to a stack. You can have as many directories on the stack as you like. popd pops the top directory off the stack and returns you to the next directory on the stack. This removes the popped directory from the stack and so must be pushed back on if you want to return to it. pushd can be used to switch between directories in the stack without actually removing them:
dmx:- ~ >cd research/docs/ dmx:- docs >pushd ~/ ~ ~/research/docs dmx:- ~ >cd tools/ dmx:-tools >pushd ~/research/docs ~/tools <- directory stack is reversed! dmx:- docs >pushd ~/tools ~/research/docs dmx:-tools >
Suspend (Ctrl-Z), fg, and bg
Suspending and backgrounding tasks is a very handy way to quickly pop out of a process to do things in a shell like running a program you're working on or check your mail. Processes (like vim or emacs) can be suspended using the Ctrl-Z keystroke:
dmx:- ~ > vim
some text in vim
~
~
~
~
~
~
1,15 All
-> press Ctrl-Z <-
dmx:- ~ > vim
[1]+ Stopped /usr/bin/vim
dmx:- ~ >
The vim process has been suspended and you are free to use the shell normally. To go back to your vim process, type fg:
dmx:- ~ > fg
some text in vim
~
~
~
~
~
~
1,15 All
You can suspend as many processes as you like (within reason) using this technique. If there is more than one process suspended, you can check on them and foreground the one you want using the jobs command (two vim jobs have been suspended prior to this example):
dmx:- ~ > jobs
[1]- Stopped /usr/bin/vim
[2]+ Stopped /usr/bin/vim
dmx:- ~ > fg 2
some text in vim 2
~
~
~
~
~
~
1,19 All
By specifying the job number to foreground, you can easily switch between suspended processes. Suspending a program halts its execution, so if you have a process, such as a python program you're running from within an interpreter, you can tell the shell to continue running that command in the background using bg:
dmx:- ~ > python Python 2.4.4 (#1, Oct 23 2006, 13:58:00) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = load_huge_file() Loading... -> Ctrl-Z <- [1]+ Stopped python dmx:- ~ > bg [1]+ python & dmx:- ~ >
The python interpreter will now continue doing whatever it was doing in the background. If you had a python script to do the same thing, the above is equivalent to running the command:
dmx:- ~ > python load_huge_file.py &
You can return to the process by using fg.