在Linux下,我們很多操作都是通過指令來實作的,所接下來我們就詳解介紹Linux下指令的使用。首先要使用指令就得先知道他的格式:
Linux指令的文法格式是 command options arguments,其中選項和參數是可以省略的。Linux下面指令是有多種的,有系統自身的指令、安裝的應用程式指令、腳本執行所用的指令。
ls :list directory contents 用來列出目錄内容的,
ls /path/to/dir|file[路徑] dir是目錄名 file檔案名
[root@linux_basic ~]#ls
anaconda-ks.cfg blank gldt1210.png install.log.syslog tcpestd.sh
bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm blankdir install.log tcpestd1.sh vbird_server
不使用路徑,則是顯示目前目錄下的内容
[root@linux_basic ~]#ls anaconda-ks.cfg 其中anaconda-ks.cfg是檔案
anaconda-ks.cfg
檢視目前所使用的shell
[root@linux_basic ~]#echo $BASH
/bin/bash
我們在指令行下操作的是shell,我們輸入指令shell把指令送出給核心如果指令可執行,則核心把執行結果給我們傳回,其實shell本身也是有内置指令的,可以通過type指令來檢視指令是屬于内置指令還是外部指令。
内置指令:是有shell程式自身提供的指令
外部指令:其本身是一個獨立的可執行程式檔案,指令名即是其程式檔案名字
那我們輸入指令後,系統是如何查找指令的呢?
對于内置指令,則直接到shell内部查找即可;
對于外部指令,則是通過環境變量PATH中指定的路徑來查找的
環境變量是指全局的變量,一處定義後,在接下來的操作(不換終端的前提下)都生效,要使其永久生效則要寫到配置檔案(此處不講解)中。
通過type指令來檢視所用的指令是内置的還是外部指令
[root@linux_basic ~]#type cd
cd is a shell builtin
[root@linux_basic ~]#type echo
echo is a shell builtin
[root@linux_basic ~]#type type
type is a shell builtin
[root@linux_basic ~]#type cat
cat is hashed (/bin/cat)
在指令的格式中,options是用來調整指令的作用方式的
有兩種格式:長選項和短選項
其中長選項是不能合并使用的,短選項可以
短選項形如: -l , -a, -H 使用多個短選項用空格隔開
長選項形如: --list, --all, --help
在此處需要主要,有些選項是需要帶參數的,形如 useradd -u uid
cd:改變目前工作目錄,在不指定選項和參數時,是調到目前使用者家目錄下的。
[root@linux_basic boot]#cd
[root@linux_basic ~]#pwd
/root
也可以通過cd ~目前家目錄下 cd ~username 調到指定使用者家目錄下
cd - 在前一次所在目錄和現在所在目錄之間來回進行切換
[root@linux_basic ~]#ls -a
. .bash_history .bashrc blankdir install.log .mysql_history tcpestd1.sh vbird_server
.. .bash_logout bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm .cshrc install.log.syslog .rnd tcpestd.sh .viminfo
anaconda-ks.cfg .bash_profile blank
其中有兩個獨特的路徑 . 是指目前目錄 .. 上一級的目錄
Linux指令行下,還可以通過上下鍵來檢視自己使用過的指令,其實Linux下是有指令曆史的,指令曆史檢視通過history來檢視的
如果在指令行上輸入資訊有錯可以通過Ctrl + c在終止目前指令的執行
[root@linux_basic ~]#cd /etc/init.d/jdf^C
[root@linux_basic ~]#
在指令曆史中儲存的指令是用限制的,可以通過HISTSIZE來檢視
[root@linux_basic ~]#echo $HISTSIZE
1000
指令曆史儲存的檔案為使用者家目錄的.bash_history當中,指令儲存是要在使用者退出後,才會儲存的。
指令曆史檔案也通過一個環境變量來儲存了就是HISTFILE
[root@linux_basic ~]#echo $HISTFILE
/root/.bash_history
[root@linux_basic ~]#type history
history is a shell builtin
顯示最近使用過的n條指令,包括目前指令自身
[root@linux_basic ~]#history 10
1047 cd
1048 pwd
1049 ls
1050 ls -a
1051 history
1052 echo $HISTORY
1053 echo $HISTSIZE
1054 echo $HISTFILE
1055 type history
1056 history 10
為了保證系統安全,不讓别人檢視到指令曆史
可以使用history -c來清空所有的指令曆史
也可以通過删除指定位置處的曆史指令,要使用選項-d
1055 history 10
1056 man history
1057 whatis history
1058 man 1 history
1059 history -d 159
1060 history 5
1061 history -d 1059
1062 history
[root@linux_basic ~]#history -d 1059
1054 type history
1055 history 10
1059 history 5
1060 history -d 1059
1061 history
1062 history -d 1059
1063 history 10
history -a [/path/to/some_history_file]: 手動将目前會話中的指令曆史寫入指定檔案
[ -w 寫入指令曆史檔案中并且追加到指令曆史的清單中 ]
隻有在使用者退出時,指令的曆史才會自動儲存到.bash_history檔案中
bash調用指令曆史中的指令:
!#: 執行指令曆史中的第#條指令
1064 ls
1065 cd /boot/
1066 hitory
1067 history
1068 ls
1069 cd
1070 history
[root@linux_basic ~]#!1068
ls
!!: 執行上一條正确執行了的指令
!string: 執行指令曆史中最近一次以string開頭的指令;
[root@linux_basic ~]#!s
su - cactiuser
[cactiuser@linux_basic ~]$
!$: 調用上一條指令的最後一個參數
[root@linux_basic ~]#ls /boot/grub/
device.map fat_stage1_5 grub.conf jfs_stage1_5 minix_stage1_5 splash.xpm.gz stage2 vstafs_stage1_5
e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 menu.lst reiserfs_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
[root@linux_basic ~]#ls !$
ls /boot/grub/
e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 menu.lst reiserfs_stage1_5 stage1 ufs2_stage1_5
ESC, .:功能同上 按Esc鍵後加'.'号
從使用上述指令我們知道,Linux有如此多的指令又有如此多的參數和選項,我們怎麼能記住呢?
是以接下來我們來通過使用幫助可以快速的了解到指令具體都用哪些選項和參數。
其中内置指令的幫助資訊都可以通過: help command來檢視的
[root@linux_basic ~]#help cd
cd: cd [-L|-P] [dir]
Change the shell working directory.
外部指令:
1、command --help 絕大部分指令都是支援的,擷取指令簡要幫助資訊
[root@linux_basic ~]#cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines
-e equivalent to -vE
在Linux上指令的幫助資訊檔案所在的位置是/usr/share/man/位置之一
[root@linux_basic ~]#ls /usr/share/man/
bg da el es fr hu it ko man1 man1x man2x man3p man4 man5 man6 man7 man8 man9 mann overrides pt ro sk sv zh_CN
cs de en fi hr id ja man0p man1p man2 man3 man3x man4x man5x man6x man7x man8x man9x nl pl pt_BR ru sl tr zh_TW
Linux指令顯示語言存放的位置
[root@linux_basic ~]#cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
在檢視幫助資訊時,有顯示亂碼,可以修改顯示的語言
[root@linux_basic ~]#echo $LANG
en_US.UTF-8
# export LANG=en 導出LANG,這裡是修改後目前是生效的
Linux幫助檔案下有man,其實是manual的縮寫的,同樣Linux下是遵守可以簡寫則都簡寫的規則的。
通過man來檢視幫助資訊
root@linux_basic ~]#man tty
TTY(1) User Commands TTY(1)
NAME
tty - print the file name of the terminal connected to standard input
SYNOPSIS
tty [OPTION]...
DESCRIPTION
Print the file name of the terminal connected to standard input.
-s, --silent, --quiet
print nothing, only return an exit status
--help display this help and exit
--version
output version information and exit
AUTHOR
Written by David MacKenzie.
REPORTING BUGS
Report tty bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report tty translation bugs to <http://translationproject.org/team/>
COPYRIGHT
Copyright ? 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
The full documentation for tty is maintained as a Texinfo manual. If the info and tty programs are properly installed at your site, the
command
info coreutils 'tty invocation'
should give you access to the complete manual.
man手冊是用章節之分的,可以通過whatis指令檢視指令的章節,是從1-9
[root@linux_basic ~]#whatis tty
tty (1p) - return user's terminal name
tty (1) - print the file name of the terminal connected to standard input
tty (4) - controlling terminal
tty ioctl [tty_ioctl] (4) - ioctls for terminals and serial lines
注意:whatis根據資料庫執行查找操作,此庫為系統定期更新;可使用makewhatis手動更新;
man檢視手冊中各部分資訊
手冊的段落:
NAME: 指令名稱
DESCRIPTION: 指令功能的較長的描述
OPTIONS: 所有選項
SYNOPSIS: 使用格式
EXAMPLES: 使用示例
FILES: 與目前指令相關的配置檔案
SEE ALSO: 可參考的其它手冊
幫助中的格式字串:表示指令書寫時應安裝的格式
[]:可省略
<>: 不可省略
|: 二選一或多選一,不能同時出現
...: 同類内容可以出現多個
在man下資訊很多時,為友善檢視,可以使用
上下翻屏
空格鍵:向檔案尾部翻一屏
b: 向檔案首部翻一屏
j,Enter鍵:向檔案尾部翻一行
k: 向檔案首部翻一行
Ctrl+d: 向檔案尾部翻半屏
Ctrl+u: 向檔案首部翻并屏
還可以進行字串搜尋:
/string: 從檔案首部向尾部進行搜尋
?string: 從檔案尾部向首部進行搜尋
n: 顯示找到的下一個 顯示尋找字元在下一次出現的位置
N:顯示找到的上一個 顯示尋找字元在上一次出現的位置
退出man手冊使用:
q
注意:man能夠為除指令之外的配置檔案、系統調用、庫調用等都能提供幫助手冊,它們分别位于不同的章節中; 可以使用man man來檢視
[root@linux_basic ~]#man man
man(1) man(1)
man - format and display the on-line manual pages
man [-acdDfFhkKtwW] [--path] [-m system] [-p string] [-C config_file] [-M pathlist] [-P pager] [-B browser] [-H htmlpager] [-S sec-
tion_list] [section] name ...
man formats and displays the on-line manual pages. If you specify section, man only looks in that section of the manual. name is nor-
mally the name of the manual page, which is typically the name of a command, function, or file. However, if name contains a slash (/)
then man interprets it as a file specification, so that you can do man ./foo.5 or even man /cd/foo/bar.1.gz.
See below for a description of where man looks for the manual page files.
1: 使用者指令 User Commands
2: 系統調用 System Calls
3: C庫函數 C Library Functions
4: 裝置和特殊檔案 Devices and Special Files
5: 檔案和約定 File Formats and Conventions
6: 遊戲說明 Games et. Al.
7:雜項 Miscellanea
8:系統管理者工具 System Administration tools and Deamons
很多應用程式都自帶有幫助文檔:/usr/share/doc/
ChangeLog: 程式版本更新的變動情況
INSTALL: 安裝方法說明
README:程式說明資訊
我們學習Linux時,可以多閱讀,對應發型版的官方文檔
遇到沒見過的指令多去gfsoso.com搜尋,記住要勤搜尋,因為很多問題别人都已經遇到過了
歡迎大家指出修正錯誤