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阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容