|
#1
| ||||
| ||||
| [轉貼] 第二章 GNU and Unix Commands 一. Shell概述 1. shell是一command interpreter,在核心及使用者間提供一介面,並且可讓使用者與系統做交談(interact)。 以下圖來說,比如使用者在shell下輸入date指令,這時候會透過shell 解析給核心這個訊息,因電腦認得的是二進位的程式碼,它並不認識 date是什麼東西,所以要經過shell來將date解析成核心認得的二進位 程式碼,如此工作才有辦法順利進行。 2. 微軟下,都是配標準的shell,如MS-DOS的command.com,Windows的explorer. exe,而在Linux下則有多種shell可供選擇 (sh、ash、ksh、csh、tcsh、bash)。 3. 第一個Unix系統的shell為Steve Bourne所寫,稱之為sh,在sh出現後,一些人相繼發展出各種不同的shell,而Liunx預設使用的shell為Bourne Again SHell,就是所謂的bash,是依sh發展而來的。 4. $ cat /etc/shells → 看看系統上目前可使用的shell有哪些。 $ chsh –s /bin/sh → 改變自己的shell為sh ( logout再login才生效 ) 。 $ echo $SHELL → 顯示目前所使用的shell。 # chsh –s /bin/csh userol → root將userol的shell改為csh。 二. Entering commands at the command prompt 在介紹基本指令之前,我們先大概認識一下提示號,以bash為例,如: [barry@openlinux barry]$ 在@左邊的barry為使用者名稱 ( 帳號 ),openlinux為主機名稱,在@右邊 的barry為目前所在的目錄位置,也就是一般使用者登入系統後預設的家目錄位置barry。$ 是一般使用者的提示號,若是超級使用者 root登入,則其 提示號為 #,如:[root@openlinux root]# 1. cd ( change directory ): $ cd → 切換至家目錄,等於 $ cd ~ # cd ~user01 → 切換至使用者user01的家目錄。 $cd - → 切換至前一個目錄的位置(非上一層目錄),於此前並顯示完整路徑。 $ cd /etc/mail → 切換至mail目錄,此為絕對路徑寫法。 $ cd .. → 切換至上一層目錄。 $ cd ../dir → 切換至上一層目錄下的dir目錄。此為相對路徑寫法。 另外”.”表示現在這個目錄,例如: $ cp /etc/passwd . → 將passwd複製到現在這個目錄下。 2. ls (list): -l:Lists in long format. -a:Lists all files,including hidden files. -R:Lists all files recursively down the tree. -r:reverse order while sorting. -d:Lists only the name of a directory,not its contents,一般搭配 -l。 例:$ ls -laR $ ls –l /var → 列出var ( 不含 ) 以下的檔案目錄名稱,並以長格式顯示。 $ ls –ld /etc → 只列出etc目錄的長格式。 $ ls –l /etc/passwd → 顯示passwd檔案的長格式。 3. cat: To send the contents of one or more files and to create new files. 例:$ cat /etc/passwd $ cat /etc/named.conf>babafile $ cat>newfile → 馬上按ctrl+d就是製造新檔案,亦可輸入內容後,再按ctrl+d。 $ cat>oldfile → 馬上按ctrl+d就等於清空原檔案的內容。 $ cat file1 file2 file3 $ cat –n /etc/termcap>newfile 4. more、less:顯示檔案內容 例:$ more /etc/termcap → to scroll forward using the spacebar to scroll backward using the b key. $ less /etc/termcap → 同上,但習慣用pageup、pagedown來翻頁,上式也可寫成 $ cat /etc/termcap | less 若要退出,按q。 尋找大檔案中字串: 例:$ cat –n /etc/termcap>barry $ more barry → /fix 尋找fix字串 ( 從最前面找起 ) → n 找下一個fix。無法往回找。 $ less barry → /fix→n ( 找下一個 ) → n ( 再下一個 ) → N ( 回上一個 )。 若要進入vi command mode,可按v進入,若要退回原more或less 畫面,使用:q 。 ˙Redirector and Pipes and Stream (1) Stream: stdin ( standard input ),代表數字0。 stdout ( standard output ),代表數字 1。 stderr ( standard error ),代表數字 2。 stdin預設為從keyboard輸入。 stdout、stderr此兩種輸出類型則預設輸出至screen。 (2) Pipe:send the stdout of one command as stdin to another command 例:$ cat /etc/termcap | less = $ less /etc/termcap $ rpm –qa | grep bind (3) Redirector:主要為改變stream的預設值。 >:where to send stdout 例:$ cat /etc/passwd> barryfile ( 會cover原內容 ) $ cat >file → 按ctrl+d等於建新的空檔案或清空原檔案內容,亦可先 輸入內容後,再按ctrl+d。 $ ls –l /var> newfile $ cat /etc/passwd noexistfile 1>file1 2>file2 → 將此兩個檔案內容,其stdout輸出至file1,stderr輸出 至file2,1為預設值。 $ cat /etc/passwd noexistfile>file1 → 將stdout輸出至file1,stderr輸出至screen。 $ cat /etc/passwd noexistfile>barryfile 2>&1 = $ cat /etc/passwd noexistfile >& barryfile = $cat /etc/passwd noexistfile &> barryfile → 將stdout及stderr輸出至barryfile。 $ cat /etc/fstab noexistfile > /dev/null 2>&1 $ true > barryfile →清空檔案內容 = $ cat >barryfile 按ctrl+d = $ cat /dev/null > barryfile >>:append file,can’t cover file content. 例:$ cat /etc/fstab >> barryfile <:redirect stdin ( 由stdin讀取資料 ) 例:$ cat < /etc/fstab → file was opened by the shell ( note:shell一次只能開一 個檔案 ) $ cat /etc/fstab → file was opened by cat.以上兩者結果相同,但意義不同。 $ mail Jessica < messagefile <<: 例:$ cat <<endoff → 表示輸入至endoff字串時結束。 $ cat << end > barryfile → 輸入至end後結束,並且將輸入的內容輸出到barryfile。 以下為file filter command 5. head:output the first part of files 例:$ head -5 /etc/passwd → 列檔案前5行 = $ head –n 5 /etc/passwd = $ cat /etc/passwd | head –5 $ head /etc/passwd → 預設列前10行 $ head -8 /etc/passwd /etc/termcap 6.tail:output the last part of files 例:$ tail -15 /etc/termcap → 列檔案最後面15行的內容 = $ tail –n 15 /etc/termcap $ tail +20 /etc/passwd → 從第20行開始列至最後一行 # tail –f /var/log/messages → 按ctrl+c結束 (看最新訊息紀錄。Note:須root身份) $ cat –n /etc/termcap | head –n 20 | tail –n 6 = $ cat –n /etc/termcap | head –20 | tail +15 → 列出檔案15-20行的內容 7.cut:縱向截取檔案部份資料。 -c ( character ) -f (field) 搭配-d ( delimit ) 例:$ cat /etc/passwd | cut –c 2,5 → 列出檔案中第2及第5個字元。 $ cut –c 2-5 /etc/passwd → 列出第2到第5個字元。 $ cut –d: -f 2 /etc/passwd → 列出檔案內容中以 ” : ” 做界限的第2欄位。 $ cut –d: -f 3,5 /etc/passwd → 以 ” : ” 做界限來列出第3及第5 欄位。 $ cut –d: -f 3-5 /etc/passwd → 以 ” : ” 做界限,列出第3至5欄位。 8.wc: -l(lines) -w(words) -c(characters) 例 $ wc /etc/fstab /etc/passwd → 列出順序:行、字、字元。即分別列出fstab、passwd內容的 行數、字數、字元數,兩檔案的合計也會列出來。 $ cat /etc/termcap | wc –c $ cat /etc/passwd | wc –l → 有幾行即表幾個帳號。 9.tr:replace one string with another or delete characters in a file -s characters 將重覆字元縮成一個。 -d characters 刪除所指定字元。 <note> tr can’t open files → tr是所有filters中,唯一不能直接open file的。 例:$ cat /etc/passwd | tr a-z A- Z → 將所有小寫英文字母替換成大寫英文字母。 $ cat /etc/passwd | tr bary abcd → 將檔案中的b換成a,a換成b,r換成c,y換成d。 $ tr a-z A-Z /etc/passwd → 錯誤,因不能open files。 $ cat file | tr –s ad → 將a及d 字母有重覆的,縮成一個。 $ cat file1 file2 | tr –d c → 刪除檔案內容中的c字元。 10.nl: = cat –n 將檔案內容附上編號列出來。 例: $ nl /etc/passwd 11.sort ( 排序 ) :預設以每行第一字元為依據做排序。 預設順序:空格 → 其它字元(#!…) → 數字 → 英文字母大、小寫。 -n:按數字大小排序,英文字母大、小寫 → 數字。 -r:以預設相反預序作排序 (reverse) 。 -m (merge) :將檔案內容合併(上下),但不做排序。 -o (output) :排序後,將結果輸出至file中。 例: $ cat /etc/passwd | cut –d: -f 1,3 | sort $ sort –n file1 file2 $ sort –m file1 file2 $ sort –r /etc/passwd -o newfile = $ sort –r /etc/passwd > newfile $ cat /etc/fstab /etc/passwd | sort –m -o newfile 12.tac:cat的相反順序 例:$ tac /etc/passwd 13.od:examine binary files and output in octal numbers。 例:$ od /usr/bin/nslookup( $ file /usr/bin/nslookup查屬性) 14.sed:a stream editor 例:$ cat –n /etc/passwd > passwd $ sed ‘3,5d’ passwd → 刪除第3~5行。 $ sed ‘5,$d’ passwd → 刪除第5行~最後一行。 $ sed ‘10d’ passwd → 刪除第10行。 $ sed ‘/root/d’ passwd → 刪除root字串所在列數的那一行。 $ sed ‘s/user/barry/’ passwd → 將每列遇到的第一個user字串改成barry。 <note>:替代字串與原字串字數可不同,但tr指令須需相同。 Ex:$ cat passwd | tr user barry → 只替代了barr。 $ sed ‘s/user/mary/g’ passwd → global,將檔案內容中所有user字串改成mary,不限於只修改每列遇到的第一個字串。 $ sed –n ‘34s/user/christal/gp’ passwd → 只列出有改變的行數,記得p一定要搭配 –n。 $ sed ’34,$s/user/michael/g’ passwd $ sed –n ‘3,5p’ passwd $ sed –n ’20,$p’ passwd $ vi file → 編輯內容:35s/user/david/gp $ sed –nf file passwd 以下二者為重導指令 15.tee:唯一可同時將檔案內容輸出至screen及file中。 例:$ cat /etc/fstab | sed ‘s/none/barry/g’ | tee file1 若要避免cover,可用tee –a ( append ) 16.xargs:將前面指令執行結果,當成xargs其後所接指令的引數(argument), 此引數可能為檔案名稱或目錄名稱。 例:$ find /home/barry -name passwd | xargs rm –f $ ls | xargs rm –rf
__________________ 如果您覺得"文章小說討論區"所發的主題不錯 請不要吝嗇 在主題 " 點閱" 及 並給予 發文者一些鼓勵 --------------------------------------------- 為了防止世界被破壞 為了保護世界的和平 貫徹下載與燒錄的邪惡 可愛又迷人的盜版角色 MP3....Program... 我們是穿梭在網際網路中的盜版大隊~~ 拷貝 拷貝的明天正等著我們~~ 就是這樣....喵~~@@ |
|
#2
| ||||
| ||||
| 三. Search text files content using regular expressions ( RE ) RE: $ cat passwd | grep pattern → pattern出現以下特殊符號的意義: .:代表任意單一字元。 Ex:$ cat passwd | grep .ser 〔…〕:代表﹝﹞內的單一字元。 Ex:$ cat passwd | grep〔abc〕user 〔^…〕:不是其內的單一字元。 Ex:$ cat passwd | grep〔^bc〕user *:在 * 的前面一個pattern,可出現0~n次。 Ex:$ cat passwd | grep ba*rry ^:以^其後所接pattern為首。 Ex:$ cat passwd | grep ^user $:以$其前所接pattern為尾。 Ex:$ cat passwd | grep flase$ ?:前面pattern可出現0 -1次。 +:前面pattern可出現1 –n次。 \:跳脫特殊符號,使其變成一般符號。 如:#.&.~.().;.”.…及以上所列。 Ex:$ cat passwd | grep \* 範例:有一file,內容如下: dea50aw*3d6 3fga2x8zw*dx mayxz9w*38d abcdw*d $ cat file | egrep “a.x*[a-z0-9]+w\*3?d” 按enter後,結果為何? 1.2.4 ˙grep:print lines matching a pattern -n:把含有搜尋pattern的lines,附上編號列出來。 -c:只顯示合乎條件的列數。 -v:把不含pattern的lines列出來。 -i:忽略大小寫。 -w:pattern是一個word的,列出來。 例:$ cat passwd | grep user $ cat –n passwd | grep user $ cat passwd | grep –c user $ grep –v user passwd $ grep –in user passwd $ grep –w user01 passwd 四. wildcard:通配字元,萬用字元 1.*:表0~n個字元。 2.?:表任意單一字元。 3.〔…〕:表其內單一字元。 4.{ab,cd}:將其內字串展開。 5.〔!a-z〕:除a-z外的任意單一字元。 例:$ touch file filea fileb filec filed fileab $ ls file* $ ls ????? $ ls file〔a-z〕 $ ls file{a,b,c} $ ls file〔!A-Z〕 五.Vi Editor: 1.command mode:$vi file所進入畫面便是command mode。 此模式下,除←↑↓→可用外,亦可用H(←).J(↓).K(↑).L(→)。 至於翻頁可用pagedown(或ctrl+f)及pageup(或ctrl+b)。 按鍵代表涵意: 數字0 →start of line. $ →end of line. G →go to last line. gg →go to first line=1G b →biginning of word. e →end of word. w →start of next word. (1) 刪除(delete): x:delete character under cursor.(同DEL) X:delete character to left of cursor. dd:delete complete line. 3dd:刪除本行及以下2行,共3行。 db:delete to beginning of word. de:delete to end of word. dw:delete to start of next word. d0:delete to start of line. d$:delete to end of line.=D dG:delete to end of file. 若配合區塊,只須按d即可。 (區塊:v→character, V→line) (2) 取代(replace) r:replace character under cursor. R:enter replace mode.(向右替代) cc:先執行dd,再進入insert mode。 cb:先執行db,再進入insert mode。 其他用法參照上面,不再說明。 配合區塊,用c。 (3) 複製(yank): yy:yank complete line. 5yy:yank five complete lines. yw.y0.y$.yG…… 配合區塊,用y。 (4) 剪下: x:只剪區塊涵蓋處。 X:以行為單位剪下。 (5) 貼上: p:貼於cursor右邊or所在行下面。 P:貼於cursor左邊or所在行上面。 (6) 按u,可一直往回復原。 (7) /pattern:search forward for pattern(從file前面開始找) n:往下找另一個相同的pattern。 N:回上一個相同的pattern。 ?pattern:search backward for pattern(n:往上,N:往下) (8) ZZ:存檔離開 (command mode下) 。 2.insert mode: 由command mode → insert mode方式(最習慣用insert key) a:append after cursor. A:append at end of line. i:insert before cursor. I:insert at start of line. o:open new line below cursor. O:open new line above cursor Press <ESC> to return to command mode. 3.extended mode:(ex mode) (1):s/squid/mary →將squid字串換成mary。只替換cursor所在行(sed是全部)。 亦可於最後面加上 “/”,如:s/squid/mary/ (使用sed替換時,後面一定要有 ” / ” )。 :20,$s/user/michael/g →將第二十行至最後一行執行替換。 (2):5,$d →從第五行刪除至最後一行。 (3):$ →將游標移至最後一行, :1→到首行。 (4):wq →存檔離開, =:x。 (5):w! →強迫存檔,未離開。 (6):q! →強迫離開,未存檔。 (7):w file →將目前檔案內容寫入新檔案file (command mode)中。 (8):e newfile →開啟另一新檔newfile 來編輯。 (9):set nu →將檔案內容設定編號。 (10):set nonu →取消所設定的編號。 六.Use the shell environment: 1. echo command and Quote and alias (1) $ echo very good => very good $ echo very good => very good $ echo “very good” => very good 單引號同結果 $ echo `date` 也等於 $ echo ”`date`” $ echo ‘`date`’ => `date` 會將單引號的內容當成一般文字處理。 $ echo “current directory is `pwd`” (2) $ barry=very good =>command not found(very good中不能有空格) $ barry=”very good”(單引號同,要養成變數後面用雙引號的習慣) $ echo $barry =>very good $ echo “$barry” =>very good $ echo ‘$ barry’ =>$ barry $ barry=”current time is `date`”(此時不能用’ ‘) $ echo $barry =>current time is …… →執行程式的後面及變數內容中含有可執行程式時,絕不能使用單 引號,只能使用雙引號。 $ export barry=very good $ echo $barry =>very 所以須搭配雙引號使用。 (3) 跳脫符號:\ 例:$ echo \$ PATH => $PATH (4) alias:別名 例:$ alias barry= ls =>$ barry $ alias barry=”ls -l” 單引號亦同。 $ unalias barry →取消別名設定 <note>alias別名名稱=執行程式,此執行程式用單、雙引號皆可。 $ alias barry=’ls -l;pwd’ 2.當user login成功後,login程式便會開啟一個shell,以提供user的使用環境。一般而言,linux對每一新增user,都使用預設的shell-bash。 Ex:$ bash =>$ tcsh =>$ exit =>$ exit →表示由此層bash→下層bash→下層tcsh,然後輸入2次exit,退回 原bash。 3.環境變數(Environment Variable):($ env或$ set來看) 所謂使用者的操作環境,即是在shell下的各項設定,設定項目如PATH(路徑)、HOME(家目錄)、PS1(提示號)、MAIL(user信件接收處)、NAME(user名稱)…等等,而其每項環境設定,即是透過環境變數來達成。 4.Shell and Environment Variables(變數類型有此2種) (1)shell(user) variables: local variable:child shell don’t inherit them 變數名稱=變數內容 變數名稱一般由字母、數字、_所組成,且第一個字不能為數字。 Ex:$test_local=tommow $echo $test_local $bash $echo $test_local→顯示無法繼承父shell。 $test_local= 或 $unset test_local →取消變數的方式。 $barry=good =>$ mary=$barry =>$ echo $mary =>good export (global) variables:child shells inherit them Ex:$ export boy=good 上式等於 $ boy=good =>$ export boy $ bash $ echo $boy $ bash $ echo $boy $ unset boy →取消變數 $ pstree 目前位哪層shell $ export var=”/etc/passwd” (2)environment variables:(global) can inherit ˙ 查看目前使用者的環境變數:使用指令查看$ env或$ set,而其中的環境變數在哪定義,目前不得而知,有可能是shell設計師預設定義好的。 ˙ 若要修改環境變數,可於命令列重新定義或透過設定檔來設定。 ˙ 環境變數設定檔:$ man bash => /etc/profile =>尋找bash讀取檔案的順序。 /etc/profile:user login時,首先執行的檔案(bash所讀取)。 其內容定義以下變數:PATH、 USER=`id -un`→username LOGNAME=$USER、 HISTSIZE =1000、 HOSTNAME=`/bin/hostname`、 INPUTRC、 MAIL=”/var/spool/mail/$USER” ~/.bash_profile:如果此檔案存在,便讀取 (redhat)。 ~/.bash_login:若.bash_profile不存在,便讀此檔案。 ~/.profile:若以上兩者不存在,便讀取這個檔案 (openlinux)。 ~/.bashrc:login後會被自動執行。 ~/.bash_logout:logout其間會被執行。 /etc/bashrc:user login shell後,都會採用。 註:openlinux → /etc/config.d/shells/bashrc <note>:redhat: ~ /.bashrc內敘述 if〔-f /etc/bashrc〕;then . /etc/bashrc fi openlinux: ~/.bashrc內敘述 [ -r /etc/config.d/shells/bashrc ] && . /etc/config.d/shells/bashrc common environment variables: PATH:在命令列上輸入指令後,系統會先找尋是否為bash的內鍵命令,若不是,再到PATH中查詢此指令所在目錄是否存在於PATH變數所定義的路徑中,若是→可於命令列下直接輸入指令,若否→需輸入相關路徑。 HISTFILE:定義指令紀錄檔位置(~/.bash_history)。 HISTSIZE,HISTFILESIZE: 定義指令紀錄檔內,所能儲存指 令最大數。 HOME:家目錄位置。 PS1:定義提示號樣式。 \u→username \h→hostname \H→FQDN \W→the present working directory \w→the present working directory (絕對路徑) \d→date \t→24小時制 \T→12小時制 \$→Uid為0者(root)顯示 #,其它人則顯示 $。 \#→顯示指令數(目前輸入第幾個指令)。 Ex:$ PS1=”[\d@\t \W]\\$” PWD:目前所在工作目錄。 範例1:$ PATH=”$PATH:.” “.”表示現在這個目錄,其意思是說將現在這個目錄加入路徑當中。若現於目錄下新增一執行檔(設檔名barry),此時便可於提示號下直接輸入。 →$ barry 但若沒加入路徑,則須寫成 $ ./barry或 $ sh barry 範例2:$ ifconfig =>command not found # which ifconfig => /sbin/ifconfig $ echo $PATH =>找無/sbin目錄 $ PATH=”$PATH: /sbin” =>加入sbin目錄,另外一種方式是修改~/.bash_profile中的PATH,改完後再執行 $ source .bash_profile或 $ . .bash_profile或logout再login。 5. bash history file $ history →顯示使用過的指令,含正在login所使用的指令。 $ cat –n .bash_history →指令紀錄檔案,不含正在login使用的指令。 $ echo $HISTFILESIZE (同HISTSIZE) → .bash_history內,所能儲存最大指令數限制。指令數超過限制時,則刪除前面的指令,以維持設定值。 $ !150 →表執行第150行指令(先用$history看)。 $ !cat →表執行最後面的cat指令(含引數)。 $ !! →執行前一個指令。 6. /etc/skel: $ cd /etc/skel $ ls –a → .bash_profile .bash_logout .bashrc 當新增user時,就會由 /etc/skel 複製一份至新user家目錄下,故管理者可由此目錄下的檔案,設定新user的使用環境。
__________________ 如果您覺得"文章小說討論區"所發的主題不錯 請不要吝嗇 在主題 " 點閱" 及 並給予 發文者一些鼓勵 --------------------------------------------- 為了防止世界被破壞 為了保護世界的和平 貫徹下載與燒錄的邪惡 可愛又迷人的盜版角色 MP3....Program... 我們是穿梭在網際網路中的盜版大隊~~ 拷貝 拷貝的明天正等著我們~~ 就是這樣....喵~~@@ |
|
#3
| ||||
| ||||
| Perform basic file management $ ls –l /etc/passwd -rw-r--r-- 2 root root 1581 9 18 12:30 /etc/passwd →第一欄第一格代表檔案類型(file type),有以下幾種: -:regular file,為一般檔案,有白色(ascii file、hard link)、綠色 →執行檔、紅色→壓縮檔。(openlinux壓縮檔為白色) d:directory,目錄,一般為深藍色。 l:symbolic link file,淺藍色。 c:character devic,黃色。 b:block device,黃色。 例:$ ls –ld /var $ ls –l link (先執行$ln -s /etc/passwd link) $ ls –l /dev/ttys0 (lp0.audio.tty1.psaux…)→字元裝置(與顯示有關)。 $ ls –l /dev/hda (hdd.fd0.hda.sda…)→區塊裝置(與資料儲存有關)。 第一欄2-4格為owner permission,5-7格為group permission,8-10格 為other permission。 第二欄為硬式連結數 (hard link count),目錄的hard link count等於其下 的子目錄數加2(目錄本身及上個目錄要算進去),至於檔案的 hard link count要把本身算進去。 第三欄為owner 第四欄為 group 第五欄為檔案大小,單位為bytes。 接著是檔案的建立時間或修改時間,最後則是檔案名稱。 1. 權限:r(讀)、w(寫)、x(執行) 針對目錄:r(read):表示可以看得到該目錄下的子目錄及檔案名稱清單。 w(write):可新增或刪除其下的子目錄及檔案。 x(execute):可切換到該目錄上。 針對檔案:r:可讀取檔案內容。 w:可寫入檔案內容,就是可修改檔案內容的意思。 x:執行該檔案。 目錄權限 r-- 表不可切換至該目錄,亦不能於其下新增刪除子目錄及檔 案,只能讀取該目錄下的清單。 --x表無法看到此目錄下清單,亦不能於其下新增刪除子目錄及 檔案,只能切換至此目錄。 欲於目錄下新增、刪除子目錄及檔案,則該目錄須開放w、x的權限。 欲於目錄下讀、寫 檔案內容,則該層目錄只須開放x權限。 另外若欲複製檔案至目錄中,執行者須對該檔案的上層目錄有x權限,目 的目錄有w.x權限,對檔案須有r的權限。 至於scripts file,執行此scripts file的使用者需對此file有r.x的權限,對於file的上層目錄需有x權限。 Ex:設 /tmp下有一目錄 /tmp/dir,目錄下有一檔案/tmp/dir/file, drwxr-xr-x root root dir -rw-rw-rw- barry barry file →file擁有者為barry,但無法刪除此file,因上層目錄dir的other無w權限所致。 Ex:drwxr--r-- root group dir -rw-rw-rw- barry barry file,barry屬於group群組 →barry無法修改file內容,因所屬group無x權限。 2. basic UNIX file management commands (1)cp: 一般使用者執行此指令時若未加參數,預設會cover原內容,若為 root使用,則會出現交談模式,因在 ~/.bashrc內有設定別名之故。 而openlinux預設都沒設定,可自行補上,如: alias cp=’cp –i’ alias mv=’mv –i’ alias rm=’rm –i’ -i:interactive交談,→y or n -b:backup -r:(同-R)copy directory’s contents to another recursively. -P ( --parents ):將來源目錄複製到已存在的目錄中。 -s(-d): copy symbolic link file. -l:copy hard link file. -f:force the copy (RedHat預設) -p:preserve file attributes if possible. -a= -dpR→-a為cp指令最常使用參數 Ex. # cp –a /var/spool/mail/* /tmp/dir # cp –pr /home/barry/dir /tmp/dir1 # cp –P /var/log/messages /dir (dir須已存在) →來源目錄下可再接目錄,但最後要接檔案,如此才有意義。 $ cp /etc/passwd “my file” $ cp file1 file2 dir =$ cp f* dir (2)mv: -f -i -b(同上),-f(預設) Ex. $ mv file dir →將file移至目錄dir下。 $ mv file filename →將file改名成filename。故mv可有邊移邊改名的功能。 $ mv -i file1 file2 $ mv –b file1 file2 以上兩參數須對已存在的2 file,才有意義,cp亦同。 (3)mkdir:製造新目錄, -p.-m Ex:$ mkdir newdir $ mkdir “new dir” $ mkdir new\ dir 建立名為new dir的目錄 $ mkdir -- -new-dir 建立-new-dir目錄 $ mkdir -- “-new dir” = $ mkdir -- -new\ dir $ mkdir –m 700 dir1 dir2 $ mkdir –p dir/dir1/dir2 →遞迴製造空目錄。 (4)rmdir:刪除目錄,且必須是空目錄。 Ex:$ rmdir -- -dir $ rmdir –p dir/dir1/dir2 →遞迴刪除空目錄。 (5)rm:刪除檔案及目錄 -i、-r(R)、-f Ex:$ rm file1 file2 file3 $ rm –rf dir file (6)touch:建立空檔案或更新檔案時間。 Ex:$ touch emptyfile $ touch emptyfile oldfile $ cat >newfile → ctrl+d 3. 檔案、.目錄權限及擁有權變更:chmod.chown.chgrp --- --x -w- -wx r-- r-x rw- rwx 000 001 010 011 100 101 110 111 0 1 2 3 4 5 6 7 umask: 預設:$ umask 002表預設目錄權限775,檔案權限664。 # umask 022表預設目錄權限755,檔案權限644。 $ umask 2 表002 $ umask 27 表027 例:$ umask 027 $ mkdir newdir $ touch newfile $ ls –l →drwxr-x--- newdir -rw-r----- newfile (1)chmod(Change Permissions on a file or directory) Only the owner or root can change the permission. <1>symbolic mode: u→user, g→group, o→other, a=u+g+o 例:$mkdir dir dir1 dir2 $ chmod –v g-w dir1 dir2 (755) $ chmod –v go+w dir (777) $ chmod –v a=rw dir (666) $ chmod –v u=rx, g=x, o= dir (510) $ chmod –v u+w,g=rw,o+rx dir (765) $ chmod –v +r newdir →表示讓所有人都有r 的權限,+x是讓所有人都有x 權限, 但無法使用+w。 <2>number mode: 例:$ chmod –v 644 file1 file2 參數:-v→無論權限改變前後是否相同,都會顯示(enter後)。 -c→權限改變後與改變前相同,不顯示,不同才顯示。 -R→目錄下的子目錄及檔案權限會跟目錄相同。 例:$ chmod –Rv 777 dir → 若dir底下有子目錄或檔案,則它們的權限也改成777。 (2) chown(change the user and the group ownership) chgrp(change the group ownership) <note:chmod所使用的參數-v、-c、-R皆可適用於此> only root can change the ownership 例:# chown –v user01.user02 dir →將dir目錄的擁有者改為user01,擁有群組改為user02。也可使用user01:user02 # chown –Rv .user02 dir1 →將dir1的擁有群組改成user02。 # chgrp –Rv user01 dir2 dir3 →將dir2.dir3的擁有群組改為user01。 註: 一般使用者可使用chown.chgrp的時機,除非該檔案的擁有者同 時也屬於群組成員的一分子(預設群組或附屬群組皆可),此時擁有者才可將檔案的擁有群組改為該群組。 進限權限設定: SUID:Set User ID(4). SGID:Set Group ID(2). Sticky bit(1) rwxrw-rw- =>0766 rwsrw-rw- =>4766 rwsrwSrw- =>6766 3644 =>原權限rw-r--r-- =>rw-r-Sr-T SUID:只能使用於執行檔。 當執行檔案時,執行此程式所具備的權限,就是以執行者的ID為準,現若增加SUID,則執行此檔案所具權限,是以檔案擁有者為主,當然本身須具備x權限才行。 Ex:$ ls –l /usr/bin/passwd r-s--x—x (r-sr-xr-x) →passwd指令須寫入 /etc/shadow,而此檔案一般user無法存取,若無SUID,則user無法使用此指令,有SUID則執行此程式所擁有的權限,等於該檔案的擁有者所擁 有的權限。 SGID:用於檔案時,同SUID,只是執行此檔案所具權限是以檔案擁有群 組為主。 用於目錄,則此目錄下,無論誰新增檔案或子目錄(含root),則此檔案或子目錄所屬群組必和此目錄的所屬群組相同。 Ex:barry$ mkdir –m 2775 dir # chgrp –v group dir $ touch dir/file1 # touch /home/barry/dir/file2 # ls –l /home/barry/dir Sticky bit:一般針對目錄而設,而位此目錄下的檔案及子目錄的擁有者(root除外),方有權刪此檔案或子目錄,other縱使有w權限,亦不能刪除 Ex:$ ls –ld /tmp drwxrwxrwt barry$ touch /tmp/file 另一user mary從另一終端機 login mary$ rm –f /tmp/file 無法刪除 <補充>指令chattr. lsattr # chattr +i /home/barry/passwd →設定+i屬性,表passwd 檔案不能被修改、刪除、重命名,root亦 例外。(只有root能刪除此屬性) # lsattr /home/barry/passwd→看檔案屬性。 # chattr –i /home/barry/passwd 取消檔案屬性。
__________________ 如果您覺得"文章小說討論區"所發的主題不錯 請不要吝嗇 在主題 " 點閱" 及 並給予 發文者一些鼓勵 --------------------------------------------- 為了防止世界被破壞 為了保護世界的和平 貫徹下載與燒錄的邪惡 可愛又迷人的盜版角色 MP3....Program... 我們是穿梭在網際網路中的盜版大隊~~ 拷貝 拷貝的明天正等著我們~~ 就是這樣....喵~~@@ |
|
#4
| |||
| |||
| 我剛開始使用 linux 這些內容真的是不錯 感謝感謝 |