UNIX TUTORIALS - Unix - Quick Guide
Unix - Quick Guide
ADVERTISEMENTS
Filename Substitution:
Command | Description |
---|---|
ls -[l] | List Files in Current Directory |
ls -[l]a | List Hidden Files |
~ | Home Directory |
~user | Home Directory of Another User |
? | Wild Card, matches single character |
* | Wild Card, matches multiple characters |
ADVERTISEMENTS
Filename Manipulation:
Command | Description |
---|---|
cat filename | Display File Contents |
cp source destination | Copy source file into destination |
mv oldname newname | Move (Rename) a oldname to newname. |
rm filename | Remove (Delete) filename |
chmod nnn filename | Changing Permissions |
touch filename | Changing Modification Time |
ln [-s] oldname newname | Creates softlink on oldname |
ls -F | Display information about file type. |
ADVERTISEMENTS
Command | Description |
---|---|
mkdir dirname | Create a new directory dirname |
rmdir dirname | Delete an existing directory provided it is empty. |
cd dirname | Change Directory to dirname |
cd - | Change to last working directory. |
cd ~ | Change to home directory |
pwd | Display current working directory. |
Escape Characters:
Escape Sequence | Description |
---|---|
\t | Current time, expressed as HH:MM:SS. |
\d | Current date, expressed as Weekday Month Date |
\n | Newline. |
\s | Current shell environment. |
\W | Working directory. |
\w | Full path of the working directory. |
\u | Current user.s username. |
\h | Hostname 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:
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 | LANG 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. |
PATH | Indicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands. |
PWD | Indicates the current working directory as set by the cd command. |
RANDOM | Generates a random integer between 0 and 32,767 each time it is referenced. |
SHLVL | Increments 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. |
UID | Expands to the numeric user ID of the current user, initialized at shell startup. |
Command | Description |
---|---|
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. |
Variable | Description |
---|---|
$0 | The filename of the current script. |
$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 (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:
Operator | Description | Example |
---|---|---|
+ | 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:
Operator | Description | Example |
---|---|---|
-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:
Operator | Description | Example |
---|---|---|
! | This is logical negation. This inverts a true condition into false and vice versa. | [ ! false ] is true. |
-o | This is logical OR. If one of the operands is true then condition would be true. | [ $a -lt 20 -o $b -gt 100 ] is true. |
-a | This 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:
Operator | Description | Example |
---|---|---|
= | 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:
Operator | Description | Example |
---|---|---|
-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:
Form | Description |
---|---|
${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. |
Command | Description |
---|---|
pgm > file | Output of pgm is redirected to file |
pgm < file | Program pgm reads its input from file. |
pgm >> file | Output of pgm is appended to file. |
n > file | Output from stream with descriptor n redirected to file. |
n >> file | Output from stream with descriptor n appended to file. |
n >& m | Merge output from stream n with stream m. |
n <& m | Merge 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 fiThe 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 fiThe 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 fiThe 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 ;; esacThe while Loop:
while command do Statement(s) to be executed if command is true doneThe for Loop:
for var in word1 word2 ... wordN do Statement(s) to be executed for every word. doneThe until Loop:
until command do Statement(s) to be executed until command is true doneThe select Loop:
select var in word1 word2 ... wordN do Statement(s) to be executed for every word. doneThe break statement:
break [n]The continue statement:
continue [n]Command Substitution:
`command`Variable Substitution:
$man command