Linux命令入門雜談

Linux學(xué)習(xí)任重道遠捂掰,對于初學(xué)者來說敢会,看到多如繁星的命令,就讓人望而卻步这嚣,接下來就說說Linux命令的一些入門學(xué)習(xí)鸥昏,希望我可以給看到的人幫助。
我認為學(xué)習(xí)Linux命令分為下面三個方面姐帚,命令的格式吏垮、分類以及執(zhí)行過程,最后重要的是,自己學(xué)會如何學(xué)習(xí)使用一個剛了解的命令惫皱。

一:命令的格式:

格式:命令 選項(長選項 短選項) 參數(shù)(例如 文件名 路徑 用戶等等)
例如:
(1像樊、多個選項或多個參數(shù)用一個空格的空白字符隔開

[root@centos7 ~]# ls -a -l /home
total 4
drwxr-xr-x. 3 root root 18 Nov 8 16:16 .
dr-xr-xr-x. 18 root root 235 Nov 8 16:16 ..
drwx------. 14 wang wang 4096 Nov 8 16:30 wang
[root@centos7 ~]#

[root@centos7 ~]# touch a.txt b.txt
[root@centos7 ~]# ls
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
[root@centos7 ~]#

(2、一條命令很長旅敷,寫不完,可以加反斜杠( \) 颤霎,分成多行來寫

[root@centos7 ~]# shutdown -h
18:30
'system will be down !!!'
Shutdown scheduled for Fri 2017-11-10 18:30:00 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#

注意:單命令直接后跟一個反斜杠就可以媳谁,如果是帶有選項和參數(shù)的命令,保持每行最后先打一個空格友酱,在跟一個反斜杠
(3晴音、多條命令一起執(zhí)行,可以用分號( 缔杉;)隔開

[root@centos7 ~]# cat /etc/issue;ls;pwd
Users is \u
Tty is \l
Time is \t
Date is \d
Hostname is \n
\S
Kernel \r on an \m
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
/root
[root@centos7 ~]#

(4锤躁、取消或結(jié)束命令 ctrl+c(強制退出)或者ctrl+d(正常退出)

[root@centos7 ~]# sleep 1000
^C
[root@centos7 ~]#

二:命令的分類:

(1內(nèi)部命令:被構(gòu)建在shell之中,常駐內(nèi)存或详,在任何時候都可以使用系羞,執(zhí)行速度非常快
(2外部命令:都是以文件的形式存在霸琴,是一個個單獨的可執(zhí)行文件椒振,存放于磁盤

如何知道是內(nèi)部命令還是外部命令呢?
用法:#type 后跟命令 查看給定的指令為“外部指令”梧乘、“命令別名”或者“內(nèi)部指令”
-a 后跟命令 在環(huán)境變量“PATH”指定的路徑中澎迎,顯示給定指令的信息,包括命令別名

例如:查看cat和cd选调、ls是什么命令夹供?

[root@centos7 ~]# type cat
cat is /usr/bin/cat //有路徑顯示,表示為外部命令
[root@centos7 ~]# type cd
cd is a shell builtin //顯示shell builtin 仁堪,表示內(nèi)部命令
[root@centos7 ~]#
[root@centos7 ~]# type ls
ls is aliased to `ls --color=auto' //顯示alias哮洽,表示為命令別名
[root@centos7 ~]#

注意:有些命令是內(nèi)部命令,外部命令也有
例如:

[root@centos7 ~]# type -a echo
echo is a shell builtin
echo is /usr/bin/echo
[root@centos7 ~]#

也就解釋了系統(tǒng)某些內(nèi)部命令丟失枝笨,也還是可以執(zhí)行的原因

關(guān)于內(nèi)部命令的啟用和禁用
用法:enable 列出已開啟的內(nèi)部命令
后跟命令 啟用命令
-n 后跟命令 禁用命令
-n 查看被禁用的命令

舉例:
查看啟用的內(nèi)部命令:

[root@centos7 ~]# enable -a
enable .
enable :
enable [
enable alias
....................
enable unset
enable wait
[root@centos7 ~]#

禁用內(nèi)部命令:

[root@centos7 ~]# enable -n pwd
[root@centos7 ~]#

查看被禁用的命令:

[root@centos7 ~]# help
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally. Type help' to see this list. Typehelp name' to find out more about the function name'. Useinfo bash' to find out more about the shell in general.
Use man -k' orinfo' to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or history -anr>
(( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS;>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x comma>
: kill [-s sigspec | -n signum | -sigspec] pid>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] *pwd [-LP]
...
hash [-lr] [-p pathname] [-dt] [name ...] while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
[root@centos7 ~]#

命令前面有個星號袁铐,表示已禁用,也可用enable命令查

[root@centos7 ~]# enable -n
enable -n pwd
[root@centos7 ~]#

關(guān)于別名命令別名使用:
用法: #alias 命令A(yù)="BBB ccc" 定義‘BBB ccc’命令的別名為命令A(yù)
#unalias 命令A(yù) 取消別名命令A(yù)
以上只是臨時横浑,關(guān)機失效
#alias 查看已有可用的所有別名
例如:

[root@centos7 ~]# alias
alias cp='cp -i'
................................
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@centos7 ~]#

注意:1剔桨、在某些情況別名不想取消,又想使用定義內(nèi)的原始命令徙融,在原始命令上加單引號
例如:定義 #alias ifconfig="ifconfig ens33"
想使用ifconfig原始命令洒缀,不取消定義別名,可以這樣加單引號執(zhí)行即可 #'ifconfig'
2、以上操作重啟或注銷都會丟失
不丟失树绩,需要寫入文件萨脑,文件在家目錄下
/home/用戶/.bashrc 只對系統(tǒng)當(dāng)前用戶生效
/etc/bashrc 對系統(tǒng)所有用戶生效

        這個時候說下,對于剛修改的配置文件饺饭,想讓它生效又不想重啟渤早,可以如下操作
         soure 文件名      使修改后的文件立即生效
      3、別名優(yōu)先級大于內(nèi)部命令瘫俊,內(nèi)部命令優(yōu)先級大于外部命令

三:命令的執(zhí)行過程:

系統(tǒng)先判斷是否是內(nèi)部或外部命令----是內(nèi)部命令就直接執(zhí)行鹊杖;是外部命令,在hash緩存表查找----緩存表找不到去PATH變量路徑查找---還找不到扛芽,命令不存在

命令hash緩存表的使用:
用法:
hash 顯示當(dāng)前系統(tǒng)命令緩存表
-l 顯示緩存表的詳細信息
-d 命令 清除指定命令緩存
-r 清除所有hash緩存
例如:

[root@centos7 ~]# hash
hits command
1 /usr/bin/pwd
2 /usr/sbin/service
2 /usr/bin/cat
1 /usr/bin/touch
1 /usr/bin/sleep
4 /usr/sbin/shutdown
12 /usr/bin/ls
[root@centos7 ~]# hash -l
builtin hash -p /usr/bin/pwd pwd
builtin hash -p /usr/sbin/service service
builtin hash -p /usr/bin/cat cat
builtin hash -p /usr/bin/touch touch
builtin hash -p /usr/bin/sleep sleep
builtin hash -p /usr/sbin/shutdown shutdown
builtin hash -p /usr/bin/ls ls
[root@centos7 ~]#

關(guān)于環(huán)境變量路徑:
echo $PATH 查看所有的環(huán)境變量

[root@centos7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]#

當(dāng)命令不是內(nèi)部命令骂蓖,查了hash表也沒有,就會在PATH路徑從左到右的順序查找

小技巧: which cmd 查看命令的環(huán)境變量路徑
-a cmd 查看命令所有存在的路徑

[root@centos7 ~]# which -a ls

alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]#

四:命令的幫助:

help man info
再多的命令川尖,人是記不完的登下,還是學(xué)會查看命令幫助,學(xué)會自學(xué)叮喳。
用法:
命令 --help 查看命令幫助
help 命令 查看內(nèi)部命令幫助
man 命令 查看外部命令幫助
man一個命令進入詳細頁被芳,空格向尾部翻屏,b向首部翻屏
d向尾部翻半屏嘲更,u向首部翻半屏
q退出信息頁筐钟。
/關(guān)鍵詞 以當(dāng)前位置,向尾部搜索想要查找的關(guān)鍵詞 n下一個赋朦,N上一個
篓冲?關(guān)鍵詞 以當(dāng)前位子,想首部搜索想要查找的關(guān)鍵詞 n下一個宠哄,N上一個
注意看到中間有日期號壹将,表示最后一頁到了

info 命令 查看詳細的命令幫助
whatis whatis命令顯示手冊部分的頁眉行。然后可以發(fā)出man命令以獲取附加的信息毛嫉。whatis命令等同于使用man -f命令
如果不能用诽俯,執(zhí)行命令: #makewhatis centos6的開啟
# mandb centos7的開啟
例如:

[root@centos7 ~]# 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, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
With no FILE, or when FILE is -, read standard input.
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
GNU coreutils online help: http://www.gnu.org/software/coreutils/
For complete documentation, run: info coreutils 'cat invocation'
[root@centos7 ~]#

[root@centos7 ~]# man cat | more
CAT(1) User Commands CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version
output version information and exit
With no FILE, or when FILE is -, read standard input.
EXAMPLES
cat f - g
Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
GNU coreutils online help: http://www.gnu.org/software/coreutils/ Report cat trans‐
lation bugs to http://translationproject.org/team/
AUTHOR
Written by Torbjorn Granlund and Richard M. Stallman.
COPYRIGHT
Copyright ? 2013 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 WAR‐
RANTY, to the extent permitted by law.
SEE ALSO
tac(1)
The full documentation for cat is maintained as a Texinfo manual. If the info and cat
programs are properly installed at your site, the command
info coreutils 'cat invocation'
should give you access to the complete manual.
GNU coreutils 8.22 November 2016 CAT(1)
[root@centos7 ~]#

[root@centos7 ~]# whatis cat
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
[root@centos7 ~]#

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市承粤,隨后出現(xiàn)的幾起案子暴区,更是在濱河造成了極大的恐慌,老刑警劉巖辛臊,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仙粱,死亡現(xiàn)場離奇詭異,居然都是意外死亡彻舰,警方通過查閱死者的電腦和手機伐割,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門候味,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人隔心,你說我怎么就攤上這事白群。” “怎么了硬霍?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵帜慢,是天一觀的道長。 經(jīng)常有香客問我须尚,道長崖堤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任耐床,我火速辦了婚禮,結(jié)果婚禮上楔脯,老公的妹妹穿的比我還像新娘撩轰。我一直安慰自己,他們只是感情好昧廷,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布堪嫂。 她就那樣靜靜地躺著,像睡著了一般木柬。 火紅的嫁衣襯著肌膚如雪皆串。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天眉枕,我揣著相機與錄音恶复,去河邊找鬼。 笑死速挑,一個胖子當(dāng)著我的面吹牛谤牡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播姥宝,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼翅萤,長吁一口氣:“原來是場噩夢啊……” “哼腊满!你這毒婦竟也來了套么?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碳蛋,失蹤者是張志新(化名)和其女友劉穎胚泌,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疮蹦,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡窖剑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年讯私,在試婚紗的時候發(fā)現(xiàn)自己被綠了拜隧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡壁公,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绅项,到底是詐尸還是另有隱情紊册,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布快耿,位于F島的核電站囊陡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏掀亥。R本人自食惡果不足惜撞反,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望搪花。 院中可真熱鬧遏片,春花似錦、人聲如沸撮竿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽幢踏。三九已至髓需,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間房蝉,已是汗流浹背僚匆。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留惨驶,地道東北人白热。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像粗卜,于是被迫代替她去往敵國和親屋确。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容