2012年3月2日星期五

Linux Basics

0.Others
    • 启动到text模式
      修改/etc/inittab文件,将id:5:initdefault:一行中的5改为3
    • 启动到xwindow模式   
     init 5
1.Getting Help
    • man ls
    • ls --help
    • man -K user
         This would search all man pages for the keyword user and return a list of all man pages con-taining
         the term. You will then be prompted to view each page that is returned, skip a page and go to the next
         page, or quit the search.
    • whatis useradd
    • apropos­ whoam
         The apropos command also searches the whatis database but searches for strings rather than complete
         words.

2.Users and Groups
    • Users
         /etc/passwd
    • Groups
         /etc/group

3.Services and processes
    • ps -A
          ps command with the -A flag (for all) to list all the processes currently running on our host.
    • top
          The top command starts an interactive monitoring tool that updates every few seconds with the top
           running processes on your host.

4.Files and File Systems
    • pwd
    • linux directory structure

         Directory Description
         /bin/       User commands and binaries.
         /boot/     Files used by the boot loader. (We talk about boot loaders in Chapter 5.)
         /dev/      Device files.
         /etc/       System configuration files.
         /home/   User’s home directories.
         /lib/        Shared libraries and kernel modules.
         /media/   Removable media is usually mounted here (see Chapter 8).
         /mnt/      Temporary mounted file systems are usually mounted here (see Chapter 8).
         /opt/       Add-on application software packages.
         /proc/     Kernel and process status data is stored in here in   text-  file format.
         /root/      The root user’s home directory.
         /sbin/      System binaries.
         /srv/        Data for services provided by this host.
         /tmp/       Directory for temporary files.
         /usr/        User utilities, libraries, and applications.
         /var/        Variable or transient files and data, for example logs, mail queues, and print jobs.
 
    • ls(­ls­-la)
         the l and a switches have been added to the ls command. The l switch, which is an abbreviation of
         long, uses a long listing format, which as you can see shows a lot more information. The a switch tells
         ls to list all files and directories, even hidden ones.

             -rw-r--r--­­­1­root­­root­­­­­­0­­2008-07-­15­20:47­.autofsck
             drwxr-xr-x­­­2­root­­root­­­4096­­2008-05-­18­04:11­bin

        Each line of the listing contains seven pieces of information about each object:
       - Unix file type
       - Permissions
       - Number of hard links
       - User and group ownership
 - Size
 - Time and date
 - Name

   4.1 File Types and Permissions
        • File types
           Type Description
           -        File
           d        Directory
           l         Link
           c        Character devices
           b        Block devices
           s        Socket
           p        Named pipe

        • Permissions
          Each file on your host has three classes of permissions:
    - User
    - Group
    - Other (everyone else)

       

          With chmod, each class is abbreviated to a single letter:
          u: User
          g: Group
          o: Other or everyone
          a: All

          chmod ­u+x­ myfile
          ­chmod­­ u-­x,og+w­ myfile
          chmod­ 654 ­myfile
          
          行动: 
            + 表示添加权限 
             - 表示删除权限 
            = 表示使之成为唯一的权限 


          We can also apply the permissions of one class to another class by using the = symbol:

            ­chmod­ u=g ­myfile --用户和组的权限一样

         

          You can also set permissions for multiple files by listing each file separated by space like so:
            ­chmod­ u+r ­file1 ­file2 ­file3
          
          You can also use the asterisk symbol to specify all files and add the -R switch to recurse into lower 
          directories like so:
            chmod­ -R ­u+x ­/usr/local/bin/*

          其中:rwx也可以用数字来代替 
             r ------------4 
             w -----------2 
             x ------------1 
             - ------------0 

         当大家都明白了上面的东西之后,那么我们常见的以下的一些权限就很容易都明白了: 
            -rw------- (600) 只有所有者才有读和写的权限 
            -rw-r--r-- (644) 只有所有者才有读和写的权限,组群和其他人只有读的权限 
            -rwx------ (700) 只有所有者才有读,写,执行的权限 
            -rwxr-xr-x (755) 只有所有者才有读,写,执行的权限,组群和其他人只有读和执行的权限 
            -rwx--x--x (711) 只有所有者才有读,写,执行的权限,组群和其他人只有执行的权限 
            -rw-rw-rw- (666) 每个人都有读写的权限 
            -rwxrwxrwx (777) 每个人都有读写和执行的权限

   4.2 Users, Groups, and Ownership
        use the chown command to change user and group ownership.
          chown ­jsmith ­myfile
          chown ­jsmith:admin­ myfile
          chown­-R ­jsmith:admin ­/home/jsmith/*

   4.3 Size and Space
         • ­ls­-lh
              -rw-rw-r--­­1­jsmith­jsmith­­51K­­2008-08-­17­23:47­myfile
           On the previous line, you can see that the myfile file is 51 kilobytes in size.

         • du­-sh /usr/local/bin
            ­  4.7M ­­­­­/usr/local/bin
            The -s switch summarizes the total, and the -h switch displays the size in a   human-  readable form.

         • df -h
            This command displays all of your disks and storage devices and the free space present on them.
            We’ve executed the command and added the -h switch, which returns   human-  readable sizes.

   4.4 Date and Time
         • ­ls­-lu
           display the last accessed time for a file by listing it with the -u switch

         • ­ls­-lc
           list creation dates by using the -c switch

         • date
           Using date on the command line without any options will return the current time and date like so:
              Tue ­Aug­ 19­13:01:20 ­EST ­2008

5.Working with Files
   5.1 Reading Files
   ­• cat ­/etc/hosts
     The cat command is so named because it “concatenates and prints files
   ­• less ­/etc/services

     From inside the less interface, you can scroll through the file.
       - spacebar : 下一页
       - Enter key: 下一行To scroll backward,
       - B key     : 上一行(也可以用上下键)
       - Q key    : 退出
   ­• ­grep­ localhost­ /etc/hosts
     you can see a very simple grep search for the string localhost in the file /etc/hosts.
       $­grep­localhost­/etc/hosts
         127.0.0.1 ­­­­­­­­­­­­­­­localhost.localdomain­ localhost ­localhost
     You can also recursively search down into lower directories by adding the -r switch like so:
       $­grep ­-r­ localhost­ /etc

   5.2 Searching for Files
   ­• ­find ­/home/­ -type ­f­ -iname ­myfile*
     The -iname option searches for a   case-  insensitive pattern, in this case, all files starting with myfile.
   ­• ­find ­/home/­ -type ­f­ -iname ­myfile* -exec grep mail {} \;
   • ­find ­/ ­-nouser ­-o­ -nogroup  
     This command, run as root, will search the whole directory tree for any files that don’t belong to a valid
     user or group.

   5.3 Copying Files
   ­• cp ­-i­ /home/jsmith/*.c ­./
     We’ve also added the -i switch to make sure we’re prompted if a file already exists.
   ­• ­cp ­-r ­/home/jsmith ­/backup
     The previous line copies the /home/jsmith directory and all files and directories beneath it to the /backup
     directory.
   ­• ­cp ­-p ­/home/jsmith ­/backup
     Lastly, when copying files using the cp command, some items about the file, such as dates, times, and
     permissions, can be changed or updated. If you want to preserve the original values on the copy, you can
     use the -p switch.
   ­• ­cat ­/home/jsmith/myfile ­> ­/home/jsmith/yourfile
     The use of the > symbol sends the output from one command to the command or action on the other side
     of the > symbol. In this case, the output of the cat command is redirected into a file called yourfile.
     If this file doesn’t exist, it will be created. If it does exist, its content will be overwritten.
   ­• ­cat ­/home/jsmith/myfile ­>> ­/home/jsmith/yourfile
     Using the >> syntax will append the output from the cat of myfile to the end of yourfile.
     If yourfile does not exist, it will be created.

   5.4 Moving and Renaming Files
   ­• ­mv ­-i­ ~/myfile ­~/mynewfile
      The -i option again ensures we get prompted if the target file already exists. You can also rename files in
      place with the mv command:

   5.5 Deleting Files
   • rm­ -fr ­/home/jsmith/backup

   5.6 Linking Files
   • ­ln­ -s ­/home/jsmith/myfile
     The previous line would create a symbolic link called myfile to the /home/jsmith/myfile file.

   5.7 Linking Files
   • vi file

没有评论:

发表评论