Linux命令入門雜談

Linux學習任重道遠,對于初學者來說,看到多如繁星的命令,就讓人望而卻步,接下來就說說Linux命令的一些入門學習,希望我可以給看到的人幫助。
我認為學習Linux命令分為下面三個方面,命令的格式、分類以及執行過程,最后重要的是,自己學會如何學習使用一個剛了解的命令。

一:命令的格式:

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

[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 ~]#

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

[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、取消或結束命令 ctrl+c(強制退出)或者ctrl+d(正常退出)

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

二:命令的分類:

(1內部命令:被構建在shell之中,常駐內存,在任何時候都可以使用,執行速度非常快
(2外部命令:都是以文件的形式存在,是一個個單獨的可執行文件,存放于磁盤

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

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

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

注意:有些命令是內部命令,外部命令也有
例如:

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

也就解釋了系統某些內部命令丟失,也還是可以執行的原因

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

舉例:
查看啟用的內部命令:

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

禁用內部命令:

[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 ~]#

關于別名命令別名使用:
用法: #alias 命令A="BBB ccc" 定義‘BBB ccc’命令的別名為命令A
#unalias 命令A 取消別名命令A
以上只是臨時,關機失效
#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、在某些情況別名不想取消,又想使用定義內的原始命令,在原始命令上加單引號
例如:定義 #alias ifconfig="ifconfig ens33"
想使用ifconfig原始命令,不取消定義別名,可以這樣加單引號執行即可 #'ifconfig'
2、以上操作重啟或注銷都會丟失
不丟失,需要寫入文件,文件在家目錄下
/home/用戶/.bashrc 只對系統當前用戶生效
/etc/bashrc 對系統所有用戶生效

        這個時候說下,對于剛修改的配置文件,想讓它生效又不想重啟,可以如下操作
         soure 文件名      使修改后的文件立即生效
      3、別名優先級大于內部命令,內部命令優先級大于外部命令

三:命令的執行過程:

系統先判斷是否是內部或外部命令----是內部命令就直接執行;是外部命令,在hash緩存表查找----緩存表找不到去PATH變量路徑查找---還找不到,命令不存在

命令hash緩存表的使用:
用法:
hash 顯示當前系統命令緩存表
-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 ~]#

關于環境變量路徑:
echo $PATH 查看所有的環境變量

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

當命令不是內部命令,查了hash表也沒有,就會在PATH路徑從左到右的順序查找

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

[root@centos7 ~]# which -a ls

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

四:命令的幫助:

help man info
再多的命令,人是記不完的,還是學會查看命令幫助,學會自學。
用法:
命令 --help 查看命令幫助
help 命令 查看內部命令幫助
man 命令 查看外部命令幫助
man一個命令進入詳細頁,空格向尾部翻屏,b向首部翻屏
d向尾部翻半屏,u向首部翻半屏
q退出信息頁。
/關鍵詞 以當前位置,向尾部搜索想要查找的關鍵詞 n下一個,N上一個
?關鍵詞 以當前位子,想首部搜索想要查找的關鍵詞 n下一個,N上一個
注意看到中間有日期號,表示最后一頁到了

info 命令 查看詳細的命令幫助
whatis whatis命令顯示手冊部分的頁眉行。然后可以發出man命令以獲取附加的信息。whatis命令等同于使用man -f命令
如果不能用,執行命令: #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 ~]#

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,283評論 6 530
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 97,947評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,094評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,485評論 1 308
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,268評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,817評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,906評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,039評論 0 285
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,551評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,502評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,662評論 1 366
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,188評論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,907評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,304評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,563評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,255評論 3 389
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,637評論 2 370

推薦閱讀更多精彩內容