2009年11月5日 星期四
~/.bashrc settings (prompt style & color setting)
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[\033[31m\]\u\[\033[00m\]:\W\[\033[31m\]\$\[\033[00m\] '
{
\[\033[31m\]:Red
\[\033[00m\]:end of color setting
}
2009年8月29日 星期六
2009年6月25日 星期四
Linux 基本指令(一)
Linux基本指令
本頁最近更新日期:2007/10/30
以下指令若有「※」標誌的可點入連結觀看進階講解
快速選單:
A B C D
E F G H
I J K L
M N O P
Q R S T
U V W X
Y Z
alias
替指令取別名
◎ alias 列出目前系統所使用的所有指令別名
◎ alias 別名=Linux 指令名稱 此時若輸入「別名」則功能會跟輸入「Linux 指令名稱」相同
◎ 若想要每次開機都使用此別名,在 bash 中,一定要在 .bashrc 中指定,如果是 tcsh 中,則要在 .cshrc 中指定
ar
將許多檔案備存成一個或多個檔案
◎ ar -rcv 備存檔名 欲備存檔名1 欲備存檔名2 ...
將「欲備存檔名1 欲備存檔名2 ...」及所有您指定的檔案,備存至「備存檔名」中。
參數 -r:在新加入組成檔時,檢查備存檔案中的組成黨,如果重複的話,就取而代之。
參數 -c:建立備存檔案。
參數 -v:顯示詳細資訊。
◎ ar -t 備存檔名 列出「備存檔名」中之檔案名稱
◎ ar -x 備存檔名 取出「備存檔名」中所有檔案
◎ ar -x 備存檔名 備存檔中檔案之檔名 取出「備存檔名」中某些檔案
bg
將程式置於背景執行
如果您執行的程式很多的話,可以在執行程式時,按下「Ctrl + z」暫停程式,
然後利用指令「jobs」查詢該工作之編號
◎ bg 工作編號 將「工作編號」之工作置於背景執行
◎ fg 工作編號 將「工作編號」之工作置於前景執行
bc
簡易計算機
◎ quit 離開 bc
clear
清空畫面
若是您嫌畫面東西太多、太雜了,不妨試試這個指令
cmp
列出兩個檔案的文字內容第一個相異處
◎ cmp 檔名1 檔名2 若輸入此指令,電腦會將「檔名1、檔名2」中第一個字元相異處列印出來
cp
複製檔案
◎ cp 檔名1 檔名2 複製「檔名1」至「檔名2」,若「檔名2」原本就存在,將會被覆蓋
◎ cp -r 資料夾名1 資料夾名2 複製「資料夾名1」至「資料夾名2」
df
檢視電腦硬碟之使用情形
◎ df -h 以人類較易閱讀的方式列出資料
diff
比較兩個檔案的文字內容有何不同處
◎ diff 檔名1 檔名2 若輸入此指令,電腦會將「檔名1、檔名2」中有字元相異的那行列印出來
du
取得資料夾大小
◎ du 資料夾路徑 取得該「資料夾路徑」中所有目錄 (包含其下子目錄) 的資料夾大小
◎ du -s 資料夾路徑 取得該「資料夾路徑」的資料夾大小 (不包含子目錄)
◎ du -h 資料夾路徑 以人類較易閱讀的方式顯示該「資料夾路徑」的資料夾大小 (不包含子目錄)
fg
將程式置於前景執行
相對於指令「bg」,此指令用來將程式置於前景執行
可利用指令「jobs」查詢該工作之編號。
◎ fg 工作編號 將「工作編號」之工作置於前景執行
free
查看記憶體使用狀況
◎ -m 以 MB 為單位
◎ -t 顯示記憶體總合
◎ -s 秒數 每隔「秒數」顯示一次記憶體狀況
grep
搜尋檔案中的字串
◎ grep "字串" * 搜尋此層目錄中,包含「字串」的所有檔案,若「字串」中的英文字元大小寫不同,將被視為不同字串
◎ grep -i "字串" * 搜尋此層目錄中,包含「字串」的所有檔案,若「字串」中的英文字元大小寫不同,將被視為相同字串
◎ grep -r "字串" ./ 遞迴搜尋此層目錄包含其下所有子目錄中,包含「字串」的所有檔案,若「字串」中的英文字元大小寫不同,將被視為不同字串
history
列出曾經用過的指令
◎ !編號 可執行 history 中該編號的指令
◎ history 數字 可列出最近「數字」項指令
◎ history -d 編號 可刪除該「編號」的指令
◎ history -c 刪除 history 中所紀錄的所有指令
ifconfig
設定、檢視網路介面
此指令用以檢視、設定網路卡介面
◎ ifconfig 列出系統中所有的網路卡介面及其相關設定,包含 IP address, MAC address 等資訊
◎ ifconfig 網路介面 IP位址 設定「網路介面」的 IP 位址為「IP位址」,如:ifconfig eth0 192.168.0.3,即是設定 eth0 的 IP 位址為 192.168.0.3
◎ ifconfig 網路介面 netmask IP位址 設定「網路介面」的子網路遮罩為「IP位址」,如:ifconfig eth0 255.255.255.0,即是設定 eth0 的子網路遮罩為 255.255.255.0
◎ 其他與網路相關的設定,若您是使用 Fedora Linux Distribution,可至
/etc/sysconfig/networking/profiles/default/ifcfg-eth0 檔案中設定,該設定檔中,若
BOOTPROTO=dhcp 表示此電腦要使用 DHCP 自動取得 IP 位址,若要自己指定 IP 位址,則 BOOTPROTO 必須要設定成
static (即 BOOTPROTO=static); 另外,還有些較常用參數,像是
IPADDR=IP位址,NETMASK=子網路遮罩位址,以及 GATEWAY=預設閘道等都可在此設定檔中做設定,待設定完畢存檔後,便可透過
ifdown 和 ifup 指令重新啟用網路介面套用新的設定值。
ifdown
停止網路介面運作
此指令用可與 ifup (啟動網路介面) 配合使用
◎ ifdown eth0 停止「eth0」網路介面運作
ifup
啟動網路介面
此指令用可與 ifdown (停止網路介面運作) 配合使用
◎ ifup eth0 啟動「eth0」網路介面
kill
終止系統中的 proces
此指令可配合 ps 指令使用,先利用 ps 指令查詢欲終止 process 的 PID,再利用 kill 指令終止該 process
◎ kill -9 PID 終止系統中編號為「PID」的 process
ln
建立檔案或目錄的連結檔
若修改透過 ln 指令建立出來的連結檔,原本的檔案內容也會跟著改變
◎ ln 檔名 連結檔名稱 建立「檔名」的硬連結檔,而該連結檔名為 「連結檔名稱」
◎ ln -s 檔名 連結檔名稱 建立「檔名」的符號連結檔,而該連結檔名為 「連結檔名稱」
ls
列出該資料夾的檔案名稱
◎ ls 列出資料夾中檔案的檔案名稱
◎ ls 檔名 只列出「檔名」的資料
◎ ls 資料夾名稱 列出「資料夾名稱」內的資料
◎ ls -l 列出檔案的詳細介紹,包含有存取權限、檔案擁有者、內含字元總數、最後更動檔案時間及檔名...等
◎ ls -t 依「最後更動檔案時間」排序,由最近排至最久
◎ ls -r 與原本排序相反地(reverse)排列
◎ 依此類推,則 ls -rtl 為列出所有檔案的詳細資料,並用「最後更動檔案時間」排序,方法為最久排至最近
lsmod
列出目前 linux kernel 中所有 modules 的狀態
可透過此指令檢視系統中目前安裝了那些 modules
mail
個人信件工具
◎ 在命令列輸入 mail 即可進入 mail 程式內收發信件
◎ 若想寫信給他人,則在命令列輸入 mail 收信者帳號 ,即可發送信件給「收信者」
◎ 若想將既有的文字檔當成信件內容寄給他人,則在命令列輸入 mail 收信者帳號 -s 信件主題 < 文件檔名稱
即可將「文件檔名稱」發送給「收信者」
在 mail 程式中
◎ 輸入 ? 查詢所有指令
◎ 輸入 h 列出所有信件
◎ 輸入 t 閱讀信件
◎ 輸入 n 閱讀下一封信件
◎ 輸入 d 刪除信件
◎ 輸入 u 救回刪除信件
◎ 輸入 R 回覆信件
◎ 輸入 m 收信者帳號 發送信件給「收信者」
◎ 輸入 q 離開 mail 程式,並將閱讀過的所有信件存至「mbox」檔案
◎ 輸入 x 離開 mail 程式
man
列出 Linux 主要的說明檔
◎ man 區段清單 說明的主題 列出關於「說明的主題」之相關訊息
1:使用者指令 | 2:系統呼叫 | 3:程式庫呼叫 |
4:裝置 | 5:檔案格式 | 6:遊戲 |
7:雜項 | 8:系統指令 | 9:核心內部指令 |
mv
移動檔案( 檔案更名 )
◎ mv 檔名1 檔名2 移動「檔名1」至「檔名2」,若「檔名2」原本就存在,將會被覆蓋
netstat
檢視目前電腦的網路連線情況
◎ netstat -a 列出電腦中 listening 和 non-listening 的連線
◎ netstat -n 列出電腦現有的連線時,列出 IP 位址就好,不必列出 domain name
◎ netstat -t 列出電腦中現有的 TCP 連線
◎ netstat -u 列出電腦中現有的 UDP 連線
ntsysv
設定系統開機時要自動啟動的服務
passwd
設定密碼
◎ passwd -d 使用者名稱 刪除「使用者名稱」之密碼
◎ passwd -l 使用者名稱 鎖住「使用者名稱」之帳號,使之失效
ps
列出系統中 processes 的執行狀況
◎ ps -f 列出 processes 的完整資訊,包含 UID (User ID)、PID (Process ID)、PPID (Parent Process ID) 等資訊
◎ ps -A 列出系統中所有的 processes
rm
刪除檔案
◎ rm 檔名 刪除「檔名」
◎ rm -r 資料夾名 刪除「資料夾名」
rpm
RPM 套件管理員 (package manager)
如果您要替 Linux 系統安裝新的軟體,不妨先上網找找看網路上是否有已經包裝好的 RPM packet,若找的到,則安裝軟體的過程就省事多了
◎ rpm -q 套件名稱 查詢目前系統中是否有安裝「套件名稱」這個套件
◎ rpm -ivh 套件名稱 安裝名為「套件名稱」的套件於系統中,並在安裝過程中顯示文字訊息
◎ rpm -u 套件名稱 升級系統中名為「套件名稱」的套件
◎ rpm -e 套件名稱 解除安裝系統中名為「套件名稱」的套件
sort
將文字檔案內容排序並印出
◎ sort 檔名 將文字檔案「檔名」內容排序並印出(由小至大排序)
◎ sort -r 檔名 將文字檔案「檔名」內容排序並印出(由大至小排序)
◎ sort -f 檔名 將文字檔案「檔名」內容排序並印出(不管字元之大小寫,由小至大排序)
su
切換登入身份
◎ su 切換登入身份到 root
◎ su -l 使用者帳號 切換登入身份到「使用者帳號」
◎ su -c "指令" 以 root 的權限執行「指令」
tail
列出文字檔案尾端內容
◎ tail 檔名 列出「檔名」最後十行
◎ tail -n 檔名 列出「檔名」最後 n 行
◎ tail +n 檔名 從 n 行開始列出「檔名」到最後
tar
建立/解開 .tar 封裝檔
◎ tar -cvf 封裝檔名稱.tar 資料夾名稱 將 「資料夾名稱」 封裝成為 「封裝檔名稱.tar」檔案
◎ tar -czvf 壓縮封裝檔名稱.tar.gz 資料夾名稱 將 「資料夾名稱」 壓縮成為 「壓縮封裝檔名稱.tar.gz」壓縮檔
◎ tar -cjvf 壓縮封裝檔名稱.tar.bz2 資料夾名稱 將 「資料夾名稱」 壓縮成為 「壓縮封裝檔名稱.tar.bz2」壓縮檔
◎ tar -xvf 檔名.tar.gz 解開 「檔名.tar」封裝檔
◎ tar -xzvf 檔名.tar.gz 解開 「檔名.tar.gz」壓縮封裝檔
◎ tar -xjvf 檔名.tar.bz2 解開 「檔名.tar.bz2」壓縮封裝檔
top
列出目前系統中正在執行的工作,以及每個工作所分配到的記憶體空間與 CPU 使用百分比
useradd
建立使用者帳號
◎ useradd 新使用者帳號 建立 「新使用者帳號」於此 Linux 系統中
※vim
開啟編輯文字檔程式
在指令模式下輸入 vi 檔名 或 vim 檔名 即可進入 VIM 程式
wc
計算文件檔之行數、字數及字元數(word-counting)
◎ wc 檔名若輸入此指令,則電腦會列出「檔名」內文字之行數、單字數及字元數
◎ wc -l 檔名列出「檔名」內文字之行數
◎ wc -w 檔名列出「檔名」內文字之單字數
◎ wc -m 檔名列出「檔名」內文字之字元數
◎ wc -L 檔名列出「檔名」內最長那行文字所包含的字元總數
whereis
查詢指令執行檔所在位址
◎ whereis 指令名稱查詢 指令名稱 所在目錄
who
列出所有線上的使用者
◎ 若想知道自己是誰,可利用指令 whoami 來查詢
UltraEdit 使用 CTAG
CTAGs and UltraEdit
What is Ctags?
Ctags is a program that generates a tag (or index) file of objects and functions found in C/C++ header and source files. The tag file allows these items to be referenced quickly from within UltraEdit.
What does Ctags do?
The purpose of Ctags is to allow you to quickly find a function or object that is defined elsewhere in the active file or another file (i.e. header or source file). You can use this feature to navigate through your project to find the various functions, variables, class members, and so on...
After you have generated your tag file, you simply use the "Find Symbol" in the Search menu to find the symbol (located next to the cursor in the active document) elsewhere in the file(s).
UltraEdit supports Ctags, either as part of a project, or independently.
Using Ctags
Ctags EXE
UltraEdit relies on an external tag file generator. There are many versions of Ctags; however, we recommend "Exuberant Ctags" available from http://ctags.sourceforge.net/. UltraEdit includes a Ctags EXE in the GNU folder (in the installation directory) as part of the standard installation. Therefore, you would not need to download/install Ctags to use this feature.
UltraEdit can make use of an existing tagfile, or if an EXE for creating Ctags is available it can also create the tag file. If you prefer to use a different Ctags version, you may specify this under Ctag Options in the Project menu.
PLEASE NOTE: IDM Computer Solutions, Inc. does not support Ctags (the program) and we can't offer definitive answers on how Ctags itself works. We can help with information on standard configuration for integration with UltraEdit and how Ctags should work in conjunction with UltraEdit. For the most comprehensive and up-to-date information regarding Exuberant Ctags please see http://ctags.sourceforge.net/. The latest version of the manual for Exuberant Ctags should be available at http://ctags.sourceforge.net/ctags.html.
The tag file is the index file of the various functions, class members, etc. In order to use Ctags, you must first create the Tag file.
You can create the Tag file using the Create CTAG file menu item in the Project menu.
If you prefer, you may also select the option Create CTAG file on project load in the Project Settings dialog in the Project Menu. This will cause the Ctags file to be created automatically when the project is loaded. This essentially ensures that you have an updated Ctags file every time you open the project.
As we've previously discussed, the purpose of Ctags is to be able to quickly find a function or object that is defined elsewhere in the active file or another file (i.e. header or source file).
Using Ctags is simple. When editing source files, you may position the cursor over a function, variable, class member, or macro and press F11. This activates the Find Symbol functionality (in the Search menu) which positions the cursor on the definition of the item you searched for.
If the specified item (symbol) exists in the active document, focus will shift to the appropriate line in the active file. If the specified item (symbol) exists in a different file, that file will be opened to the line where the symbol is defined. Let's give it a try...
Now that we've created the Tag file in Step 1, you may place your cursor next to a symbol or tag (function, class member, variable, etc).
You may now select the Find Symbol item from the Search menu (or press F11). UltraEdit, in conjunction with the Tag file, will search for the symbol.
If only one match is found
The file will be opened (in UltraEdit) and the cursor positioned at the symbol.
If multiple matches occur
A dialog box will be shown giving you the option to position the cursor at the appropriate tag.
If a tag cannot be found
A dialog will be displayed indicating that the tag could not be found.
Using our sample project, the definition of "UpdateClientWindow" was found on line 32 and the cursor is positioned at the beginning of the line.
As we've previously mentioned above, Ctags is intalled as part of the standard installation, so it is not necessary to download/install the Ctags exe. However, UltraEdit does allow you to configure some options in Ctags.
The Ctag Options dialog (in the Project menu) allows you to change options related to Ctags:
The following three items may be defined using the Tag File Options dialog:
Default Tagfile
This specifies the tagfile to be used if there is not a specific project tagfile active (defined in the Project Settings dialog under the Project menu). Note: This must be present if there is not a project-specific tagfile for UltraEdit to look up the symbols associated with the active project.
Ctag EXE Path/Name
This specifies the full path and name of the Ctag exe that will be run to create the tag file from within UltraEdit. UltraEdit can reference a tagfile for symbols if this is not present, but it can not create a new Tag file. The default setting for this is "ctags.exe" in the "GNU" subdirectory under the UltraEdit installation directory.
CTAG Parameters
This specifies the parameters to be passed to the Ctags exe for it to function correctly. The default settings ( -L %fi -f %fo ) are configured for Exuberant Ctags as described above.
Troubleshooting/Tips
-L file This instructs Ctags to read from file a list of file names for which tags should be generated. If file is specified as "−", then file names are read from standard input. File names read using this option are processed following file names appearing on the command line. Options are also accepted in this input. If this option is specified more than once, only the last will apply.
Note: File is read in line-oriented mode, where a new line is the only delimiter and non-trailing white space is considered significant, in order that file names containing spaces may be supplied (however, trailing white space is stripped from lines); this can affect how options are parsed if included in the input.
%fi
%fi on the parameter line is replaced when the ctag exe is run with the filename/path of a file that contains a list of files that the ctags.exe will use to create its tags from. If a project is loaded in UltraEdit, UltraEdit creates a temporary file with a list of the project files and passes this in place of %fi.
-f tagfile
-f tagfile instructs Ctags to use the name specified by tagfile for the tag file (default is "tags", or "TAGS" when running in etags mode). If tagfile is specified as "−", then the tag file is written to standard output instead. Ctags will stubbornly refuse to take orders if tagfile exists and its first line contains something other than a valid tags line. This will save your neck if you mistakenly type "ctags −f *.c", which would otherwise overwrite your first C file with the tags generated by the rest! It will also refuse to accept a multi-character file name which begins with a ’−’ (dash) character, since this most likely means that you left out the tag file name and this option tried to grab the next option as the file name. If you really want to name your output tag file "−ugly", specify it as "./−ugly". This option must appear before the first file name. If this option is specified more than once, only the last will apply.
%fo
%fo on the parameter line is replaced with the full filename/path of the tag file to be created. This would be the tag file specified in the project, or the Default Tagfile defined above.
All Options
For further options for Exuberant Ctags please see http://ctags.sourceforge.net/ctags.html.
--langmap=map[,map[...]] controls how file names are mapped to languages (see SOURCE FILES, at http://ctags.sourceforge.net/ctags.html). Each comma-separated map consists of the language name (either a built-in or user-defined language), a colon, and a list of file extensions and/or file name patterns. A file extension is specified by preceding the extension with a period (e.g. ".c"). A file name pattern is specified by enclosing the pattern in parentheses (e.g. "([Mm]akefile)"). If appropriate support is available from the runtime library of your C compiler, then the file name pattern may contain the usual shell wildcards common on Unix (be sure to quote the option parameter to protect the wildcards from being expanded by the shell before being passed to ctags). You can determine if shell wildcards are available on your platfom by examining the output of the --version option, which will include "+wildcards" in the compiled feature list; otherwise, the file name patterns are matched against file names using a simple textual comparison.
If the first character in a map is a plus sign, then the extensions and file name patterns in that map will be appended to the current map for that language; otherwise, the map will replace the current map. For example, to specify that only files with extensions of .c and .x are to be treated as C language files, use "--langmap=c:.c.x"; to also add files with extensions of .j as Java language files, specify "--langmap=c:.c.x,java:+.j". To map makefiles (.e.g files named either "Makefile", "makefile", or having the extension ".mak") to a language called "make", specify "--langmap=make:([Mm]akefile).mak". To map files having no extension, specify a period not followed by a non-period character (e.g. ".", "..x", ".x."). To clear the mapping for a particular language (thus inhibiting automatic generation of tags for that language), specify an empty extension list (e.g. "--langmap=fortran:"). To restore the default language mappings for all a particular language, supply the keyword "default" for the mapping. To specify restore the default language mappings for all languages, specify "--langmap=default". Note that file extensions are tested before file name patterns when inferring the language of a file.
If you were to specify this in the Ctag Options dialog in the Project menu you could use
"--langmap=c:.c.x -L %fi -f %fo" with the default parameters so that files with a ".x" extension would be parsed as C files. The "-L" option which indicates that the files to be parsed are to be read from the specified file would still require that these files be part of the project for this to work.
The -n option may be added so that only line numbers would be used in the tag file for locating tags. This has four advantages:
1. Significantly reduces the size of the resulting tag file.
2. Eliminates failures to find tags because the line defining the tag has changed, causing the pattern match to fail.
3. Eliminates finding identical matching, but incorrect, source lines.
4. Retains separate entries in the tag file for lines which are identical in content.