Linux命令入門雜談

Linux學習任重道遠,對於初學者來講,看到多如繁星的命令,就讓人望而卻步,接下來就說說Linux命令的一些入門學習,但願我能夠給看到的人幫助。
我認爲學習Linux命令分爲下面三個方面,命令的格式、分類以及執行過程,最後重要的是,本身學會如何學習使用一個剛瞭解的命令。
一:命令的格式:
格式:命令 選項(長選項 短選項) 參數(例如 文件名 路徑 用戶等等)
例如:
(一、多個選項或多個參數用一個空格的空白字符隔開
 [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 ~]#html

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

(二、一條命令很長,寫不完,能夠加反斜槓( \) ,分紅多行來寫
[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 ~]#redis

注意:單命令直接後跟一個反斜槓就能夠,若是是帶有選項和參數的命令,保持每行最後先打一個空格,在跟一個反斜槓
(三、多條命令一塊兒執行,能夠用分號( ;)隔開shell

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

(四、取消或結束命令 ctrl+c(強制退出)或者ctrl+d(正常退出)
[root@centos7 ~]# sleep 1000
^C
[root@centos7 ~]#centos

二:命令的分類:

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

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

例如:查看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 ~]#ide

注意:有些命令是內部命令,外部命令也有
例如:
[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.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' 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 ~]#

注意:一、在某些狀況別名不想取消,又想使用定義內的原始命令,在原始命令上加單引號
例如:定義 #alias ifconfig="ifconfig ens33"
想使用ifconfig原始命令,不取消定義別名,能夠這樣加單引號執行便可 #'ifconfig'
二、以上操做重啓或註銷都會丟失
不丟失,須要寫入文件,文件在家目錄下
/home/用戶/.bashrc 只對系統當前用戶生效
/etc/bashrc 對系統全部用戶生效

這個時候說下,對於剛修改的配置文件,想讓它生效又不想重啓,能夠以下操做
soure 文件名 使修改後的文件當即生效
三、別名優先級大於內部命令,內部命令優先級大於外部命令

三:命令的執行過程:
系統先判斷是不是內部或外部命令----是內部命令就直接執行;是外部命令,在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 catcat (1) - concatenate files and print on the standard outputcat (1p) - concatenate and print files[root@centos7 ~]#

相關文章
相關標籤/搜索