Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Basic  Linux/Unix 

Commands

Somsak Ketkeaw
www.aoddy.com
August 9 ­ 10, 2008
Outline
Introducing the BASH Shell.
­ What Is the BASH Shell?
­ Getting Started with the Shell
­ Running Programs
­ Getting Help
­ Working with Files
­ All basic command such as cp, rm, more, less etc.
Understanding Linux Files and Users.
­ Users and File Permissions
­ The File System Explained
­ File Size and Free Space
Working with text file
How to use VI Editor.
­ How to use command & user mode.
Introducing the BASH shell
What Is the BASH Shell?

BASH = Bourne Again Shell 

“Born Again concept”

Bourne Shell was created by 
Stephen Bourne 
In 1978 (2521) *

Bash shell was created by 
Brain Fox 
in 1987 (2530) *
Brain Fox

* http://en.wikipedia.org/wiki/Bash
Why do you use the Shell?

It's simple & fast.
It’s versatile. 
It’s consistent among distributions.
It’s crucial for troubleshooting. 
It’s useful for remote access.
It’s respected in the community. 
Getting started with the shell. 
Applications 
   ➤ Accessories 
       ➤ Terminal
Detail of shell prompt.

aoddy@aoddy­lnx: ~$

Ordinary user

Current Path (pwd)

Host name (hostname)

User name (id)
Getting help!!.
Where is command ?
      $ whereis <command>
Ex. $ whereis gnome­terminal   
What is this command ?
      $ whatis <command>
Ex  $ whatis whatis
How to use this command ?
      $ man <command> 
Ex   $ man man
      $ <command> ­­help
Ex  $ man ­­help
How to search command ?
      $ man ­k <text for search>
Ex  $ man ­k “list dir”
Working with Files.
List file : ls
     $ touch axxx.txt bxxx.txt cxxx.txt dxxx.txt
     $ ls ­lh [a­b]xxx.txt
Copy file : cp
Move file&directory : mv
     When you move files that have spaces in their name, you have 2 
solutions to solve this problem.
     1. enclose the filename in quotation marks (")
     2. add backslash (\) before spaces.
Delete file&directory : rm
Change & create directory : cd & mkdir
Create alias command : alias
      Temporary  : alias <new_command> <old_command>
      Permanent : edit in file .bashrc 
Understanding Linux Files
and Users
Users and File Permissions
Changing permission.
Default permission
Umask : 022
File : 644
Directory : 755
You can change default permission at 'umask' :
All user : /etc/profile
Current user : ~/.bash_profile
Example
if umask = 022
permissions for the user are 0 = 000 binary
     read allowed, write allowed, execute allowed 
permissions for the group are 2 = 010 binary
     read allowed, write NOT allowed, execute allowed 
permissions for others are 2 = 010 binary
     read allowed, write NOT allowed, execute allowed#
If the umask is 022, a new directory will have permissions: rwxr­xr­x
If the umask is 022, a new file will have permissions: rw­r­­r­­
(the execute permissions are not turned on automatically) 
** http://floppix.ccai.com/umask.html
Changing permission (2).
Change by number :
Example :
$ chmod 777 aoddy.txt

Change by character :
Example :
$ chmod a+r aoddy.txt
$ chmod a­r aoddy.txt
$ chmod g+w aoddy.txt
$ chmod g­w aoddy.txt
$ chmod u+x aoddy.txt
$ chmod u­x aoddy.txt
** If you leave out the 'a', chmod assumes you mean 'all'.
Changing owner.
You can change owner by :
$ chown [user]:[group] file

Example :
$ rm ­rf aoddy.txt
$ sudo groupadd newgroup
$ touch aoddy.txt
$ ls ­l aoddy.txt
$ sudo chown aoddy:newgroup aoddy.txt
$ ls ­l aoddy.txt
The File System Explained.

Directory Contents
bin Vital tool necessary to get the system running.
boot Boot loader program
dev Virtual file representing hardware installed on system.
etc Center repository configure file system.
home Where each user's personal directory is stored.
proc Virtual directory containing data about your system.
root The root's personal directory.
sbin Program for administrator.
opt Software that is theoretically optional and not vital to running of the system.
var Used by system to store data such as log.
usr Program and data that might be shared with other systems.
File Searches.
You can search file by these command :

1. find <path> [­option] “myfile”
Example
$ find /home/aoddy ­name aoddy.txt

2. locate “myfile”
Example
$ locate aoddy.txt

** Sometime you should update data by command 'updatedb'
before you search by command 'locate'
Example
$ sudo updatedb
File Size and Free Space.
You can check your size of files or disks by : 

1. ls [option]
Example
$ ls ­lh * 

2. du [option]
Example
$ du ­sh *

3. df [option]
Example
$ df ­h 
Working with
Text file
Viewing Text Files.
You can view detail of text file by :

1. cat myfile 4. head myfile
Example Example
$ cat /etc/password $ head /etc/password

2. more myfile 5. tail myfile
Example Example
$ cat /etc/password $ tail /etc/password

3. less myfile
Example
$ less /etc/password
Searching Through Files.
1. Grep command : 
$ grep “abc” aoddy.txt Exercise # 2
$ grep ­r “abc” * $ cat a1.txt
$ grep ­v “abc” aoddy.txt abc
$ grep “^abc” aoddy.txt    123
$ grep “abc$” aoddy.txt    def
   456
Exercise # 1
$ mkdir Exercise/sl20/    $ cp a1.txt a2.txt;
$ cd Exercise/sl20/    $ cp a1.txt a3.txt
$ touch a1.txt a2.txt a3.txt    $ grep 123 a[1­2].txt
$ ls ­l | grep “a[1­2].txt”    $ grep ­r 123 a[1­2].txt
   $ grep 123 a?.txt
$ grep “^def” *.txt
$ grep “456$” *.txt
Exercises # 1.
1. I would like to know how many lines which have not “dev” 
word in file /etc/passwd.

2. I would like to change default permission of files to 640  & 
directories to 751when they are created, please help me.
How to use
VI Editor.
History of VI.
VI is text editor written by Bill Joy
in 1976 (2519)*

Bill Joy
ADM3A

* http://en.wikipedia.org/wiki/Vi
Mode of VI.

VI has 3 modes for user :
1. Command mode.
2. Insert mode (Normal Mode).
3. Command line mode.
VI mode topology.
Open VI Editor

Press 'Esc' key Press 'Esc' key
Command Mode

Press ':' key Press 'i' key

Command­Line Mode Insert Mode
Command mode.
Open your vi editor on your terminal :
1. $ vi SL25.txt.
2. Press 'Ese' key.
3. Press '2' & '0' key.
4. Press 'i' key.
5. Type 'I love Naresuan University, Phitsanulok,Thailand.'
6. Press 'Enter' key.
7. Press 'Esc' key.
8. Type ':wq' .
9. Press 'Enter' key.
Command mode. (2)
Delete Text
dd Delete current line
ndd Delete n number of lines (for example, 5dd will delete five lines)
dw Delete the current word under the cursor 
db Delete the word before the cursor2
D Delete everything from the cursor to the end of the line1
Search
/ Search forward (type the search text directly after the slash)
? Search backward
n Repeat search in a forward direction
N Repeat search in a backward direction
Cut and Paste
yy Copy the current line
nyy Copy n number of lines into the buffer from the cursor downwards
             (for example, 5yy copies five lines of text)
p Paste the contents of the clipboard
Command mode. (3)
Insert Text
i Switch to Insert mode at the cursor
o Switch to Insert mode, placing the cursor below current line
O Switch to Insert mode, placing the cursor above current line
A Append text to end of line

Navigation
$ Move the cursor to the end of the current line
w Move the cursor to the next word
b Move the cursor to beginning of the current or previous word

Miscellaneous
. Repeat the last command
u Undo the last command
Insert mode.

Like you use notepad program on Windows.
It's very easy to use!!!.
You can change to insert mode, press 'i' key.
To quit insert mode, press 'Esc' key.  
Command­line mode.
You can enter Command line mode by typing 
a colon (:), if you are in Insert mode, you will first 
need to leave it by pressing the 'Esc' key.

Help you to : 
1. Save file.
2. Load file.
3. Search text.
4. Etc. such as set line number, replace text. 
Command­line mode. (2)
1. Set line number by :
:set nu

2. Copy text between line 'x' with 'y' and paste below line 'z' 
:x,y copy z

3. Put '#' at first string between line 'x' with 'y'.
:x,y s/^/#/

4. Search 'string­A' and replace with 'string­B'
:%s/string­A/string­B/g
Exercises # 2.
1. I would like to know amount of free and used memory in your system. 
Please create a script so as to show that information.

2. From item#1, I would like to know what time which you run that script. 
Please edit the script and add a command so as to show the time.
Homework.
Hey!!  boys&girls.  Tonight,  I  will  have  a  dinner  with  my  GF  but  my 
boss would like to know amount of free and used memory on his server 
at 11.00 PM.
Sure,  I  need  your  script  from  Exercise#2  to  help  me  but  I  want  to 
start it automatic. How I do? Please help me !!! :( .
The end.

You might also like