A glimpse at Unix Commands

Working in a terminal shell on a command line sounds like heaven for some developer, but hell for some others. Even though I would consider myself more technical than creative, i.e. more programmer than designer, it did quite happen a lot in the past that opening a terminal window (whether it was on Mac OSX or any other Unix-based machine) caused an attack of sweating. Well, I am exaggerating. But still, when I watched “the gurus” using the command line, I was always astonished how they could all keep all the myriads of commands and options in their heads. It is just a matter of practice and experience I guess. But if you don’t have that much of an experience, like I do, the following “cheat sheet” might help to get familiar with it and keep one or the other useful command in your head for the future.

Files and Directories

Change directory

cd [directory]

Examples

  • cd ~ takes you to your users home directory

Resources

Create new directory

mkdir [dirname]

Examples

  • mkdir homedir creates a new directory ‘homedir’

Resources

Delete a directory

rmdir [option] [directory]

Examples

  • rmdir homedir remove the directory ‘homedir’
  • rm -r directory remove a directory, even if files existed in that directory

Resources

Move file f to directory d

mv f1 [f2...] d

Examples

  • mv myfile.txt newdirectory/ moves the file myfile.txt to the directory newdirectory

Resources

Rename directory d1 as d2

mv d1 d2

Examples

  • mv alpha beta renames directory ‘alpha’ to ‘beta’

Resources

Delete a file

rm [-f] [-i] [-R] [-r] [filenames | directory]

Examples

  • rm myfile.txt removes the file myfile.txt without prompting the user
  • rm -rf /tmp/* removes all files (including write-protected ones) within /tmp recursively (including directories)

Resources

List contents of directory

ls [options] [pathnames]

Examples

  • ls -a shows you all files, even files that are hidden (these files begin with a dot.)
  • ls -d */ only list the directories in the current directory
  • ls -lsa shows additional information about files (permission, size, hidden files, blocks etc.)
  • ls -1 lists the contents with one entry on each line (without any add. info)

Resources

Create symbolic link

ln -s [destination] [source]

Examples

  • ln -s ~/dev/myproject/htdocs . symlinks current directory to stated directory
  • ln -s /export/space/common/archive /archive create a symlink from…to…
    (speak: “/archive is gonna link to /export/space/common/archive”)

Resources

Environment Information

OS information

uname -a

Change password

passwd

Create command alias (csh/tcsh)

alias name1 name2

Create command alias (ksh/bash)

alias name1="name2"

Remove command alias na

unalias name1[na2...]

Login securely to remote node

ssh nd

End terminal session

exit

Set env var to value v (csh/tcsh)

setenv name v

set environment variable to value v (ksh/bash)

export name="v"

execute a file as a tcl script

source

Examples

  • source ~/.profile executes .profile in user home directory

Search

Search for Text in Files

grep [options] PATTERN [FILE...]

Examples

  • grep -r "class='headline'" /home/john/test searches for content “class=’headline’” in files
  • grep -r "body" . searches for “body” in current directory

Search for Files

find [path] [expressions]

Examples

  • find / -name "php.ini" searches for php.ini in the root and all subdirs

Resources

Process Control

Sleep for n seconds

sleep n

Print list of jobs

jobs

Kill job n

kill %

Print process status stats

ps

Remove process n

kill -9 n

Suspend background job n

stop %n

Display tasks/processes

top

Run cmmd in background

cmmd&

Resume background job n

bg [%n]

Resume foreground job n

fg [%n]

Exit from shell

exit

Environment Status

Display command aliases

alias [name]

Print environment values

printenv [name]

Display disk quota

quota

Display the available disk space for each mount

df

Print date & time

date

List logged in users

who

Display current user

whoami

Output user information

finger [username]

Change finger information

chfn

Print working directory

pwd

Display recent commands

history

Submit recent command n

! n

Get current network adapter details (e.g. ip address)

ifconfig -a

File Manipulation

Vi fullscreen editor

vi [f]

Emacs fullscreen editor

emacs [f]

Text editor

ed [f]

Line, word, & char count

wc f

List contents of file

cat f

List file contents by screen

more f

Concatenates f1 & f2 into f3

cat f1 f2 >f3

Change protection mode of f

chmod mode f

Compare two files

cmp f1 f2

Copy file f1 into f2

cp f1 f2

Alphabetically sort f

sort f

Split f into n-line pieces

split [-n] f

Rename file f1 as f2

mv f1 f2

Delete (remove) file f

rm f

Outputs lines that match ptn

grep 'ptn' f

Lists file differences

diff f1 f2

Output beginning of f

head f

Output end of f

tail f

Examples

  • tail -n 1000 /home/log/apache/error | grep index looks for “index” in the last 1000 lines of apache error log

Resources

About the author

This website is the personal web appearance of Klaus Komenda, an Austrian web developer. The about section offers more info about me and this site.

Categories

Copyright © 2000-2014 Klaus Komenda