Menu

UNIX TUTORIALS - Unix - Quick Guide

Unix - Quick Guide

ADVERTISEMENTS

Filename Substitution:

CommandDescription
ls -[l]List Files in Current Directory
ls -[l]aList Hidden Files
~Home Directory
~userHome Directory of Another User
?Wild Card, matches single character
*Wild Card, matches multiple characters

ADVERTISEMENTS

Filename Manipulation:

CommandDescription
cat filenameDisplay File Contents
cp source destinationCopy source file into destination
mv oldname newnameMove (Rename) a oldname to newname.
rm filenameRemove (Delete) filename
chmod nnn filenameChanging Permissions
touch filenameChanging Modification Time
ln [-s] oldname newnameCreates softlink on oldname
ls -FDisplay information about file type.

ADVERTISEMENTS

CommandDescription
mkdir dirnameCreate a new directory dirname
rmdir dirnameDelete an existing directory provided it is empty.
cd dirnameChange Directory to dirname
cd -Change to last working directory.
cd ~Change to home directory
pwdDisplay current working directory.

Escape Characters:

Escape SequenceDescription
\tCurrent time, expressed as HH:MM:SS.
\dCurrent date, expressed as Weekday Month Date
\nNewline.
\sCurrent shell environment.
\WWorking directory.
\wFull path of the working directory.
\uCurrent user.s username.
\hHostname of the current machine.
\#Command number of the current command. Increases with each new command entered.
\$If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $.

Environment Variables:

VariableDescription
DISPLAY Contains the identifier for the display that X11 programs should use by default.
HOMEIndicates the home directory of the current user: the default argument for the cd built-in command.
IFSIndicates the Internal Field Separator that is used by the parser for word splitting after expansion.
LANGLANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.
LD_LIBRARY_PATH On many Unix systems with a dynamic linker, contains a colon-separated 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.
PATHIndicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands.
PWDIndicates the current working directory as set by the cd command.
RANDOMGenerates a random integer between 0 and 32,767 each time it is referenced.
SHLVLIncrements by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session.
TERM Refers to the display type
TZ Refers to Time zone. It can take values like GMT, AST, etc.
UIDExpands to the numeric user ID of the current user, initialized at shell startup.

CommandDescription
wc [-l] Word/Line Count
tail [-n] Displays last n lines from a file
sort [-n] Sort lines
pr -t Multicolumn Output
grep "pattern" filename Searching for a pattern with grep
pg or more Paginate a file content display.

VariableDescription
$0The filename of the current script.
$nThese 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 (the first argument is $1, the second argument is $2, and so on).
$#The number of arguments supplied to a script.
$*All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$?The exit status of the last command executed.
$$The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
$!The process number of the last background command.

Arithmetic Operators:

OperatorDescriptionExample
+Addition - Adds values on either side of the operator `expr $a + $b` will give 30
-Subtraction - Subtracts right hand operand from left hand operand `expr $a - $b` will give -10
*Multiplication - Multiplies values on either side of the operator`expr $a * $b` will give 200
/Division - Divides left hand operand by right hand operand`expr $b / $a` will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainder `expr $b % $a` will give 0
=Assignment - Assign right operand in left operand a=$b would assign value of b into a
==Equality - Compares two numbers, if both are same then returns true. [ $a == $b ] would return false.
!=Not Equality - Compares two numbers, if both are different then returns true. [ $a != $b ] would return true.

Relational Operators:

OperatorDescriptionExample
-eq Checks if the value of two operands are equal or not, if yes then condition becomes true. [ $a -eq $b ] is not true.
-ne Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.[ $a -ne $b ] is true.
-gt Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. [ $a -gt $b ] is not true.
-lt Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. [ $a -lt $b ] is true.
-ge Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. [ $a -ge $b ] is not true.
-le Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. [ $a -le $b ] is true.

Boolean Operators:

OperatorDescriptionExample
! This is logical negation. This inverts a true condition into false and vice versa. [ ! false ] is true.
-oThis is logical OR. If one of the operands is true then condition would be true.[ $a -lt 20 -o $b -gt 100 ] is true.
-aThis is logical AND. If both the operands are true then condition would be true otherwise it would be false.[ $a -lt 20 -a $b -gt 100 ] is false.

String Operators:

OperatorDescriptionExample
= Checks if the value of two operands are equal or not, if yes then condition becomes true. [ $a = $b ] is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.[ $a != $b ] is true.
-z Checks if the given string operand size is zero. If it is zero length then it returns true. [ -z $a ] is not true.
-n Checks if the given string operand size is non-zero. If it is non-zero length then it returns true. [ -z $a ] is not false.
str Check if str is not the empty string. If it is empty then it returns false. [ $a ] is not false.

File Test Operators:

OperatorDescriptionExample
-b file Checks if file is a block special file if yes then condition becomes true. [ -b $file ] is false.
-c file Checks if file is a character special file if yes then condition becomes true. [ -c $file ] is false.
-d file Check if file is a directory if yes then condition becomes true. [ -d $file ] is not true.
-f file Check if file is an ordinary file as opposed to a directory or special file if yes then condition becomes true. [ -f $file ] is true.
-g file Checks if file has its set group ID (SGID) bit set if yes then condition becomes true. [ -g $file ] is false.
-k file Checks if file has its sticky bit set if yes then condition becomes true. [ -k $file ] is false.
-p file Checks if file is a named pipe if yes then condition becomes true. [ -p $file ] is false.
-t file Checks if file descriptor is open and associated with a terminal if yes then condition becomes true. [ -t $file ] is false.
-u file Checks if file has its set user id (SUID) bit set if yes then condition becomes true. [ -u $file ] is false.
-r file Checks if file is readable if yes then condition becomes true. [ -r $file ] is true.
-w file Check if file is writable if yes then condition becomes true. [ -w $file ] is true.
-x file Check if file is execute if yes then condition becomes true. [ -x $file ] is true.
-s file Check if file has size greater than 0 if yes then condition becomes true. [ -s $file ] is true.
-e file Check if file exists. Is true even if file is a directory but exists. [ -e $file ] is true.

Variable Substitution:

FormDescription
${var}Substitue the value of var.
${var:-word}If var is null or unset, word is substituted for var. The value of var does not change.
${var:=word}If var is null or unset, var is set to the value of word.
${var:?message}If var is null or unset, message is printed to standard error. This checks that variables are set correctly.
${var:+word}If var is set, word is substituted for var. The value of var does not change.

CommandDescription
pgm > fileOutput of pgm is redirected to file
pgm < fileProgram pgm reads its input from file.
pgm >> fileOutput of pgm is appended to file.
n > fileOutput from stream with descriptor n redirected to file.
n >> fileOutput from stream with descriptor n appended to file.
n >& mMerge output from stream n with stream m.
n <& mMerge input from stream n with stream m.
<< tag Standard input comes from here through next tag at start of line.
|Takes output from one program, or process, and sends it to another.

Login Unix:

login : amrood
amrood's password:
Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
$

The if...fi statement:

if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

The if...else...fi statement:

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

The if...elif...fi statement:

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

The case...esac Statement:

case word in
  pattern1)
     Statement(s) to be executed if pattern1 matches
     ;;
  pattern2)
     Statement(s) to be executed if pattern2 matches
     ;;
  pattern3)
     Statement(s) to be executed if pattern3 matches
     ;;
esac

The while Loop:

while command
do
   Statement(s) to be executed if command is true
done

The for Loop:

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

The until Loop:

until command
do
   Statement(s) to be executed until command is true
done

The select Loop:

select var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

The break statement:

break [n]

The continue statement:

continue [n]

Command Substitution:

`command`

Variable Substitution:

$man command