一、查看命令历史及相关信息
bash查找命令的方式:
外部命令查找:$PATH
[[email protected]_basic ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin查找路径是从左往右查找的,
如果说刚要执行的命令是在/root/bin则要一各路径一个路径去查找,不是会耗费很多时间吗?还有如果命令被反复使用了,
如果每一个使用都要去查找,速度不是会很慢啊。那如何来提升这个速度呢?
可以把每一次的查找结果存储下来,以后再执行此命令时,不用去找那个命令了,直接运行即可了。
第一次执行真正的查找操作;查找结果会缓存;执行同一个命令,则有缓存中直接去获取曾经查找到的路径
查看此缓存信息使用hash命令
hash显示所有执行的外部命令(是绝对路径),以及被命中的次数
查看命令是内置的还是外部命令
[[email protected]_basic ~]# type hash
hash is a shell builtin
为内置命令,则可以通过help hash来查看帮助信息
[[email protected]_basic ~]# help hash
hash: hash [-lr] [-p pathname] [-dt] [name ...]
Remember or display program locations.
记住或显示程序存储位置
Determine and remember the full pathname of each command NAME. If
no arguments are given, information about remembered commands is displayed.
Options:
-d forget the remembered location of each NAME 忘记所指定记住的路径
-l display in a format that may be reused as input
-p pathname use PATHNAME is the full pathname of NAME
-r forget all remembered locations 忘记所有记住的路径
-t print the remembered location of each NAME, preceding
each location with the corresponding NAME if multiple
NAMEs are given
Arguments:
NAME Each NAME is searched for in $PATH and added to the list
of remembered commands.
Exit Status:
Returns success unless NAME is not found or an invalid option is given.
-d: 清除指定名称的缓存内容
-r: 清空全部缓存
[[email protected]_basic ~]# hash
hits command
1 /sbin/ifconfig
1 /usr/bin/man
3 /bin/ls
1 /sbin/ip
[[email protected]_basic ~]# hash -d ifconfig
[[email protected]_basic ~]# hash
hits command
1 /usr/bin/man
3 /bin/ls
1 /sbin/ip
[[email protected]_basic ~]# hash
hits command
1 /usr/bin/man
3 /bin/ls
1 /sbin/ip
[[email protected]_basic ~]# hash -r
[[email protected]_basic ~]# hash
hash: hash table empty 缓存信息已经为空了,因为刚清空了所有的缓存信息
bash的重要特性之一:命令补全
根据内建命令或外部命令的查找方式查找以用户指定的字符串开头的命令
如果用户指定的开头字符串能惟一标识某命令,则tab键可补全此命令
否则,则两次tab键可显示所有以指定字串开头的命令
Linux文件系统的基础特性:
1、文件名严格区分大小写
2、所有文件都位于根目录下,Linux目录一个一颗倒置树 FHS
3、路径以/做为分隔符
4、每个文件的名称可使用除/以外任意字符(一般使用比较规范的格式(用字符或数字或下划线等组成)),单个名称最长不能超出255个字符;
bash重要特性之一:路径补全(可避免自己书写出错,也可提高操作效率)
以用户指定的起始路径进行文件名补全查找
每个绝对路径都可以切割为两部分,文件名和父目录
基名
basename /path/to/somewhere 可以获取其基名
结果为somewhere
目录名
dirname /path/to/somewhere 可以获取其目录名
结果为/path/to
[[email protected]_basic network-scripts]# basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0
[[email protected]_basic network-scripts]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
bash的命令历史:几个依赖以环境变量的有
HISTSIZE
HISTFILE
HISTFILESIZE
[[email protected]_basic network-scripts]# echo $HISTSIZE
1000
[[email protected]_basic network-scripts]# echo $HISTFILE
/root/.bash_history
[[email protected]_basic network-scripts]# echo $HISTFILESIZE
1000
HISTCONTROL:
ignoredups: 忽略重复的命令,输入的是连续的相同命令才会为重复,只显示一次
ignorespace: 忽略以空白字符开头的命令(可以用此值给HISTCONTROL赋值,不让他人看到命令历史中我们输入的命令,可以在输入命令是先输入空白符再输命令)
ignoreboth: 以上二者同时生效;
[[email protected]_basic ~]# echo $HISTCONTROL
ignoredups
[[email protected]_basic ~]# HISTCONTROL=ignorespace
之后你输入一空格开头的命令后,在命令历史列表中就不会显示那条命令了
练习:获取下面命令的使用方法
shutdown, date, hwclock, ntpdate, reboot, halt, who, whoami, which, hash
二、系统关机及日期设置
reboot: 重启
halt: 关机
[[email protected]_basic ~]# halt
Broadcast message from [email protected]_basic
(/dev/pts/1) at 12:19 ...
The system is going down for halt NOW! 执行后,在每个终端都会有此信息接收
poweroff: 关机
如何关机,如何重启,如在指定时间上重启
shutdown -h
-r 执行之后系统重启
-r Requests that the system be rebooted after it has been brought down.
-c 取消系统关闭
-c Cancels a running shutdown. TIME is not specified with this option, the first argument is MESSAGE.
-h
-h Requests that the system be either halted or powered off after it has been brought down, with the choice as to which left up to the
system.
时间格式
TIME may have different formats, the most common is simply the word ’now’ which will bring the system down immediately. Other valid for-
mats are +m, where m is the number of minutes to wait until shutting down and hh:mm which specifies the time on the 24hr clock.
now
+m
hh:mm
shutdown - bring the system down
SYNOPSIS
shutdown [OPTION]... TIME [MESSAGE]
shutdown -h now 现在马上关机
[[email protected]_basic ~]# shutdown -h +5 过5分钟后关机
Broadcast message from [email protected]_basic
(/dev/pts/0) at 13:14 ...
The system is going down for halt in 5 minutes!
[[email protected]_basic ~]# shutdown -c 取消系统关机
date: 显示和设置系统日期和时间
date - print or set the system date and time
date [options] [+FORMAT]
%s: %s seconds since 1970-01-01 00:00:00 UTC 时间戳计时法,从Unix元年(1970-01-01 00:00:00)到此刻所经过的秒数
%F, %D
%F full date; same as %Y-%m-%d
%D date; same as %m/%d/%y
%T time; same as %H:%M:%S
%Y year
%m month (01..12)
%d day of month (e.g, 01)
%H hour (00..23)
%M minute (00..59)
%S second (00..60)
%u day of week (1..7); 1 is Monday
设定系统时间
date [MMDDhhmm[[CC]YY][.ss]]
[[email protected]_basic ~]# date
Sat Dec 20 13:17:18 CST 2014
[[email protected]_basic ~]# date +%F
2014-12-20
[[email protected]_basic ~]# date +%D
12/20/14
[[email protected]_basic ~]# date +%T
13:18:03
[[email protected]_basic ~]# date +%Y-%m-%d
2014-12-20
[[email protected]_basic ~]# date +%H:%M:%S
13:21:00
Linux有两个时钟:系统时钟和硬件时钟
硬件时钟:保证系统能精确计时的,系统开机后,会从硬件时钟上读取时间到系统时间
系统时钟:Linux内核也有工作频率的,可以自己设置
bash上同时执行多条命令,命令间使用‘;‘分号分隔开
hwclock 查询和设置硬件时钟
hwclock - query and set the hardware clock (RTC)
-s: 以硬件为准
-s, --hctosys
Set the System Time from the Hardware Clock.
Also set the kernel’s timezone value to the local timezone as indicated by the TZ environment variable and/or /usr/share/zoneinfo,
as tzset(3) would interpret them. The obsolete tz_dsttime field of the kernel’s timezone value is set to DST_NONE. (For details on
what this field used to mean, see settimeofday(2).)
This is a good option to use in one of the system startup scripts.
-w:以系统为准
-w, --systohc
Set the Hardware Clock to the current System Time.
[[email protected]_basic network-scripts]# hwclock ; date
Sat 20 Dec 2014 08:52:26 AM CST -0.982617 seconds
Sat Dec 20 08:52:25 CST 2014
之间是有差别的
ntp: Network Time Protocol 网络时间协议
centos 7之后不在使用ntp协议了
通过网络同步系统时间
是C/S架构的: Server, Client
同步到对应时间服务器
ntpdate server 同步到server上的时间
ntpdate ipaddr_server
[[email protected]_basic ~]# mtpdate --help
-bash: mtpdate: command not found
[[email protected]_basic ~]# ntpdate --help
ntpdate: unknown option --
ntpdate: unknown option -h
ntpdate: encryption delay lp is unlikely
usage: ntpdate [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...
三、查看用户登录和Linux下目录结构
who: 登录至当前系统的所有用户
who - show who is logged on
[[email protected]_basic ~]# who
root tty1 2014-12-10 19:34 没有显示ip为本地登录
root pts/0 2014-12-20 07:39 (192.168.20.93) 在192.168.20.93主机上登录
root pts/1 2014-12-20 08:38 (192.168.20.93)
whoami: 当前终端上登录的用户
whoami - print effective userid
[[email protected]_basic ~]# whoami
root
which: 显示指定命令的完整路径
which - shows the full path of (shell) commands.
[[email protected]_basic ~]# which ls
alias ls=‘ls --color=auto‘
/bin/ls
--skip-alias: 跳过命令别名
--skip-alias
Ignore option `--read-alias′, if any. This is useful to explicity search for normal binaries, while using the `--read-alias′ option in
an alias or function for which.
[[email protected]_basic ~]# which --skip-alias ls
/bin/ls
FHS: 文件系统层次结构标准(Filesystem Hierarchy Standard)
/bin, /sbin: 系统自身启动和运行时可能会用到的核心二进制命令
/lib: 共享库文件和内核模块
/lib64:
/etc: 存放配置文件
/etc : Host-specific system configuration
/usr:
/usr/bin, /usr/sbin: 系统运行中,用户为完成某些操作可能用到的命令
/usr/lib, /usr/lib64: 库文件 非核心命令依赖的共享库
/usr/share/man, /usr/share/doc 帮助文档
/dev: devices的简写,所有设备的设备文件都存放于此处;设备文件通常也称为特殊文件(仅有元数据,而没有数据)
/dev : Device files
/proc: 伪文件系统,内核和进程信息的虚拟文件系统接口
/proc : Kernel and process information virtual filesystem
/sys: 伪文件系统,硬件设备信息虚拟文件系统接口
/sys : Kernel and system information virtual filesystem
/boot: 系统引导加载时用到的静态文件,内核(vmlinuz)和ramdisk(CentOS5:initrd, CentOS6:initramfs), 引导加载器grub(bootloader(X86机器才会常见))
/boot : Static files of the boot loader
/home/USERNAME
/home : User home directories (optional)
/root
/mnt 用来临时挂载的目录的
/media
/srv: 服务所用到的数据
/srv : Data for services provided by this system
/tmp: 临时文件存储位置
/var: 经常发生变化的文件
/var/log 存放日志的目录
/misc: 备用目录
/opt: 第三方应用程序的安装目录
/usr/local:
/selinux
Security Enhanced Linux: 安全加强的Linux
查看当前selinux的状态
[[email protected]_basic ~]# getenforce
Permissive
selinux的配置文件,/etc/sysconfig/selinux
修改改可以通过setenforce Value
应用程序的组成部分:
二进制程序
库文件
配置文件:定义程序的特性的
帮助文件
文件管理类命令:
文件类型
文件信息查看
用户和权限
bash的一些特性
管道和重定向
文件文本编辑器
正则表达式
文件查找
[[email protected]inux_basic ~]# ls -l
total 3936
-rw-------. 1 root root 1248 Dec 5 23:44 anaconda-ks.cfg 总共7部分
-r--r--r--. 1 root root 186584 Dec 6 01:46 bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm
-rw-r--r--. 1 root root 0 Dec 11 12:03 blank
查看ls属于那些章节中
[[email protected]_basic ~]# whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
[[email protected]_basic ~]# ls /usr/share/man/ 我们发现有1p
bg da el es fr hu it ko man1 man1x man2x man3p man4 man5 man6 man7 man8 man9 mann overrides pt ro sk sv zh_CN
cs de en fi hr id ja man0p man1p man2 man3 man3x man4x man5x man6x man7x man8x man9x nl pl pt_BR ru sl tr zh
[[email protected]_basic ~]# man ls
LS(1) User Commands LS(1)
NAME
ls - list directory contents
。。。。。。。。。。
-l use a long listing format
。。。。。。。。。。
[[email protected]_basic ~]# man 1p ls
LS(1P) POSIX Programmer’s Manual LS(1P)
。。。。。。。。。。。。。。。
-l (The letter ell.) Do not follow symbolic links named as operands unless the -H or -L options are specified. Write out in long for-
mat (see the STDOUT section). When -l (ell) is specified, -1 (one) shall be assumed.
。。。。。。。。。。。。。。。
If the -l option is specified without -L, the following information shall be written:
"%s %u %s %s %u %s %s\n", <file mode>, <number of links>,
<owner name>, <group name>, <number of bytes in the file>,
<date and time>, <pathname> 总共7部分信息,"%s %u %s %s %u %s %s\n"和后面的格式显示是对应的,详细解释可以自己去man
。。。。。。。。。。。。。
文件类型:
- Regular file.
普通文件:-, f
d Directory.
目录文件: d
l (ell)
Symbolic link.
符号链接文件:l
设备文件:
c Character special file.
字符设备:c (线性设备,只能通过顺序访问的) 键盘,鼠标
b Block special file.
块设备:b (随机设备,可以随机访问) 硬盘
p FIFO.
命名管道:p
套接字文件:s (unix sock文件)
查看文件内容的类型:
file /path/to/somefile
[[email protected]_basic network-scripts]# file /dev/disk/
/dev/disk/: directory
[[email protected]_basic network-scripts]# file ifcfg-eth0
ifcfg-eth0: ASCII text