Unix Quick Guide
Unix Quick Guide
Unix Quick Guide
http://www.tutorialspoint.com/unix/unix-quick-guide.htm
Copyright tutorialspoint.com
GETTING STARTED
What is Unix ?
The UNIX operating system is a set of programs that act as a link between the computer and the
user.
Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs, including
Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.
There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD
are few examples. Linux is also a flavor of Unix which is freely available.
Several people can use a UNIX computer at the same time; hence UNIX is called a multiuser
system.
A user can also run multiple programs at the same time; hence UNIX is called multitasking.
Login Unix
You can login to the system using login command as follows
login : amrood
amrood's password:
Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
$
Logging Out
When you finish your session, you need to log out of the system to ensure that nobody else
accesses your files while masquerading as you.
To log out
1. Just type logout command at command prompt, and the system will clean up everything and
break the connection
FILE MANAGEMENT
In UNIX there are three basic types of files
1. Ordinary Files: An ordinary file is a file on the system that contains data, text, or program
instructions. In this tutorial, you look at working with ordinary files.
2. Directories: Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, UNIX directories are equivalent to folders.
3. Special Files: Some special files provide access to hardware such as hard drives, CD-ROM
drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts
and enable you to access a single file using different names.
Filename Substitution
Command
Description
ls -[l]
ls -[l]a
Home Directory
~user
Filename Manipulation
Command
Description
cat filename
cp source destination
mv oldname newname
rm filename
Changing Permissions
touch filename
ls -F
DIRECTORY MANAGEMENT
Command
Description
mkdir dirname
rmdir dirname
cd dirname
cd -
cd ~
pwd
ENVIRONMENT SETUP
When you type any command on command prompt, the shell has to locate the command before it
can be executed. The PATH variable specifies the locations in which the shell should look for
commands.
Escape Characters
Escape Sequence
Description
\t
\d
\n
Newline.
\s
\W
Working directory.
\w
\u
\h
\#
\$
Environment Variables
Following is the partial list of important environment variables. These variables would be set and
accessed as mentioned above
Variable
Description
DISPLAY
Contains the identifier for the display that X11 programs should use by
default.
HOME
Indicates the home directory of the current user: the default argument
for the cd built-in command.
IFS
Indicates the Internal Field Separator that is used by the parser for
word splitting after expansion.
LANG
LD_LIBRARY_PATH
On many Unix systems with a dynamic linker, contains a colonseparated list of directories that the dynamic linker should search for
shared objects when building a process image after exec, before
searching in any other directories.
PATH
PWD
RANDOM
SHLVL
TERM
TZ
Refers to Time zone. It can take values like GMT, AST, etc.
UID
Description
wc [-l]
Word/Line Count
tail [-n]
sort [-n]
Sort lines
pr -t
Multicolumn Output
grep "pattern"
filename
pg or more
SPECIAL VARIABLES
Variable
Description
$0
$n
These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an
argument thefirstargumentis$1, thesecondargumentis$2, andsoon.
$#
$*
All the arguments are double quoted. If a script receives two arguments,
isequivalentto1 $2.
$@
All the arguments are individually double quoted. If a script receives two
arguments, @isequivalentto1 $2.
$?
$$
The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.
$!
Operator
Description
Example
==
!=
Relational Operators
Assume variable a holds 10 and variable b holds 20 then
Operator
Description
Example
-eq
-ne
[ a neb ] is true.
-gt
-lt
[ a ltb ] is true.
-ge
-le
[ a leb ] is true.
Boolean Operators
Assume variable a holds 10 and variable b holds 20 then
Operator
Description
Example
[ ! false ] is true.
-o
-a
String Operators
Assume variable a holds "abc" and variable b holds "efg" then
Operator
Description
Example
[ a = b ] is not true.
!=
[ a! = b ] is true.
-z
[ -z $a ] is not true.
-n
Checks if the given string operand size is nonzero. If it is non-zero length then it returns
true.
[ -z $a ] is not false.
str
[ $a ] is not false.
Operator
Description
Example
-b file
[ -b $file ] is false.
-c file
[ -c $file ] is false.
-d file
-f file
[ -f $file ] is true.
-g file
[ -g $file ] is false.
-k file
[ -k $file ] is false.
-p file
[ -p $file ] is false.
-t file
[ -t $file ] is false.
-u file
[ -u $file ] is false.
-r file
[ -r $file ] is true.
-w file
[ -w $file ] is true.
-x file
[ -x $file ] is true.
-s file
[ -s $file ] is true.
-e file
[ -e $file ] is true.
]
be executed if expression 1 is true
2 ]
be executed if expression 2 is true
3 ]
be executed if expression 3 is true
be executed if no expression is true
SHELL SUBSTITUTIONS
The shell performs substitution when it encounters an expression that contains one or more
special characters.
Command Substitution
The command substitution is performed when a command is given as
`command`
Variable Substitution
Form
Description
${var}
${var:-word}
If var is null or unset, word is substituted for var. The value of var
does not change.
${var:=word}
${var:?message}
${var:+word}
If var is set, word is substituted for var. The value of var does not
change.
REDIRECTION COMMANDS
Following is the complete list of commands which you can use for redirection
Command
Description
n > file
n >> file
n >& m
n <& m
<< tag
Standard input comes from here through next tag at start of line.