Docstoc

Tutorial Linux Commands

Document Sample
Tutorial Linux Commands Powered By Docstoc
					1 Practice Linux command

1.1 Login &logout

You can login to a node of your experiment. Before that, you need to get the user name and password
from your system administrator. You can use ssh client to login to the node, and should see something
like the following:
login as:user
Using username "user".
Using keyboard-interactive authentication.
Password: (input your password here)
Linux node0.homework2.sjsucmpe209.isi.deterlab.net 2.6.24-23-deter #4 SMP Wed Jan 21 23:15:52 MST 2009
i686
Last login: Wed Aug 18 22:52:55 2010 from users.isi.deterlab.net
user@node0:~$
After finished your operation, you need to logout from the system. You can use ether “logout” or
“exit” to do that, so that your command history can be recorded correctly. Then you can use the
command “history” to find the command that you have used. This tutorial will introduce
command “history” in latter section.
user@node0:~$logout
Or
user@node0:~$exit



1.2 Get Help information and manual of the Commands

If you don’t familiar the command you want to use, then there are several ways to get the help.


1.2.1 Use “-h” or “--help” option

Most of the Linux commands have option “-h” or “—help” option. You can input the command
followed by “-h” or “—help” to get the usage information. Ex.:
user@node0:~$ man --help
Usage: man [OPTION...] [SECTION] PAGE...

  -C, --config-file=FILE  use this user configuration file
  -d, --debug                  emit debugging messages
  -D, --default              reset all options to their default values
        --warnings[=WARNINGS] enable warnings from groff

 Main modes of operation:
  -f, --whatis               equivalent to whatis
  -k, --apropos               equivalent to apropos
  -l, --local-file        interpret PAGE argument(s) as local filename(s)
  -w, --where, --location     print physical location of man page(s)
  -W, --where-cat, --location-cat
                                      print physical location of cat file(s)

  -c, --catman                     used by catman to reformat out of date cat pages
  -R, --recode=CODE                output source page encoded in CODE

 Finding manual pages:
  -e, --extension=EXTENSION limit search to extension type EXTENSION
  -L, --locale=LOCALE        define the locale for this particular man search
  -m, --systems=SYSTEM         search for man pages from other unix system(s)
  -M, --manpath=PATH            set search path for manual pages to PATH
  -S, -s, --sections=LIST use colon separated section list

  -i, --ignore-case             look for pages case-insensitively (default)
  -I, --match-case                look for pages case-sensitively

  -a, --all                       find all matching manual pages
  -u, --update                     force a cache consistency check

 Controlling formatted output:
  -P, --pager=PAGER            use program PAGER to display output
  -r, --prompt=STRING          provide the `less' pager with a prompt

  -7, --ascii                    display ASCII translation of certain latin1 chars
  -E, --encoding=ENCODING          use the selected nroff device and display in pager

  -p, --preprocessor=STRING      STRING indicates which preprocessors to run:
                                      e - [n]eqn, p - pic, t - tbl,
                                      g - grap, r - refer, v - vgrind

  -H, --html[=BROWSER]          use www-browser or BROWSER to display HTML output
  -t, --troff                 use groff to format pages
  -T, --troff-device[=DEVICE]  use groff with selected device
  -X, --gxditview[=RESOLUTION]     use groff and display through gxditview
                                    (X11):
                                    -X = -TX75, -X100 = -TX100, -X100-12 = -TX100-12
  -Z, --ditroff               use groff and force it to produce ditroff

  -?, --help                       give this help list
        --usage                      give a short usage message
  -V, --version                   print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to cjwatson@debian.org.



1.2.2 Use “man”

You can use command “man” to get a manual of a command. For example, you can use the
following command to get the usage information about “men” itself:
user@node0:~$man man
MAN(1)
Manual                                                     pager                        utils
MAN(1)



NAME
         man - an interface to the on-line reference manuals

SYNOPSIS
           man [-c|-w|-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-adhu7V] [-i|-I] [-m system[,...]] [-L locale] [-p
string]
-C file] [-M path] [-P pager] [-r prompt] [-S list] [-e exten▒▒▒
           sion] [--warnings [warnings]] [[section] page ...] ...
           man -l [-7] [-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-p string] [-P pager] [-r prompt] [--warnings[warni_
ngs]] file ...
           man -k [apropos options] regexp ...
           man -f [whatis options] page ...

DESCRIPTION
         man is the system’s manual pager. Each page argument given to man is normally the name of a program,
utility or function. The manual page associated with each of these argu‐
           ments is then found and displayed. A section, if provided, will direct man to look only in that section
of the manual. The default action is to search in all of the available
           sections, following a pre-defined order and to show only the first page found, even if page exists in
several sections.

         The table below shows the section numbers of the manual followed by the types of pages they contain.


         1    Executable programs or shell commands
         2    System calls (functions provided by the kernel)
         3    Library calls (functions within program libraries)
         4    Special files (usually found in /dev)
         5    File formats and conventions eg /etc/passwd
         6    Games
         7    Miscellaneous (including macro packages and conventions),
              e.g. man(7), groff(7)
         8    System administration commands (usually only for root)
         9    Kernel routines [Non standard]
……



1.3 sudo

The command “sudo” will temporally give you the root privilege so that you can execute
command requiring root privilege, such as the “apt-get install” command which will install
software. You can add “sudo” before the command with its argument. For example, we want to
upgrade the installed software, without sudo, it fails:
user@node0:~$ apt-get upgrade
E: Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?


After adding sudo(in some cases you need input your password to allow executing of sudo):
user@node0:~$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages have been kept back:
  bind9-host dnsutils libbind9-30 libdns35 libisc35 libisccfg30 liblwres30
The following packages will be upgraded:
  base-files dpkg dselect expat grub gzip klibc-utils language-pack-en language-pack-en-base libc6 libc6-dev
libc6-i686 libexpat1 libexpat1-dev libisccc30 libklibc libkpathsea4 libkrb53
  libmysqlclient15off libpng12-0 libssl-dev libssl0.9.8 linux-libc-dev mysql-client mysql-client-5.0 mysql-common
nfs-common ntp ntpdate openssl python2.5 python2.5-dev python2.5-minimal
  samba-common smbclient sudo texlive-base-bin tzdata
38 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
Need to get 47.2MB of archives.
After this operation, 414kB of additional disk space will be used.
Do you want to continue [Y/n]?



1.4 Install software

If there’s some software you want to use but it is not installed yet, then you can use the command
“apt-get” with “install” option to install it. If you cannot remember the exact name of the software, then
you can use “apt-cache” with “search” option to find out the exact name of the software package.             For
example, let’s search a text-mode web browser elinks. We input the keyword “elink” instead “ellinks”,
let’s see what does it find:

user@node0:~$ apt-cache search elink
elinks - Advanced text-mode WWW browser
elinks-data - Data files for ELinks - An advanced text-mode WWW browser
elinks-doc - Documentation for ELinks - An advanced text-mode WWW browser
elinks-lite - Lightweight version of Elinks - An advanced text-mode WWW browser
gt5 - shell program to display visual disk usage with navigation
libbusiness-onlinepayment-transactioncentral-perl        -     Transaction     Central   backend   module     for
Business::OnlinePayment
mpatrolc2 - A library for debugging memory allocations
prelink - ELF prelinking utility to speed up dynamic linking
wv - Programs for accessing Microsoft Word documents
zope-cmfplone - content management system based on zope and cmf


So you find it! Then next step is to install it with “apt-get install”, but this time you need to use
sudo because almost all the software installation with default options need root privilege.
user@node0:~$ sudo apt-get install elinks
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  elinks-data liblua50 liblualib50 libruby1.8
Suggested packages:
  elinks-doc
The following NEW packages will be installed:
  elinks elinks-data liblua50 liblualib50 libruby1.8
0 upgraded, 5 newly installed, 0 to remove and 50 not upgraded.
Need to get 2026kB of archives.
After this operation, 9282kB of additional disk space will be used.
Do you want to continue [Y/n]?



1.5 Some Useful Concepts and Commands

1.5.1 Pipeline

Pipeline is very useful when you use Linux or UNIX. According to Wikipedia, pipeline is “a set of
processes chained by their standard streams, so that the output of each process (stdout) feeds
directly as input (stdin) to the next one……”(http://en.wikipedia.org/wiki/Pipeline_(Unix)). Its
operator is “|”. For example, the command “ls” is to list all the file and directory in current
directory, so if you write:
user@node0:/$ ls -l | more
total 118K
drwxr-xr-x     2 root root    4096 2009-10-27 20:27 bin
drwxr-xr-x     3 root root    4096 2009-10-27 20:28 boot
lrwxrwxrwx      1 root root      11 2007-10-11 08:08 cdrom -> media/cdrom
drwxr-xr-x    12 root root 13340 2010-08-17 14:48 dev
drwxr-xr-x 105 root root 12288 2010-08-17 15:49 etc
drwxr-xr-x     2 root root    4096 2007-10-12 11:10 home
drwxr-xr-x     2 root root    4096 2007-10-11 08:13 initrd
lrwxrwxrwx      1 root root      31 2009-01-22 00:43 initrd.img.old -> boot/initrd.im
g-2.6.24-23-deter
drwxr-xr-x    14 root root 12288 2009-10-27 20:27 lib
drwxr-xr-x     3 root root    4096 2007-10-15 17:13 local
drwx------    2 root root 16384 2007-10-11 08:08 lost+found
drwxr-xr-x     3 root root    4096 2007-10-11 08:08 media
drwxr-xr-x     2 root root    4096 2007-04-12 02:11 mnt
drwxr-xr-x     2 root root    4096 2007-10-11 08:13 opt
-rw-r--r--   1 root root      516 2010-06-16 15:12 prepare.log
dr-xr-xr-x   98 root root       0 2010-08-17 14:48 proc
drwxr-xr-x     3 root root    4096 2010-06-16 12:11 proj
drwxr-xr-x     6 root root    4096 2009-10-27 20:26 root
drwxr-xr-x     2 root root 12288 2009-10-27 20:27 sbin
drwxrwxr-x     17 root dhcp     512 2010-07-26 16:28 share
drwxr-xr-x     2 root root    4096 2007-10-11 08:13 srv
--More--


The operator “|” connect data of two processes. In this matter, the content of the working
directory is scrolled according to the process which is defined by the command more.
1.5.2 Wildcards * and ?

* represents any sequence of symbols (0 or more). Ex. a* represents a1.txt a2.txt    a2.sh, etc.
? represents any symbol. Ex. a?.txt represents a1.txt a2.txt, etc.

user@node0:~/test$ ls
a11.txt a1.txt a2.sh a2.txt b2.txt
user @node0:~/test$ ls *.txt
a11.txt a1.txt a2.txt b2.txt
user @node0:~/test$ ls a?.txt
a1.txt a2.txt
user @node0:~/test$ ls a*
a11.txt a1.txt a2.sh a2.txt




1.5.3 Input and Output Redirection

The operator “<” and “>”                    is use to input and output redirection
http://en.wikipedia.org/wiki/Redirection_(computing) . For example:
program < my_in_file > my_out_file
causes input to the executable program p from my_in_file and output from the program p to the
my_out_file. It is written from the beginning of this file.
The command
p < my_in_file >> my_out_file
has the same meaning but the output is appended to the contents of my_out_file




1.5.4 Concatenation Files

The command cat concatenates files and prints on the standard output.
Example
If in a file A is written The quick brown fox jumped and in a file B is a text over a lazy dog. then
command
       $ cat A B

      causes the output:



      The quick brown fox
      jumped over a lazy dog.



      If you write
        $ cat A B > C

     Then the same text is written to the file C.


1.5.5 history:Print commands submitted before

The command history can be used to trace the commands you submitted before.
user@node0:~/test$ history
……
  495   ls
  496   ls a?.txt
  497   ls
  498   ls *.txt
  499   ls a?.txt
  500   ls a*
  501   ls
  502   man grep
  505   man man
  507   man history
  508   history



1.5.6 grep:Print lines matching a pattern

grep searches the named input FILEs (or standard input if no files are named, or if a single
hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN.
By default, grep prints the matching lines.
Here we give an example of using grep with pipe line, where “man” is the pattern:
user@node0:~$ history|grep man
  430   man man
  431   man man
  432   man --help
  433   man --help
  434   man -h
  502   man grep
  503   man history
  504   man 1 history
  505   man man
  507   man history
  510   history|grep man
1.5.7 ps: Report a Snapshot of the Current Processes

The command ps print out the process information. With different options you can get more of
the detailed information.
For example, use ps without option, it only print the process in current console.
user@node0:~$ ps
 PID TTY                  TIME CMD
25738 pts/0           00:00:00 bash
25830 pts/0           00:00:00 ps
Use ps with “-e”, it print all the processes.
     user@node0:~$ ps -e
       PID TTY                  TIME CMD
       1?              00:00:03 init
       2?              00:00:00 kthreadd
       …..
25735 ?                00:00:00 sshd
25737 ?                00:00:00 sshd
25738 pts/0           00:00:00 bash
25831 pts/0           00:00:00 ps
Use ps with “-ef”, it print all the processes and the original commands to run these processes.
user@node0:~$ ps -ef
UID              PID    PPID      C STIME TTY           TIME CMD
root              1         0     0 Aug17 ?        00:00:03 /sbin/init
root              2         0     0 Aug17 ?        00:00:00 [kthreadd]
….
root           5996         1     0 Aug17 ?        00:00:00 /usr/sbin/console-kit-daemon
root          25735     5037     0 12:46 ?        00:00:00 sshd: user [priv]
user         25737 25735        0 12:46 ?        00:00:00 sshd: user@pts/0
user         25738 25737        0 12:46 pts/0   00:00:00 -bash
user         25833 25738        0 12:55 pts/0   00:00:00 ps -ef



1.5.8 Starting and Stopping Processes in the Background

If you write for example:

$ p&
[1] 65667

The process p starts and it is running on the background. The operator & tells the system that this
program need to be executed in the background.
The number which appears in the screen is PID (Process Identification Number). You can do on
the foreground other activities but you can stop the process p at any time by a command:
$ kill -9 65667

Which stops the process p before this has been finished normally.
For more information about kill, you can use "man kill” or “kill –help”


1.5.9 date: print or set the system date and time

To print current date and time, you just need to input the command “date”
mftan@node0:~$ date
Tue Aug 23 14:59:08 PDT 2010




1.6 Working with File & Directory

1.6.1 Structure of Standard Directories in Unix/Linux

Here is the typical directory structure of the UNIX/Linux systems, here is especially for ubuntu:

/bin               binary applications (most of your executable files)
/boot              files required to boot (such as the kernel, etc)
/dev               your devices (everything from drives to displays)
/etc               just about every configuration file for your system
/etc/profile.d     contains scripts that are run by /etc/profile upon login.
/etc/rc.d          contains a number of shell scripts that are run on bootup at different run levels. There is also
                   typically an rc.inet1 script to set up networking (in Slackwar), an rc.modules script to load
                   modular device drivers, and an rc.local script that can be edited to run commands desired by
                   the administrator, along the lines of autoexec.bat in DOS.
/etc/rc.d/init.d   contains most of the initialization scripts themselves on an rpm-based system.
/etc/rc.d/rc*.d    where “*” is a number corresponding to the default run level. Contains files for services to
                   be started and stopped at that run level. On rpm-based systems, these files are symbolic
                   links to the initialization scripts themselves, which are in /etc/rc.d/init.d.
/etc/skel          directory containing several example or skeleton initialization shells. Often contains
                   subdirectories and files used to populate a new user’s home directory.
/etc/X11           configuration files for the X Window system
/home              locally stored user files and folders
/lib               system libraries (similar to Program Files)
/lost+found        lost and found for lost files
/media             mounted (or loaded) devices such as cdroms, digital cameras, etc.
/mnt               mounted file systems
/opt               location for “optionally” installed programs
/proc              dynamic directory including information about and listing of processes
/root                “home” folder for the root user
/sbin                system-only binaries (see /bin)
/sys                 contains information about the system
/tmp                 temporary files
/usr                 applications mainly for regular users
/users               The home directory of users, each user should have a sub-directory in this directory
/var                 mainly logs, databases, etc.
/usr/local/bin       the place to put your own programs. They will not be overwritten with upgrades.
/usr/share/doc       documentation.



1.6.2 List Contents of the Working Directory

The command “ls” displays the names of all subdirectories and files in the working (current,
actual) directory.
Example:

user@node0:/$ ls
bin      etc                lib           mnt                 proj     srv      usr
boot     home                 local        opt                root      sys      var
cdrom    initrd            lost+found    prepare.log   sbin      tmp          vmlinuz.old
dev      initrd.img.old   media         proc            share        users


If the command ls is written with parameter -l then the command lists contents of the working
directory with details.
Example:

user@node0:/$ ls -l
total 118K
drwxr-xr-x    2 root root 4096 2009-10-27 20:27 bin
drwxr-xr-x    3 root root 4096 2009-10-27 20:28 boot
lrwxrwxrwx     1 root root     11 2007-10-11 08:08 cdrom -> media/cdrom
drwxr-xr-x 12 root root 13340 2010-08-17 14:48 dev
drwxr-xr-x 105 root root 12288 2010-08-17 15:49 etc
drwxr-xr-x    2 root root 4096 2007-10-12 11:10 home
drwxr-xr-x    2 root root 4096 2007-10-11 08:13 initrd
lrwxrwxrwx     1 root root     31 2009-01-22 00:43 initrd.img.old -> boot/initrd.img-2.6.24-23-deter
drwxr-xr-x 14 root root 12288 2009-10-27 20:27 lib
drwxr-xr-x    3 root root 4096 2007-10-15 17:13 local
drwx------   2 root root 16384 2007-10-11 08:08 lost+found
drwxr-xr-x    3 root root 4096 2007-10-11 08:08 media
drwxr-xr-x    2 root root 4096 2007-04-12 02:11 mnt
drwxr-xr-x    2 root root 4096 2007-10-11 08:13 opt
-rw-r--r--  1 root root    516 2010-06-16 15:12 prepare.log
dr-xr-xr-x 97 root root       0 2010-08-17 14:48 proc
drwxr-xr-x    3 root root 4096 2010-06-16 12:11 proj
drwxr-xr-x    6 root root 4096 2009-10-27 20:26 root
drwxr-xr-x    2 root root 12288 2009-10-27 20:27 sbin
drwxrwxr-x 17 root dhcp       512 2010-07-26 16:28 share
drwxr-xr-x    2 root root 4096 2007-10-11 08:13 srv
drwxr-xr-x 12 root root        0 2010-08-17 14:48 sys
drwxrwxrwt      4 root root 4096 2010-08-18 23:17 tmp
drwxr-xr-x  4 root root 4096 2010-06-16 12:11 users
drwxr-xr-x 12 root root 4096 2009-01-22 01:02 usr
drwxr-xr-x 15 root root 4096 2007-10-29 15:44 var
lrwxrwxrwx   1 root root   28 2009-01-22 00:43 vmlinuz.old -> boot/vmlinuz-2.6.24-23-deter



1.6.3 Display File: more & less

If you write the command “more” and a name of a file then the file will be displayed. And User
can control the output: 1) press space...the next screen is displayed;2)press enter...the next row is
displayed;3)press q.......the command is finished.
user@node0:~/homework2$ more example3.s
……
   .section           .rodata
.LC0:
           .string "%d\n"
           .text
.globl main
           .type     main, @function
main:
           pushl     %ebp
           movl       %esp, %ebp
           subl      $24, %esp
           andl      $-16, %esp
           movl       $0, %eax
           addl      $15, %eax
--More--(57%)


Another command “less”is similar to “more”. The main difference between more and less is that
less allows backward and forward movement using the arrow keys, while more only uses the
[Spacebar] and the [B] key for forward and backward navigation


1.6.4 head & tail

The command “head” print the first 10 lines of each file to            standard output. With more
than one file, precede each with a header giving the file name.         Withno file, or when FILE is -,
read standard input.
The command “head” print the last 10 lines of each file to             standard output. With more
than one file, precede each with a header giving the file name.         With no file, or when FILE is -,
read standard input.
Example
user@node0:~/homework2$ tail example3.s
        movl      $1, -4(%ebp)
        movl      -4(%ebp), %eax
        movl      %eax, 4(%esp)
        movl      $.LC0, (%esp)
        call     printf
        leave
        ret
        .size    main, .-main
        .section          .note.GNU-stack,"",@progbits
        .ident "GCC: (GNU) 3.4.6 (Ubuntu 3.4.6-6ubuntu5)"
1.6.5 cat: Concatenate Files

Concatenate files and print on the standard output.
user@node0:~$ more 1.txt
111
user@node0:~$ more 2.txt
222
user@node0:~$ cat 1.txt 2.txt
111
222
user@node0:~$ cat 2.txt 1.txt
222
111
You can redirect the concatenated results into another file
cat 1.txt 2.txt >3.txt
user@node0:~$ more 3.txt
111
222



1.6.6 Copy File

The command
cp file_1 file_2
copies file_1 to file_2. The both files must be in the same working directory. If they are in various
directories, the path must be given.

user@node0:~$ cp 1.txt 4.txt
user@node0:~$ more 4.txt
111


If you are copying directory, then remember to add the option “-R”


1.6.7 Rename and/or Move the File

The command
mv file_1 file_2
moves file_1 to file_2. The both files must be in the same working directory. If they are in various
directories, the path must be given. The file_1 is removed from the disk.
user@node0:~$ more 1.txt
111
user@node0:~$ more 2.txt
222
user@node0:~$ mv 1.txt 2.txt
user@node0:~$ more 2.txt
111
user@node0:~$ more 1.txt
1.txt: No such file or directory
1.6.8 Remove File

rm 1.txt             removes the 1.txt from the system at all.
rm *.txt             will remove all files with postfix “.txt”
rm *                 will erase all files from your working directory.
rm -i *              it will be done also but the system will ask for permission before removing each
                     file.
rm -r your_file      causes removing of the directory your_file even if this directory is not empty.
rm -rf /             will erase all your directories (including not empty directories).
                     Never do it!!!

user@node0:~$ls
2.txt 4.txt 3.txt
user@node0:~$ rm *.txt
user@node0:~$ ls

user@node0:~$




1.6.9 Find File

The command
find /usr -name filename
Finds a file filename in subdirectories in directory /usr.

user@node0:~$ sudo find / -name password
/usr/share/doc/dialog/examples/password
user@node0:~$ sudo find / -name passwd
/usr/share/linda/overrides/passwd
/usr/share/lintian/overrides/passwd
/usr/share/doc/passwd
/usr/bin/passwd
/etc/emulab/passwd
/etc/pam.d/passwd
/etc/passwd




1.6.10 pwd: show current path

The command pwd shows current directory:
user@node0:~$ pwd
/users/user
1.6.11 cd:Change Working Directory

The command
cd directory
changes the directory try_it which path is given relative to the working directory. Here the
symbol “.” represents current directory, “..” represents the superior directory.
user@node0:~$ pwd
/users/user
user@node0:~$ cd /etc/init.d/
user@node0:/etc/init.d$ pwd
/etc/init.d
user@node0:/etc/init.d$ cd ..
user@node0:/etc$ pwd
/etc



1.6.12 Make Directory

The command
mkdir my_dir
makes new directory my_dir (the path is given relative) as a subdirectory of the current directory.
user@node0:~ $ls -l
total 2K
-rw-r--r-- 1 user SJSUCMPE209 1 2010-08-24 13:54 1.txt
user@node0:~ $ mkdir test
user@node0:~ $ ls -l
total 4K
-rw-r--r-- 1 user SJSUCMPE209    1 2010-08-24 13:54 1.txt
drwxr-xr-x 2 user SJSUCMPE209 512 2010-08-24 13:55 test



1.6.13 Remove Directory

The command “rmdir your_dir” removes directory your_dir if it is empty. If you want to remove not
empty directory.

user@node0:~/ls -l
total 4K
-rw-r--r-- 1 user SJSUCMPE209    1 2010-08-24 13:54 1.txt
drwxr-xr-x 2 user SJSUCMPE209 512 2010-08-24 13:55 test
user@node0:~ $ rmdir test
user@node0:~ $ ls
1.txt
user@node0:~ $ ls -l
total 2K
-rw-r--r-- 1 user SJSUCMPE209 1 2010-08-24 13:54 1.txt
1.6.14 Access Permission of File

Example :
user@node0:~ $ ls -l
total 2K
-rw-r--r-- 1 user SJSUCMPE209 1 2010-08-24 13:54 1.txt
There are 10 columns as the first part of the file’s information shows:the first column is empty for
data and programs or in first column is written character d if the item is directory etc. The next
three columns are permissions for the user, the columns 5, 6, 7 for the user's group (in this
example the group is called user), and the last three for the rest of the word. The next
information in the row is the size of the file (in bytes), the date of the last update, and the name
of the file. The next table clarifies the meaning of the letters written from the 2nd to the 10th
column:
                   Character Meaning
                   r          Permission for reading
                   w          Permission for writing
                   x          File is executable
If we want to allow people from the user's group to read this file and the rest of the word will be
unable even to read this file, we will write the command:
$ chmod 760 1.txt
The access permission looks now like:
user@node0:~/test$ chmod 760 1.txt
user@node0:~/test$ ls -l
total 2K
-rwxrw---- 1 user SJSUCMPE209 1 2010-08-24 13:54 1.txt
To understand this you must know that number 1 "allows" and number 0 "suppresses" access
permission and you need to know relation between octal and binary numbers:
                   Octal scale Binary scale
                   0            000
                   1            001
                   2            010
                   3            011
                   4            100
                   5            101
                   6            110
                   6            110
                   7            111




1.6.15 Determine File Type

The type of file can be detected by a command file.
Example:
user@node0:~ $file example3.s
example3.s: ASCII assembler program text
Here is some Postfixes of Files
                              Meaning
                  .c          Source program in C language
                  .f          Source program in Fortran 77
                  .f90        Source program in Fortran 90
                  .p          Source program in Pascal
                  .pbm        bi-level, black and white image (2 bits per pixel)
                  .pgm        grayscale (8 bits per pixel)
                  .ppm        color (24 bits per pixel)
                  .jpg        compressed by JPEG



1.7 Quick Tutorial for Editor vi

Vi is a powerful programmers text editor.


1.7.1 Invoking vi

If you write a command “vi my_file”, you will see the screen with a column of tildes. The editor vi is now
in so called command mode. The screen looks like:

~
~
~
~
~
~                            VIM - Vi IMproved
~
~                             version 7.1.138
~                         by Bram Moolenaar et al.
~                Vim is open source and freely distributable
~
~                      Help poor children in Uganda!
~               type :help iccf<Enter>       for information
~
~               type :q<Enter>             to exit
~               type :help<Enter> or <F1> for on-line help
~               type :help version7<Enter> for version info
~
~
~
~
~
                                                               0,0-1        All
~
The two basic commands are the following:
                  i Insert text to the left of cursor

                  a Insert text to the right of cursor
Since you are at the beginning of an empty file it does not matter which of these you type. Cursor
Movements Commands
You need to be in the command mode. If you do not know what your actual mode is, press the bottom
“ESC”. This keystroke always turns the editor in the command mode. Then you can move along the
screen if you keystroke the button:
                     Keystroke of Doing

                     h            Cursor is moved one space to the left

                     j            Cursor is moved one line down

                     k            Cursor is moved one line up

                     l            Cursor is moved one space to the right



1.7.2 Deleting Text

If you are in command mode then
                     Keystroke of Doing

                     x            Delete one character at the cursor

                     dd           Delete one line where the cursor is placed



1.7.3 File Saving

You must be in command mode. You can use then several tricks to save the file:
                     Keystrokes Doing

                     :x         Write file to the disk and finish

                     ZZ         Write file to the disk and finish

                     :w         Write file to the disk and continue



1.7.4 Replace Mode

Very useful is so called replace mode which enables overwrite the existing text.
                     Keystrokes Doing

                     :r        Replace one character over the cursor

                     :R        Overwrite text until the next action (e.g. keystroke of esc)



1.8 Edit code, Compile it and Execute a program

Now use vi to write a c code file named test1.c.

//test1.c
#include <stdio.h>
int main(int argc,char ** argv){
  int a=0;
  a=(int)&a;
  printf("The address of variable a is 0x%x.\n",a)
  return 0;
}

:wq


And compile it with GCC compiler. The command of compiler is gcc, the option –g tells it that the
symbol table and debug information should be included in the complied object. And –o tells the compiler
that the target executive file is test1.

vi test1.c


user@node0:~/test$ gcc -g test1.c -o test1


test1.c: In function ‘main’:


test1.c:5: error: expected ‘;’ before ‘return’


Then revise the mistake in vi.

//test1.c
#include <stdio.h>
int main(int argc,char ** argv){
   int a=0;
   a=(int)&a;
   printf("The address of variable a is 0x%x.\n",a);
   return 0;
}~
~
~
~
~
~
:wq


Then recompile it and run it.

user@node0:~/test$ gcc -g test1.c -o test1
user@node0:~/test$
user@node0:~/test$ ls
1.txt test1 test1.c
user@node0:~/test$ ./test1
The address of variable a is 0xbffffc30.
user@node0:~/test$


Congratulation!
1.9 Network Related Command

1.9.1 ping:send ICMP ECHO_REQUEST to network hosts

ping can be used to test the target host is available or not. But some server may not response
due to security consideration. You need to keystroke “ctrl+c” to terminate ping in Linux.
mftan@node0:~$ping 10.1.1.3
PING 10.1.1.3 (10.1.1.3) 56(84) bytes of data.
64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=5.60 ms
64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=6.17 ms
64 bytes from 10.1.1.3: icmp_seq=3 ttl=64 time=5.58 ms
64 bytes from 10.1.1.3: icmp_seq=4 ttl=64 time=4.24 ms
(ctrl+c)
--- 10.1.1.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 4.249/5.403/6.170/0.706 ms




1.9.2 netstat : Print network connections, routing tables,
           interface statistics,etc

First let us use netstat to check the connection status:
mftan@node0:~$netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address                Foreign Address             State
tcp          0        0 localhost:16505             localhost:46937             ESTABLISHED
tcp          0        0 localhost:46937             localhost:16505             ESTABLISHED
tcp          0        0 localhost:46939             localhost:16505             ESTABLISHED
tcp          0        0 bpc176.isi.deterl:44849 users.isi.deterla:16505 ESTABLISHED
tcp          0        0 localhost:16505             localhost:46940             ESTABLISHED
tcp          0        0 localhost:46940             localhost:16505             ESTABLISHED
tcp          0        0 localhost:16505             localhost:46939             ESTABLISHED
tcp6         0      640 bpc176.isi.deterlab:ssh users.isi.deterla:51107 ESTABLISHED
udp           0        0 bpc176.isi.deterl:38205 boss.isi.deterlab.:8509 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags         Type          State             I-Node     Path
unix 2          []           DGRAM                               6060      @/com/ubuntu/upstart
unix 2          []           DGRAM                               6298      @/org/kernel/udev/udevd
unix 8          []           DGRAM                               11244     /dev/log
unix 3          []           STREAM          CONNECTED           494011    /var/run/dbus/system_bus_socket
unix 3          []           STREAM          CONNECTED           494010
unix 3          []           STREAM          CONNECTED           494009    /var/run/dbus/system_bus_socket
unix 3          []           STREAM          CONNECTED           494008
unix 2          []           DGRAM                               494004
unix 3          []           STREAM          CONNECTED           494003
unix 3          []           STREAM          CONNECTED           494002
unix 2          []           DGRAM                               14659
unix 3          []           STREAM          CONNECTED           14657     /var/run/dbus/system_bus_socket
unix 3          []           STREAM          CONNECTED           14656
unix    2      []            DGRAM                            11783
unix    2      []            DGRAM                            11636
unix    2      []            DGRAM                            11549
unix    3      []            STREAM        CONNECTED          11326
unix    3      []            STREAM        CONNECTED          11325
unix    2      []            DGRAM                            11294


And then use “-r” option to print the route table. This information can also fetched by the
command “route”.
mftan@node0:~$netstat -r
Kernel IP routing table
Destination        Gateway              Genmask          Flags   MSS Window irtt Iface
10.1.1.0           *                     255.255.255.0 U          00            0 eth10
192.168.0.0        *                     255.255.252.0 U          00            0 eth15
default            router.isi.dete 0.0.0.0          UG         00          0 eth15

Finally we use “-I” option to print the inter face information. This information can also fetched by
the command “ifconfig”.
mftan@node0:~$ netstat -i
Kernel Interface table
Iface    MTU Met       RX-OK RX-ERR RX-DRP RX-OVR        TX-OK TX-ERR TX-DRP TX-OVR Flg
eth10         1500 0           8       0       00                  17      0       0      0 BMRU
eth15         1500 0      579871       0       00            187606        0       0      0 BMRU
lo           16436 0          77       0       00                 77       0       0      0 LRU



1.9.3 ifconfig: print information of interface

By using the command ifconfig, you can get the detailed information about the interface, and you
can compare them to the information obtained by “netstat -i"
mftan@node0:~$ ifconfig
eth10    Link encap:Ethernet HWaddr 00:15:17:5d:38:3c
          inet addr:10.1.1.2 Bcast:10.1.1.255 Mask:255.255.255.0
          inet6 addr: fe80::215:17ff:fe5d:383c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:984 (984.0 B) TX bytes:1668 (1.6 KB)
          Base address:0xece0 Memory:fe9e0000-fea00000

eth15       Link encap:Ethernet HWaddr 00:04:23:9f:1d:f6
             inet addr:192.168.3.176 Bcast:192.168.3.255 Mask:255.255.252.0
             inet6 addr: fe80::204:23ff:fe9f:1df6/64 Scope:Link
             UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
             RX packets:579726 errors:0 dropped:0 overruns:0 frame:0
             TX packets:187520 errors:0 dropped:0 overruns:0 carrier:0
             collisions:0 txqueuelen:1000
             RX bytes:99909007 (95.2 MB) TX bytes:49781197 (47.4 MB)
             Base address:0xccc0 Memory:fe3e0000-fe400000

lo          Link encap:Local Loopback
             inet addr:127.0.0.1 Mask:255.0.0.0
             inet6 addr: ::1/128 Scope:Host
             UP LOOPBACK RUNNING MTU:16436 Metric:1
             RX packets:77 errors:0 dropped:0 overruns:0 frame:0
             TX packets:77 errors:0 dropped:0 overruns:0 carrier:0
             collisions:0 txqueuelen:0
             RX bytes:9907 (9.6 KB) TX bytes:9907 (9.6 KB)
1.9.4 route:show / manipulate the IP routing table

By using the command route, you can also get route, and you can compare it to the information
obtained by “netstat -r". Besides, you can use it to change the route table if necessary.

mftan@node0:~$ route
Kernel IP routing table
Destination        Gateway              Genmask          Flags Metric Ref   Use Iface
10.1.1.0           *                     255.255.255.0 U       0        0         0 eth10
192.168.0.0        *                     255.255.252.0 U       0        0        0 eth15
default            router.isi.dete 0.0.0.0          UG    0       0         0 eth15

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:11/11/2012
language:Unknown
pages:21