您的位置:首页 > 运维架构 > Linux

Linux 中常用的基础命令

2016-07-27 17:13 330 查看
Linux 中常用的基础命令==============================================================================
概述:

==============================================================================

Linux 基础命令

1.目录相关命令的使用

★pwd:printing working directory
显示工作目录
对于pwd命令,其还有一些相关的环境变量:PWD:保存了当前目录路径
OLDPWD:保存了上一次所在目录的路径
每次执行pwd命令的时候其实就是在读取PWD的值★cd:change directory 改变工作目录语法格式:cd [/PATH/TO/SOMEDIR]cd或cd~:切换回当前用户的家目录;注意:bash中~表示家目录;
cd ~USERNAME:切换至指定用户的家目录;(只被管理员允许)
cd -:在上一次所在目录与当前目录之间来回切换; 相关的环境变量: $PWD:当前的工作目录 $OLDPWD:上一次的工作目录
cd ..:返回上一级目录
演示:
1.pwd和[b]cd命令
[/b]
[root@CentOS6 ~]# pwd
/root
[root@CentOS6 ~]# cd ~

# 切换至指定用户的家目录
[root@CentOS6 ~]# cd ~centos
[root@CentOS6 centos]# pwd
/home/centos

[root@CentOS6 ~]# cd /etc/sysconfig
[root@CentOS6 sysconfig]# pwd
/etc/sysconfig
[root@CentOS6 sysconfig]# cd ..
★ls:list 显示指定路径下的文件列表语法格式:
ls [OPTION]... [FILE]...
选项:-a:--all 显示所有文件,包括隐藏文件;
-A:--almost-all:显示除 . 和 .. 之外的所有文件;
-l:--long 长格式列表,即显示文件的详细属性信息


-h,--human-readable:对文件单位大小换算:换算后的结果可能会非精确值;(通常以 ll -h 方式使用)
-d:查看目录自身而非其内部文件列表(通常以 ll -d 方式使用)
-r:reverse 逆序显示;默认是以升序显示
-R:recursive,递归显示;如果一个目录里有子目录,则同时也显示其子目录中的文件;
-Sl : 文件由大到小排序
-Slr:文件由小到大排序 配合 l
-t :按修改时间排序,-Slt 等配合l,S,r等排序
演示:
[root@CentOS6 ~]# ls -a
.                .bash_logout   .cshrc   Documents  .gnome2          .gvfs               .lesshst        .nautilus      splash.jpg  .viminfo
..               .bash_profile  .dbus    Downloads  .gnome2_private  .ICEauthority       .local          Pictures       .ssh        .vimrc
anaconda-ks.cfg  .bashrc        Desktop  .esd_auth  .gnote           .imsettings.log     .mozilla        Public         .tcshrc     .Xauthority
.ansible         .cache         discuz   .gconf     .gnupg           install.log         Music           .pulse         Templates   .xsession-errors
.bash_history    .config        .dmrc    .gconfd    .gtk-bookmarks   install.log.syslog  .mysql_history  .pulse-cookie  Videos

[root@CentOS6 ~]# ls -A
anaconda-ks.cfg  .bashrc  Desktop    .esd_auth        .gnote          .imsettings.log     .mozilla        Public         .tcshrc    .Xauthority
.ansible         .cache   discuz     .gconf           .gnupg          install.log         Music           .pulse         Templates  .xsession-errors
.bash_history    .config  .dmrc      .gconfd          .gtk-bookmarks  install.log.syslog  .mysql_history  .pulse-cookie  Videos
.bash_logout     .cshrc   Documents  .gnome2          .gvfs           .lesshst            .nautilus       splash.jpg     .viminfo
.bash_profile    .dbus    Downloads  .gnome2_private  .ICEauthority   .local              Pictures        .ssh           .vimrc

# 显示详细信息
[root@CentOS6 ~]# ls -l
total 252
-rw-------. 1 root root   2429 Nov  6 12:11 anaconda-ks.cfg
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Desktop
drwxr-xr-x  4 root root   4096 Nov 23 16:44 discuz
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Documents
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Downloads
-rw-r--r--. 1 root root  45324 Nov  6 12:11 install.log
-rw-r--r--. 1 root root   9963 Nov  6 12:07 install.log.syslog
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Music
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Pictures
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Public
-r--r--r--  1 root root 151230 Nov 23 12:26 splash.jpg
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Templates
drwxr-xr-x  2 root root   4096 Nov 23 08:50 Videos

2.只显示某目录下的隐藏文件
[root@centos7 ~]# ls -d /.*
/.  /..  /.autorelabel

[root@centos7 ~]# ls -d .*
.   .ansible       .bash_logout   .bashrc  .config  .esd_auth        .gtkrc-2.0-kde4  .local    .mysql_history  .tcshrc   .vimrc
..  .bash_history  .bash_profile  .cache   .cshrc   .gstreamer-0.10  .kde             .mozilla  .ssh            .viminfo  .Xauthority

[root@centos7 ~]# ls -d /etc/.*
/etc/.  /etc/..  /etc/.pwd.lock  /etc/.updated
3.只显示某目录下的目录




3.文件查看类命令
★cat:文件查看类工具 格式:cat [OPTION]... [FILE]...选项:-A:显示所有非正常的所有符号;
-E:显示行结束符$;
-T:显示制表符;
-v:显示其他的非打印字符;
-n:对显示的行进行编号,与文件内容没有联系;
-b:打印出行号(空白行不标行号)
★tac,rev命令(同cat命令,显示顺序不同)tac 命令同cat命令,逆序显示(列)

rev 命令逆序显示行字符
直接输入cat命令表示等待用户的输入并打印显示,结束Ctrl+c★文件内容类型查看命令:file格式:file [file]...
通过file命令可以获知改文件是什么类型的,如普通文件、目录、链接文件等
演示:
1.cat命令
[root@centos7 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Nov  6 10:30:14 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe /                       xfs     defaults        0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot                   xfs     defaults        0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr                    xfs     defaults        0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap                    swap    defaults        0 0
[root@centos7 ~]# cat /etc/issue
\S
Kernel \r on an \m

Mage Education Learning Services http://www.magedu.com 
# 连接显示多个文件
[root@centos7 ~]# cat /etc/fstab /etc/issue

#
# /etc/fstab
# Created by anaconda on Sun Nov  6 10:30:14 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe /                       xfs     defaults        0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot                   xfs     defaults        0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr                    xfs     defaults        0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap                    swap    defaults        0 0
\S
Kernel \r on an \m

Mage Education Learning Services http://www.magedu.com 
# 对显示的行进行编号
[root@centos7 ~]# cat -n /etc/fstab
1
2	#
3	# /etc/fstab
4	# Created by anaconda on Sun Nov  6 10:30:14 2016
5	#
6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8	#
9	UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe /                       xfs     defaults        0 0
10	UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot                   xfs     defaults        0 0
11	UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr                    xfs     defaults        0 0
12	UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap                    swap    defaults        0 0

#打印出行号,空白字符不标行号
[root@centos7 ~]# cat -b /etc/fstab

1	#
2	# /etc/fstab
3	# Created by anaconda on Sun Nov  6 10:30:14 2016
4	#
5	# Accessible filesystems, by reference, are maintained under '/dev/disk'
6	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7	#
8	UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe /                       xfs     defaults        0 0
9	UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot                   xfs     defaults        0 0
10	UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr                    xfs     defaults        0 0
11	UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap                    swap    defaults        0 0
# 显示行结束符
[root@centos7 ~]# cat -E /etc/fstab
$
#$
# /etc/fstab$
# Created by anaconda on Sun Nov  6 10:30:14 2016$
#$
# Accessible filesystems, by reference, are maintained under '/dev/disk'$
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$
#$
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe /                       xfs     defaults        0 0$
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot                   xfs     defaults        0 0$
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr                    xfs     defaults        0 0$
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap                    swap    defaults        0 0$
2.cat,tac,和rev命令的区别
[root@centos7 ~]# echo "123456789" > /testdir/f1
[root@centos7 ~]# ll /testdir/f1
-rw-r--r-- 1 root root 10 2月  11 16:29 /testdir/f1

[root@centos7 ~]# cat /testdir/f1
123456789
[root@centos7 ~]# tac /testdir/f1
123456789
[root@centos7 ~]# rev /testdir/f1
987654321

[root@centos7 ~]# ls > /testdir/f2
[root@centos7 ~]# cat /testdir/f2
anaconda-ks.cfg
Desktop
Discuz
Documents
Downloads
lnamp
MHA
Music
nginx
Pictures
Public
Templates
Videos
zabbix-agent
zabbix-proxy

[root@centos7 ~]# tac /testdir/f2
zabbix-proxy
zabbix-agent
Videos
Templates
Public
Pictures
nginx
Music
MHA
lnamp
Downloads
Documents
Discuz
Desktop
anaconda-ks.cfg

[root@centos7 ~]# rev /testdir/f2
gfc.sk-adnocana
potkseD
zucsiD
stnemucoD
sdaolnwoD
pmanl
AHM
cisuM
xnign
serutciP
cilbuP
setalpmeT
soediV
tnega-xibbaz
yxorp-xibbaz
3.file命令
[root@centos7 ~]# file /etc/fstab
/etc/fstab: ASCII text

[root@centos7 ~]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped

4.echo回显命令★echo是个内建命令,因此可以使用帮助来获得echo的帮助。功能:显示字符语法:echo [-neE] [字符串]说明:echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开, 并在最后加上换行号# echo -n :关闭自动换行功能
echo 需要使用-e 参数来使转义字符生效(字符串一定要加引号)


字符串可以使用引号,单引号和双引号均可使用单引号:强引用,变量引用不执行替换(最傻) 如:echo "$SHELL" ---> 同:echo "${SHELL}" 变量引用的正规符号:${name}
双引号:弱引用,变量引用会被替换(居中) 如:echo '$SHELL'
另外:反向单引号{也可使用$()}:为命令引用(最聪明)。
用途:当一个命令调用第二个命令的结果的时候才会用到。
演示:
[root@centos7 ~]# echo "hello word"
hello word
[root@centos7 ~]# echo -n "hello word"
hello word[root@centos7 ~]#
[root@centos7 ~]# echo  "hello \nword"
hello \nword
[root@centos7 ~]# echo -e  "hello \nword"
hello
word
[root@centos7 ~]# echo -e  "hello \tword"
hello 	word
变量中单引号,双引号和反向单引号的使用
# 双引号变量被替换,弱引用
[root@centos7 ~]# echo "echo $USER"
echo root

# 单引号变量不被替换,强引用
[root@centos7 ~]# echo 'echo $USER'
echo $USER

# 反向单引号,一个命令调用第二个命令的结果
[root@centos7 ~]# echo `echo $USER`
root
[root@centos7 ~]# echo $(echo $USER)
root
4.关机或重启命令
★管理类命令关机:halt,poweroff,shutdown,init0
重启:reboot,shutdown,init6
跟用户登录相关:who,whoami,w
★halt和poweroff两者都是关机命令-f 强制关机,相当于切断电源
★reboot:重启-f 强制
-p 切断电源

★shutdown:关机或重启 格式:shutdown [OPTION...] [TIME] [WALL...]-r :reboot 重启;
-h :halt 关机;
-c :cancle 取消关机
TIME:now:立刻;
+m:相对时间表示法,从命令提交开始多久后,例如:+3 分钟;
hh:mm:绝对时间表示,到具体时间后关机
WALL:
wall 命令会在所有终端显示其输入的内容,例如:wall "taotao ai xiuxiu"

演示:
# 在5分钟后关机,会通知给所有人
[root@centos7 ~]# shutdown -r +5
Shutdown scheduled for 三 2017-02-08 09:35:05 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:30:05 CST):

The system is going down for reboot at Wed 2017-02-08 09:35:05 CST!

#取消关机,也会通知给所有人
[root@centos7 ~]# shutdown -c
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:30:45 CST):

The system shutdown has been cancelled at Wed 2017-02-08 09:31:45 CST!

# 加wall可显示自定义的语句
[root@centos7 ~]# shutdown -r +5  "hello everyone"
Shutdown scheduled for 三 2017-02-08 09:41:27 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7 (Wed 2017-02-08 09:36:27 CST):

hello everyone
The system is going down for reboot at Wed 2017-02-08 09:41:27 CST!
5.与登录相关的命令★whoami显示当前登录有效用户
★who作用:系统当前所有的登录会话格式:who [OPTION]...-b:系统此次启动的时间;
-r:运行级别

★w系统当前所有的登录会话及所做的操作;

演示:
[root@centos7 ~]# whoami
root

[root@centos7 ~]# who
root     tty1         2017-02-08 09:26
root     pts/0        2017-02-08 08:57 (192.168.1.104)
root     pts/1        2017-02-08 09:30 (192.168.1.104)
root     pts/2        2017-02-08 10:15 (192.168.1.104)
root     pts/3        2017-02-08 10:18 (192.168.43.174)
root     pts/4        2018-07-07 10:10 (192.168.1.104)
[root@centos7 ~]# who -b
系统引导 2017-02-08 15:48
[root@centos7 ~]# who -r
运行级别 3 2017-02-08 08:29

[root@centos7 ~]# w
11:02:39 up  2:32,  6 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      09:26    1:02m  0.08s  0.08s -bash
root     pts/0    192.168.1.104    08:57    1:25m  0.17s  0.17s -bash
root     pts/1    192.168.1.104    09:30    1:32m  0.04s  0.04s -bash
root     pts/2    192.168.1.104    10:15   47:11   0.05s  0.05s -bash
root     pts/3    192.168.43.174   10:18     ?     0.26s  0.26s -bash
root     pts/4    192.168.1.104    10:10     ?     0.10s  0.01s w
6.与日期相关的命令
★date命令:显示时间和日期格式:date[OPTION]...[+FORMAT] date命令可以按照指定格式显示日期,只键入date,则以默认格式显示当前时间,如下:

如果需要以指定格式显示日期,可以使用“+”开头的字符串指定其格式,详细格式如下(常用):%D:直接显示日期 mm/dd/yy (“Y”长格式年,“y”短格式年)
%F:直接显示日期 (YY-mm-dd) ;
%T:直接显示时间(24小时制);
%Y:完整年份(0000-9999);
%m:显示月份;
%d:日;
%A:星期几(Sunday-Saturday);
%a:星期几(Sun-Sat);
%w:一周中的第几天
%H:显示小时;
%M:显示分钟 ;
%S:显示秒;
%s:从1970年到现在经过的秒数;
★设置时间格式:date [MMDDhhmm[[CC]YY][.ss]]
MM:月份;
DD:几号;
hh:小时;
mm:分钟;
CC:两位年份;
CCYY:四位年份;
ss:秒;
用 -s 可以设置系统时间★Linux的两个时钟系统时钟由Linux内核通过cpu的工作频率进行计时
硬件时钟:就是主板bios提供的时钟,由主板电池供电来维持运行
命令date命令显示的是系统时间;
clock或者hwclock显示的是硬件时钟 -s, --hctosys以硬件时钟为准,校正系统时钟 -w, --systohc以系统时钟为准,校正硬件时钟
演示:
1.常用格式的日期显示
[root@centos7 ~]# date +%D
02/08/17
[root@centos7 ~]# date +%F
2017-02-08
[root@centos7 ~]# date +%T
10:20:00
[root@centos7 ~]# date +%Y
2017
[root@centos7 ~]# date +%m
02
[root@centos7 ~]# date +%d
08
[root@centos7 ~]# date +%A
星期三
[root@centos7 ~]# date +%a
三
[root@centos7 ~]# date +%w
3
[root@centos7 ~]# date +%H
10
[root@centos7 ~]# date +%M
21
[root@centos7 ~]# date +%S
24
[root@centos7 ~]# date +%s
1486520492
2.完整格式的日期显示:
[root@centos7 ~]# date "+现在的时间是:%Y-%m-%d %H:%M:%S"
现在的时间是:2017-02-08 10:23:29

[root@centos7 ~]# date "+现在的时间是:%F %H:%M:%S"
现在的时间是:2017-02-08 10:24:33

[root@centos7 ~]# date "+现在的时间是:%F %H:%M:%S %A"
现在的时间是:2017-02-08 10:24:51 星期三
3.如果要显示的时间不是当前时间,而是经过运算的时间,则可以使用-d选项,如下:
# 显示3年前的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "-3 year"
2014-02-08 10:26:08

# 显示3个月后的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "+3 month"
2017-05-08 10:26:24

# 显示10天后的时间
[root@centos7 ~]# date "+%F %H:%M:%S" -d "+10 day"
2017-02-18 10:26:42
4.有时候需要获取当前时间距离1970年0时0分0秒经历的秒数,保存在变量中,如下:
[root@centos7 ~]# time=`date "+%s"`
[root@centos7 ~]# echo $time
1486520847
5.设置时间
[root@centos7 ~]# date -s "20180707 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST

[root@centos7 ~]# date -s "2018-07-07 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST

[root@centos7 ~]# date -s "2018/07/07 10:10:10"
2018年 07月 07日 星期六 10:10:10 CST
6.系统时钟和硬件时钟
# 系统时钟
[root@centos7 ~]# date
2018年 07月 07日 星期六 10:26:42 CST

#硬件时钟
[root@centos7 ~]# clock
2017年02月08日 星期三 10时47分46秒  -0.292560 秒

[root@centos7 ~]# hwclock
2017年02月08日 星期三 10时47分53秒  -0.479196 秒

# 硬件时钟校正系统时钟
[root@centos7 ~]# clock -s
[root@centos7 ~]# date
2017年 02月 08日 星期三 10:48:24 CST
★cal命令显示当前日历,默认显示当前月份的日历;
cal [YY]:显示年份的日历 如: # cal 09 2017 (注意书写顺序)

演示:
[root@centos7 ~]# cal
二月 2017
日 一 二 三 四 五 六
1  2  3  4
5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28

[root@centos7 ~]# cal 2017
2017

一月                   二月                   三月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
29 30 31               26 27 28               26 27 28 29 30 31

四月                   五月                   六月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
1       1  2  3  4  5  6                1  2  3
2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
30
七月                   八月                   九月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
1          1  2  3  4  5                   1  2
2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
30 31
十月                  十一月                 十二月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
1  2  3  4  5  6  7             1  2  3  4                   1  2
8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
31

[root@centos7 ~]# cal 09 2017
九月 2017
日 一 二 三 四 五 六
1  2
3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

7.type、alias、which和whereis命令★type 格式:type COMMAND内部命令:builtin
外部命令:显示为命令文件路径注意:命令可以有别名,别名可以与原名相同,此时原名被隐藏,此时如果要运行原命令,则使用 \COMMAND
★alias 命令别名获取所有可用别名的定义:~]# alias
定义别名:~]# alias NAME='COMMAND' (仅对当前shell有效)
撤销别名:~]# unalias NAME
★which 显示应用程序文件位置格式:which COMMAND注意:有时候一个命令还有其他的别名,但我们先看命令本身,而非别名,此时可以使用:which --skip-alias 禁止使用别名,直接使用原始命令
★whereis命令:作用:whereis - locate the binary, source, and manual page files for a command格式:whereis [options] name...选项:-b:仅搜索二进制程序路径;
-m:仅搜索使用手册文件路径;

演示:
1.type
[root@centos7 ~]# type cd
cd 是 shell 内嵌

[root@centos7 ~]# type date
date 已被哈希 (/usr/bin/date)

[root@centos7 ~]# type tree
tree 是 /usr/bin/tree

[root@centos7 ~]# type ls
ls 是 `ls --color=auto' 的别名



2.which

[root@centos7 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]# which --skip-alias ls
/usr/bin/ls
3.whereis
[root@centos7 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
4.别名alias
[root@centos7 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

7.screen命令远程控制★使用方法:打开screen: # screen -S [session 会话]
加入screen: # screen -x [session 会话]
退出并关闭screen: exit
剥离当前screen: Ctrl+a+d
显示所有已经打开的screen: # screen -ls
恢复某screen: screen -r [screen]

bash的基础特性(一)

1.命令历史
★命令历史:shell进程会在其会话中保存此前用户提交执行过的命令
★history:
[b][b]☉[/b][/b]作用:history 是管理命令历史,每次执行的命令都会放置在内存的缓存当中,直到退出当前终端后会被保存在命令历史文件当中。
[b][b]☉[/b][/b]命令历史文件位置:
登录shall后,新执行的命令只会记录在缓存当中;
登录shall后,会读取命令历史文件中的记录命令

.bash_history 放置在家目录下的隐藏文件中
[b][b][b]☉[/b][/b][/b]history命令用法格式:history [-c] [-d 偏移量]

或 history -anrw [文件名]
或 history -ps 参数 [参数...]
选项:-c:清空命令历史
-d:删除历史中指定的命令; eg:history -d 3
#:显示最近的#条历史 eg:history 5
-a:追加本次会话新执行的命令历史列表至历史文件
-n:读历史文件中未读过的(其他终端用户)行到历史列表
-r:读历史文件附加到历史列表
-w:保存历史列表到指定的历史文件
-p:展开历史参数成多个行,但不存在历史列表中
-s:展开历史参数成一行,附加在历史列表后 (伪造命令)
★history 中的相关变量HISTSIZE:shell进程可保留的命令历史的条数;
HISTFILE:指定持久保存命令历史的文件,默认为~/.bash_history;
HISTFILESIZE:指定历史文件记录历史的大小(条数);
HISTTIMEFORMAT=“%F %T“ 显示时间
HISTIGNORE=“str1:str2:… “ 忽略string1,string2历史
控制命令历史的记录方式: 环境变量:HISTCONTROLignoredups:忽略重复的命令;连续且相同方为“重复”
ignorespace:忽略所有以空白开头的命令
ignoreboth:ignoredups和ignorespace的组合
erasedups: 删除重复命令
注意:修改变量的值:NAME='VALUE'

我们修改的命令只是记录在缓存中,要想永久生效就必须 在 /etc/profile 或~/.bash_profile 文件中修改保存。
[b][b]☉[/b][/b]重复前一个命令的方法:使用上方向键,并回车执行。
按 !! 并回车执行
输入!-1 并回车执行。
按Ctrl+p 并回车执行。
[b][b]☉[/b][/b]重复之前命令的方法:!字符串:重复前一个以“字符串”开头的命令
!num: 按照history命令输出中的序号重复对应命令
!?字符串:执行包含字符串的命令
!-n:执行倒数第n个命令
[b][b]☉[/b][/b]命令行历史的查看和搜索使用up(向上)和down(向下)键来上下浏览从前输入的命令;
键入ctrl-r来在命令历史中搜索命令 (reverse-i-search)`':
[b][b]☉[/b][/b]要重新调用前一个命令中最后一个参数:!$ 表示
Esc .(点击Esc键后松开,然后点击. 键)
Alt+ .(按住Alt键的同时点击. 键)
[b][b][b]☉[/b][/b][/b]命令调用的方法:
#cmd !^ : 利用上一个命令的第一个参数做cmd的参数
#cmd !$ : 利用上一个命令的最后一个参数做cmd的参数
#cmd !* : 利用上一个命令的全部参数做cmd的参数
#cmd !:n : 利用上一个命令的第n个参数做cmd的参数
组合:!n:^ :调用第n条命令的第一个参数
!n:$ :调用第n条命令的最后一个参数
!m:n :调用第m条命令的第n个参数
!n:* :调用第n条命令的所有参数
[b][b][b]☉[/b][/b][/b]搜索历史中的参数:!string:^ : 从命令历史中搜索以string 开头的命令,并 获取它的第一个参数;
!string:$ :从命令历史中搜索以string 开头的命令,并获取它的最后一个参数;
!string:n :从命令历史中搜索以string 开头的命令,并获取它的第n个参数;
!string:* :从命令历史中搜索以string 开头的命令,并获取它的所有参数;

[b] 2.命令及路径补全[/b]
★命令补全方法 shell程序在接收到用户执行命令的请求,分析完成之后,最左侧的字符串会被当做命令;
命令查找机制内部命令:内部命令没有具体的二进制文件,是由内核提供的;
外部命令:bash根据PATH环境变量定义的路径,自左而右在每个路径搜寻以给定命令名命名的文件,第一次找到的命令即为要执行的命令。
补全方法:直接补全:用户给定的大头字符串如果能唯一标识某命令程序文件,则直接补全;
用户给定的字符串为开头的命令不唯一时,则再次按Tab键会给出以字符串开头的命令列表。
★路径补全把用户给出的字符串当做路径开头,并在其指定上级目录下搜索以指定的字符串开头的文件名,如果惟一,则直接补全;否则:再次Tab给出列表

[b] 3.bash常用的快捷方式[/b]Ctrl+l :清屏,相当于clear命令
Ctrl+c :取消命令的执行
Ctrl+a :会移动到命令行的最前面
Ctrl+e :会移动到命令行的最后面
Ctrl+u :会删除到行首
Ctrl+k :会删除到行尾
Ctrl+b :向左 Ctrl-f 向右移动一个字符
Esc+b :左移一个单词
Esc+f :右移一个单词
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux echo history date c