首页 > 系统服务 > 详细

Linux命令入门杂谈

时间:2017-11-11 20:27:59      阅读:340      评论:0      收藏:0      [点我收藏+]

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

注意: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 ~]#

Linux命令入门杂谈

原文:http://www.cnblogs.com/linuxshuai/p/7819770.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!