2015年8月20日 星期四

Chapter 2. Managing Files From the Command Line.

 
  1. The file system hierarchy
     



    /             :This is the system's root directory.
    /root       :This is the root account's  home  directory.
    /boot      :Files needed in order to start the boot process.
    /dev       :Contains special device files which are used by the system
                      to access hardware.
    /etc        :This directory contains static, persistent system configuration data.
    /home    :Home directories where regular users store their personal
                      data and configuration files.
    /run        :Contains dynamic, non-persistent application runtime data.
    /tmp       :Temporary files are stored here.
    /usr         :Contains installed software programs and libraries.
    /usr/bin   :User commands.
    /usr/sbin :System administration commands.
    /usr/local:Locally customized software.
    /var         :This directory contains dynamic configuration data, such
                       as FTP and websites.
     
  2. Absolute paths and relative paths
     

    Absolute path:a fully qualified name, beginning at the root (/) directory
                              and specifying each subdirectory traversed to reach and
                              uniquely represent a single file.

    Relative path:specifying only the path necessary to reach the file from
                             the working directory.
     
  3. Navigating  paths
     

    # pwd
    displays the full path name of the current location

    # ls
    lists directory contents for the specified directory
    -l    long listing format
    -a    all files, includes hidden files
    -R   recursive, to include the contents of all subdirectories

    # cd
    change directories
    cd -    changes directory to the directory where the user was
              previous to the current directory.
    cd ..    move up one level to the parent directory, without needing
               to know the exact parent name.

    # touch
    normally updates a file's timestamp to the current date and time
    without otherwise modifying it.
    This is useful for creating empty files.

    ※ File names beginning with a dot(.) indicate files hidden from
         normal view using ls and other commands.
     
  4. Command-line file management
     

    Copy file
    # cp file1 file2
    將 file1 複製貼上成 file2
    # cp file1 file2 file3 dir
    將 file1、file2、file3 複製貼上到 dir 目錄下

    Move file
    # mv file1 file2
    將 file1 剪下貼上成 file2,若 file2 不存在,則相當於將 file1 更名為 file2
    # mv file1 file2 file3 dir
    將 file1、file2、file3 剪下貼上到 dir 目錄下

    Remove file
    # rm file1
    將 file1 移除
    # rm -f file1 file2 file3
    將 file1、file2、file3 移除, -f 表示忽略不存在的檔案,不會出現警告訊息

    Create directory
    # mkdir dir
    建立 dir 資料夾
    # mkdir -p par1/par2/dir
    -p 表示直接將所需要的目錄(包含上層目錄)遞迴建立起來

    Copy directory
    # cp -r dir1 dir2
    -r 表示遞迴持續複製,用於目錄的複製行為
    # cp -r dir1 dir2 dir3 dir4
    將 dir1、dir2、dir3 遞迴持續複製到 dir4 目錄下

    Move directory
    # mv dir1 dir2
    將 dir1 剪下貼上成 dir2,若 dir2 不存在,則相當於將 dir1 更名為 dir2
    # mv dir1 dir2 dir3 dir4
    將 dir1、dir2、dir3 剪下貼上到 dir4 目錄下

    Remove directory
    # rm -r dir1
    -r 表示遞迴刪除,用在目錄的刪除
    # rm -rf dir1 dir2 dir3
    將 dir1、dir2、dir3 移除, -f 表示忽略不存在的檔案,不會出現警告訊息
     
  5. File globbing: path name expansion


    # ls [ac]*
    列出開頭為a或c的檔案

    # touch file[1,2,3].txt
    同時建立 file1.txt 、 file2.txt 、 file3.txt 等檔案

    # echo Today is 'date +%A'
    # echo Today is $(date +%A)

    可使用單引號與$(),在原有指令中執行另一個指令

    # echo 'Today is $(date +%A)'
    單引號會將內容當作字串輸出,忽略指令
    顯示:Today is $(date +%A)

    # echo "Today is $(date +%A)"
    雙引號會將內容當作字串輸出,執行指令
    顯示:Today is Sunday