Quick notes on UNIX commands.

Shells

Unix provides shells that allow a user to interface with the "kernel" (lower level of the operating system). Unix shells are traditionally Command Line Interfaces (CLI), but can be GUIs (Graphical User Interface) too.

Here are some of the shells available for Unix, in rough historical order and complete with the original bad puns:

In one sense X Windows System of Unix, the Window OS of Microsoft, and the Mac OS, are all GUI "shells". Apple/Mac uses AppleScript, but MacOS X uses bash. Microsoft Windows users will compare the Unix shell to DOS or the Windows CLI opened up via command.com or cmd.exe. Windows shell scripts are wsh. The DOS prompt usually ends with >.

Concepts

Key Commands

For a full directory of commands see http://linux.oreillynet.com/linux/cmd/.

Unix is not DOS! EG:

DIRECTORY COMMANDS
cd      Change the working directory. EGs:
        To go to the user's home dir: cd ~
        In Cygwin to go to c: dir: cd c:
find    Find a file by name or by other characteristics
mkdir   Make a directory
rmdir   Remove a directory
FILE MANIPULATION COMMANDS
cat                 Concatenate and display a file
chmod octal file    Change the permissions mode of a file
    4   read (r)
    2   write (w)
    1   execute (x)
    order: owner/group/world. EGs:
        chmod 777   rwx for everyone
        chmod 755   rw for owner, rx for group/world
chown               Change the owner and/or group of a file
cp                  Copy a file
diff                Display differences between pairs of text files
mv                  Move or rename a file
rm                  Remove a file
sort                Sort lines of text
SEARCH COMMANDS
grep pattern files      Search for pattern in files
grep -r patter dir      Search recursively for patter in dir
cmd | grep pattern      Search for pattern in output of the cmd
locate file             Search for all instances of file
DISPLAY COMMANDS
finger  Display information about a user
head    Display the first few lines of a file
less    Browse a text file. h for help. :q to quit.
ls      List the contents of a directory
man     Display a reference manual page
more    Display a text file
pwd     Display the working directory pathname
tail    Display the end of a file
who     Display who is on the system
PROCESS COMMANDS
^c              Halts current command
^z              Stops current command
kill pid        Terminate or send a signal to process pid
killall proc    Kill all processes named proc
passwd          Create or change a password
ps              Display the status of active processes
ps aux          ps with more details
bg              Resumes stopped command in the background
fg              Resumes stopped command in the foreground
fg n            Bring job n to foreground
telnet          Connect to a remote system using the Telnet protocol
exit            Exit current session
sudo commnad    Do/execute the command as the superuser (/etc/sudoers)
                for 5 minutes. Unauthorized attempts emails admins.
SSH COMMANDS
ssh user@host           Connect to host as user
ssh -p port user@host   Connect using port p
ssh -D port user@host   Connect and use bind port p
NETWORK COMMNADS
ping host       ping host
whois domain    get whois for domain
dig domain      get DNS for domain
dig -x host     reverse lookup host
wget file       download file
wget -c file    continue stopped download
wget -r url     recursively download files from url
SYSTEM INFO
date        Get the date and time
cal         Get this month's calendar
uptime      Get uptime
w           Who is online
whoami      Who you are logged in as
uname -a    Show kernel config
cat /proc/cpuinfo   CPU info
cat /proc/meminfo   Memory info
man cmd     Show manual for command
df          Show disk usage
du          Show directory space usage
du -sh      Show readable size in GB
free        Show memory and swap usage
whereis app Show possible locatios of app
which app   Show which app will be run by default
COMPRESSION COMMANDS
tar cf file.tar files   tar files into file.tar
tar xf file.tar         untar into current directory
tar tf file.tar         show contents of archive (arc)
    tar flags:
        c   Create arc          j   Bzip2 compression
        t   Table of contents   k   Do not overwrite
        x   Extract             T   Files from file
        f   Specifies filename  w   Ask for confirmation
        z   Use zip/gzip        v   Verbose
gzip file           Compress file and rename to file.gz
gzip -d file.gz     Decompress file.gz
COMMAND MODIFIERS
<       input. EG: sort < test.txt
<<      here. EG: cat > foo.txt << MYSTOP
>       overwrite. EG: cat > newTarget.txt
>>      append. EG: cat >> oldTarget.txt
|       pipe. EG: cat student.txt|head
-       parameter EG: cat student.txt|head -5
SHORTCUTS
ERASE   ^h      BS          Erase 1 character
KILL    ^u      NAK         Erase 1 line
        ^w                  Erase 1 word
NL      ^j      LF          Pass input
EOF     ^d      EOT         Conclude input
STOP    ^s      DC3/XOF     Temporary stop
START   ^q      DC1/XON     Resume (or any key)
        ^d      exit        Log out of current session
        ^r                  Reverse look up of preveious commands
!!                          Repeat last command (up arrow too)
INTR    Del     DEL         Interrupt all
QUIT    ^\      FS          As INTR but saves copy
EOL     ^'      NUL
/sbin/shutdown -r now   Reboot the computer (alt-ctrl-del)
/sbin/shutdown -h now   Shutdown (halt) the computer
halt                    Shutdown (halt) the computer

vi

Emacs and vi compete for the position of most common common text editor for UNIX. vi is a screen oriented text editor written by Bill Joy in 1976 for Unix.

Offsite links related to vi:

vi has 2 modes:

To open vi:

vi file         Open/make file in Command mode
vi +n file      Open file at line n
vi -r file      Recover file as it was when system crashed.
vi --version    To see what version you have from the CLI

For help (in Command mode):

:help       Help
:help cmd   Help on cmd
:q          Quit out of Help
:version    To see what version you have from within vi

To quit, save (in Command mode):

:q              Quit. Will fail if unsaved changes.
:q!             Quit ignoring any unsaved changes
:wq             Save then quit
:x              Save then quit
ZZ              Save then quit
:w              Save (write)
:w file         Save as file. Will fail if file exists.
:w! file        Save as file, even if file exists.
:12,28 file     Save lines 12 to 28 as file
:12,28 >> file  Append lines 12 to 28 to file
:r file         Read file and insert after cursor
:n              Go to next file
:p              Go to previous file
:e file         Edit file
:e +N file      Edit file at line N
!!cmd           Replace line with output of cmd

To navigate (in Command mode):

h       Left
BS      Left
ARROW   Left

j       Down (hooks down)
RETURN  Down
ARROW   Down

k       Up
ARROW   Up

l       Right
SPACE   Right

0       Start of line
HOME    Start of line
^       Start of line
$       End of line
END     End of line
+       Next line
-       Previous line

w       Next word
W       Next word delimited by blank
b       Previous word
B       Previous word delimited by blank
e       End of next word
E       End of next word delimited by blank

:0      First line. Should have been :0
1G      First line
^HOME   First line
:n      Line n
nG      Line n
:$      Last line
G       Last line, start
^END    Last line, end

(       Previous sentence
)       Next sentence
{       Previous paragraph
}       Next paragraph

^f      Forward a screen
^b      Back a screen
^d      Down half screen
^u      Up half screen
^l      Redraw screen
^r      Redraw screen removing deleted lines

H       Head of screen
M       Middle of screen
L       Last of screen

%       Find matching ), ], or }.

fc          Find 'c' forward
Fc          Find 'c' backward

/string     Search forward for string
?string     Search backward for string
n           Repeat search in current direction
N           Repeat in opposite direction

mX      Marks line as X where X is any lowercase character
`X      Go to start of line marked X
'X      Go to 1st non-blank character of line marked X
``      Return to previous location
''      Return to previous line

To edit (from Command mode to Insert mode)

i       Insert before cursor
I       Insert before line
a       Append after cursor
A       Append after line
o       Open a new line after line
O       Open a new line before line

r       Replace 1 character
R       Replace characters
cw      Change current word
cNw     Change N current words
C       Change rest of line
c$       Change rest of line
cc      Change entire current line
Ncc     Change N current lines
cNc     Change N current lines

^z      Undo
u       Undo
U       Undo all changes to line
^r      Redo
^y      Redo

x       Delete 1 character at cursor
X       Delete 1 character before cursor
Nx      Delete N characters
dw      Delete word starting at cursor
dNw     Delete N words starting at cursor
D       Delete rest of line
d$      Delete rest of line
dd      Delete entire current line
:d      Delete entire current line
Ndd     Delete N current lines
dNd     Delete N current lines

:y      Copy (yank) current line
yy      Copy (yank) current line
Nyy     Copy (yank) N current lines
yNy     Copy (yank) N current lines
p       Paste (put) before cursor
P       Paste (put) after cursor

:s/pattern/string/flags     Regex replace pattern with string according to flags
:N,Ms/pattern/string/flags  Regex replace from line N to M
:%s/pattern/string/flags    Regex replace in whole file
    flags:
        g   All occurrences
        c   Confirm replaces
&                    Repeat last :s command

J       Join lines
xp      Switches 2 chars
ddp     Switches 2 lines
~       Toggle upper and lowercase
.       Repeast last text-changing command

Other Commands:

:.=     Show current line #
:=      Show total line count
^g      Show current filename, total line count, relative position of current

:!cmd   Execute external cmd. EG: :!mkdir NewDirectory.

Miscellany

Until 2003, Unix could run on 64 bit CPUs while Microsoft Windows could only run on 32 bit CPUs.

More than 50% of web servers are Unix-based Apache.org web servers. Microsoft's IIS web server comes in second.

Page Modified: (Hand noted: ) (Auto noted: )