I will attempt to show each command or sequence in context, so you can see both how it works and what it does, and in which situation it is useful. I will comment after commands to tell you what they do whenever appropriate. Some of this stuff you will already know, veterans will probably know all of this already. However this is stuff I use on a daily basis, and I think everyone would benefit from knowing it.
Hold on, because we are going to cover a lot of ground!
$ pwd # displays the current directory /tmp $ cd # calling cd without any args will change to your home directory $ pwd /home/gorman $ cd - # calling cd with the arg - will change to your previous directory /tmp $ ls -la # you don't need seperate "-" for each argument drwxr-xr-x 2 gorman gorman 4096 Sep 25 23:11 . drwxrwxrwt 12 root root 4096 Sep 25 23:11 .. $ ps a # programs that don't take arguments don't even need the "-" PID TTY STAT TIME COMMAND 8854 pts/1 RN+ 0:00 ps a $ mkdir test $ cd !^ # in bash "!" means "command", "^" means "first argument of" # thus this says "cd last command's first argument" cd test # which would be "cd test" # you will also notice that when using shorthand like this # bash will print the expanded command $ touch a # touch is a quick and easy way to create files # if the file already exists it won't even throw an error $ echo "Hello, world" >> a # quick and easy write to file $ cat !$ # "!^" means "first argument", "!$" means last argument cat a Hello, world $ cp !$ b cp a b $ echo "Goodbye, world" >> !$ $ history 3 # history will display the last n commands 2083 cp a b 2084 echo "Goodbye, world" >> b 2085 history 2 $ cat !2083* # "!" is command, so "!2084" means command number 2084 # the "*" means "all args" cat a b Hello, world Goodbye, world $ !!:s/b/a/ # "!!" means "previous command with all args" # ":s/b/a/" means "replace all instances of "b" with "a" cat a a Hello, world Hello, world $ !2083:s/b/a/ # of course, we can do the same with history too cat a a Hello, world Hello, world $ ^ a^ b^ # this is shorthand for shorthand - it means # replay last command with all " a" substitited with " b" cat b b Goodbye, world Goodbye, world $ !-2 # run the command 2 commands ago cat a a Hello, world Hello, world $ echo !# # "!#" means "whatever has been typed before this" echo echo # Here it is printing the command echo # and here it is echoing echo, as the command says! # I'll admit, I never used !# in a useful situation... $ cd .. # go to the current directory's parent $ ls test/ $ mkdir test # mkdir on a directory that already exists mkdir: cannot create directory `test': File exists $ mkdir test 2>/dev/null # redirect "2", stderror, to /dev/null; aka the black hole # we don't get an error message now! $ hostname # what box am I on? local.cs.curtin.edu.au $ ssh email@example.com # just sshing in to a box like normal password: # typing passwords is so tedious! Last login: Tue Sep 25 12:23:30 2012 from 127.0.0.1 # let's set up automatic logon $ exit # time to put our skills to the test! Connection to gorman.cs.university.edu.au was closed. $ alias sshhost="!!" # we will need to SSH there again, so let's alias it $ cd $ mkdir .ssh 2>/dev/null $ cd !^ cd .ssh $ ssh-keygen -t rsa # make an RSA key Generating public/private rsa key pair. Enter file in which to save the key (/home/gorman/.ssh/id_rsa): Created directory '/home/gorman/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/gorman/.ssh/id_rsa. Your public key has been saved in /home/gorman/.ssh/id_rsa.pub. The key fingerprint is: 01:23:45:67:89:ab:cd:ef:ed:cb:a9:87:65:43:21:01 gorman@local $ sshhost mkdir -p .ssh # that alias came in handy, let's make the directory on the server password: # not enjoying all these passwords... soon I won't need to type them $ cat id_rsa.pub | sshhost 'cat >> .ssh/authorized_keys' # cat that key and send it right across password: # this is the last time I need to type that sucker! $ sshhost Last login: Tue Sep 25 23:50:58 2012 from 127.0.0.1 # success! $ pwd /home/gorman $ echo "alias sshlocal='ssh local.cs.university.edu.au'" >> .bashrc # .bashrc is a very useful file to add aliases to # alternately you can make a seperate aliases file and have .bashrc call that $ exit $ echo "`alias sshhost`" >> .bashrc # surrounding a statement with ` ` make it execute # this code will echo the sshhost alias and add it to .bashrc # very useful for adding a temporary alias to your permanent list Connection to gorman.cs.university.edu.au closed. $ scp .aliases gorman.cs.university.edu.au:~ # let's copy my aliases over .aliases 100% 4430 4.3KB/s 00:00 $ sshhost Last login: Tue Sep 25 23:53:58 2012 from 127.0.0.1 $ chmod +x .aliases # make aliases runnable $ echo "~/$!" >> .bashrc # tell it to call my aliases echo "~/.aliases" >> .bashrc $ cat .aliases # this is what is in my aliases alias ll='ls -alF' # ls everything with a lot of information alias la='ls -A' # ls everything alias l='ls -CF' # quick ls with the trailing / on directories alias lr='ls -la | grep [r-][w-][x-][r-][w-][x-]r[w-][x-]' # shows me everything readable by all alias lw='ls -la | grep [r-][w-][x-][r-][w-][x-][r-]w[x-]' # everything writeable by all alias lx='ls -la | grep [r-][w-][x-][r-][w-][x-][r-][w-]x' # everything executable by all # yes, the regex could be improved :) alias hd='hexdump' # because my fingers get tired when I type more than 2 chars alias tree="ls -R 2>/dev/null | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\// /g' -e 's/^/ /' -e 's/ /|/'" # I can't always install my own programs, so this emulates tree alias boxes='ssh 220.127.116.11 "netstat -T | grep \"box[^ :]*\" -o " | uniq | sort' # sometimes I want to know which boxes are onlineline. Naturally all boxes have an open connection to the print server. $ cd bin # Every user should have a bin directory somewhere, I keep my in home $ ls # this is where all the fun scripts go portscanner # a mass port scanner utility dist # runs a single command on all boxes online $ cd $ echo "PATH=$PATH:$HOME/bin" >> .bash_profile # need to add bin to my path so I can run dist without # having to write "~/bin/dist" $ logout Connection to gorman.cs.university.edu.au closed $ logout Connection to local.cs.university.edu.au closed
TAB autocomplete - will try and complete a command or input.
example: type "ech" and press tab
CTRL+R reverse command search - searches your history for the last usage of a string.
example: press CTRL+R then type "cd "
CTRL+A go to the begining of line
CTRL+E go to the end of the line
CTRL+U clear line after cursor
CTRL+K clear line before cursor
ESC+T swap the last 2 words before the cursor
example: type "chmod file +x" then hit ESC+T
CTRL+L clear the screen
UP previous command
DOWN next command
CTRL+C kill current process
CTRL+Z suspend current process
CTRL+D disconnect from current session
equivilent to logout command
CTRL+LEFT move left by one word
CTRL+RIGHT move right by one word
Other useful commands
piping a command into less gives you an easy way to read large blocks of text
example: cat a large file in to less; “cat BIGFILE | less”
sends a process to the background
brings a process to the foreground
stops a process for exiting when you disconnect
example: "./veryslowjob.pl", suspend with CTRL+Z, use "bg" to send it to background
then use "disown" to stop it dieing when you disconnect
constantly prints out "y"
if you have something that needs confirmation you can pipe yes in to it to prevent it hanging
opens the manual page for a command
example: man history
There are many guides on hotkeys and useful commands, this is not comprehensive, this is just what I use the most. For further reading consult google, and the man pages. Especially the man page for history.
Every time you type a command, it takes some thought, especially the big pipe lines like ls -R 2>/dev/null | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\// /g' -e 's/^/ /' -e 's/ /|/' Could you remember what that does? Could you remember the correct syntax? Aliases are super important. If you run a command more than twice, alias it in the local terminal. If you use it a few more times, add it to your .bashrc or .aliases
Every time you have to think about "oh how did I do that nice tree thing" you lose some focus on the task at hand. Your aliases are your weapons, and your bin directory is your artillery. Any useful script file should be chucked in there. A good arsenal makes any programmer more efficient.
Personally I divide my scripts in to finished polished scripts, these go in bin. My unfinished hackjobs go in a different directory. I usually have to have a --usage message, and proper argument parsing before I consider a script good enough for bin.
Naturally this is all my opinion, but hopefully you can find something within this crash course which you can apply to your workflow.