在shell中可执行的命令有两类:
內建命令:由shell自带的,而且通过某命令形式停工;
外部命令:在当前系统的某文件系统系统路径下有对应的可执行程序文件
which、whereis
[root@localhost ~]# which tty
/usr/bin/tty
[root@localhost ~]# which cat
/bin/cat
[root@localhost ~]# whereis tty
tty: /usr/bin/tty /usr/share/man/man4/tty.4.gz /usr/share/man/man1/tty.1.gz /usr/share/man/man1p/tty.1p.gz
[root@localhost ~]# whereis cat
cat: /bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz
区别內建或外部命令:
#type command
內建命令:
[root@localhost ~]# type cd
cd is a shell builtin
外部命令:
[root@localhost ~]# type tty
tty is hashed (/usr/bin/tty)
运行命令:
命令格式:
commmand [options选项...] [arguments参数...]
选项:用户启用或关闭命令的某个或某些功能
短选项:-c,例如:-l、-h
多个短选项可合并使用,例如:-l、-h,可写作:-lh
[root@localhost ~]# ls -l -h
total 72K
-rw------- 1 root root 1.3K Mar 15 16:47 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.0K Mar 15 16:53 Desktop
-rw-r--r-- 1 root root 37K Mar 15 16:47 install.log
-rw-r--r-- 1 root root 3.6K Mar 15 16:47 install.log.syslog
-rw-r--r-- 1 root root 195 Mar 15 16:51 scsrun.log
[root@localhost ~]# ls -lh
total 72K
-rw------- 1 root root 1.3K Mar 15 16:47 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.0K Mar 15 16:53 Desktop
-rw-r--r-- 1 root root 37K Mar 15 16:47 install.log
-rw-r--r-- 1 root root 3.6K Mar 15 16:47 install.log.syslog
-rw-r--r-- 1 root root 195 Mar 15 16:51 scsrun.log
长选项:--word,例如:--long,--human-readable
长选项一般不能合并使用
[root@localhost ~]# ls --human-readable
anaconda-ks.cfg Desktop install.log install.log.syslog scsrun.log
参数:命令的作用对象;向命令提供数据;
注意:
1、多选项、以及多参数和命令之间都应该使用空白字符分隔;
2、取消命令执行,Ctrl+c;
文件系统:
Windows C:\Program files\office1\word\word.exe
Linux /etc/sysconfig/network-scripts/ifcfg-eth0
文件有两类数据:
元数据:metadata 数据属性信息、存放在索引中
数据:data
1、文件名严格区分字符大小写:file1、File1、FILE1是不同的部件;
2、文件名可使用除/以外的任意字符,不建议使用特殊字符;/是根目录,路径分隔符;
3、文件名长度最长不能超过255个字符;
4、所有以.开头的文件,均为隐藏文件;
路径:
绝对路径,从根目录起始的路径:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
相对路径,从当前目录起始的路径:
[root@localhost ~]# cd /etc
[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd sysconfig/network-scripts/
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
.:表示当前目录
..:表示当前目录的上一级目录、父目录
当前目录:current directory,也称作working directory;工作目录
pwd:print working directory 显示工作目录
LSB:linux standard base linux标准库
Linux命令帮助的获取
外部命令:都有一个可执行程序,位于文件系统某目录下:
which、whereis
shell程序搜寻可执行程序文件的路径定义在PATH环境变量中
# echo $PATH
[root@localhost ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
注意:自左向右的搜寻
格式:command [options...] [arguments...] (命令 选项... 参数...)
內建命令: # help command
外部命令:
1、# command --help # command -h
2、使用手册(manual) # man command
3、信息页 # info command
4、程序自身的帮助文档 readme、install、changelog
5、程序官方文档、官方站点的Documentation
6、发行版的官方文档
7、Google
内部命令帮助
# help command
[root@localhost ~]# help cd
cd: cd [-L|-P] [dir]
shell搜寻到的外部命令的路径结果会缓存到kv(key-value键值)存储中
history命令:
作用是什么?相关的环境变量是什么?有用的选项?
管理命令历史,会读取命令历史文件中记录的命令:~/.bash_history
[root@localhost ~]# cat .bash_history
cat .bash_history
echo "" > .bash_history
cat .bash_history
history -a
登录进shell后新执行的命令只会记录在缓存中:这些命令会在用户退出时“追加”到命令历史文件中:
history:
-a:追加本次会话新执行的命令历史列表到历史文件中;
[root@localhost ~]# history -a
[root@localhost ~]# cat .bash_history
echo "" > .bash_history
history -a
cat .bash_history
init 6
history -a
-d # :删除历史中指定的命令;
-c:清空命令历史;
[root@localhost ~]# history -c
[root@localhost ~]# history
1 history
快捷操作:
!#:调用历史中第#条命令;
[root@localhost ~]# history
1 history
2 ls
3 history
[root@localhost ~]# !2
ls
anaconda-ks.cfg install.log scsrun.log
Desktop install.log.syslog
!string:调用历史中最近一个以string开头的命令;
[root@localhost ~]# history
1 history
2 cd
3 ls
4 history
[root@localhost ~]# !c
cd
!!:执行上一条命令
[root@localhost ~]# history
1 history
2 ls
3 cd
4 history
[root@localhost ~]# !!
history
1 history
2 ls
3 cd
4 history
5 history
外部命令获取帮助:
man command
手册页:/usr/share/man
man1...man8
man1:用户命令
man2:系统调用
man3:C库调用
man4:设备文件及特殊文件
man5:配置文件格式
man6:游戏
man7:杂项
man8:管理类的命令
注意:有些命令在不止一个章节中存在帮助手册
要查看指定章节中的手册:man # command;whatis command该命令可以帮助查询敏玲在哪些章节中存在帮助手册;
[root@localhost ~]# whatis passwd
passwd (1) - update user's authentication tokens
passwd (5) - password file
passwd (rpm) - The passwd utility for setting/changing passwords using PAM
passwd [sslpasswd] (1ssl) - compute password hashes
[root@localhost ~]# man 1 passwd
[root@localhost ~]# man 5 passwd
man命令的配置文件:/etc/man.config
manpath /path/to/somewhere:指定新的手册文件搜索位置
MANPATH_MAP /bin /usr/share/man
MANPATH_MAP /sbin /usr/share/man
MANPATH_MAP /usr/bin /usr/share/man
MANPATH_MAP /usr/sbin /usr/share/man
MANPATH_MAP /usr/local/bin /usr/local/share/man
MANPATH_MAP /usr/local/sbin /usr/local/share/man
MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
MANPATH_MAP /usr/bin/mh /usr/share/man
man -M /path/to/somewhere command:到指定位置下搜索comand命令的手册页并显示之;
[root@localhost ~]# man -M /usr/share/man passwd
帮助手册中的段落说明:
NAME 命令名称、基本功能
SYNOPSIS 使用简要帮助
DESCRIPTION 描述详细功能
OPTIONS 选项
EXAMPLE 使用示例
AUTHOR 作者
REPORTING BUGS 报告BUGS的文职
SEE ALSO 参考
SYNOPSIS:
[]:可选内容
:必选内容
a|b:二选一、或者多选一
...:同一内容可出现多次
man命令的操作方法:
space空格键、ctrl+V、ctrl+f、ctrl+F:向文件尾部翻屏
b、ctrl+B:向文件首部翻屏
d、ctrl+D:向文件尾部翻半屏
u、ctrl+U:向文件首部翻半屏
RETURN回车键、ctrl+N、ectrl+E、j、ctrl+J:向文件尾部翻一行
y、ctrl+Y、ctrl+P、k、ctrl+K:向文件首部翻一行
q:退出
#:跳转到第#行;
1G:回到文件首部
G:翻到文件尾部
文件搜索:
/keyword:以keyword指定的字符串为关键字,从当前位置向文件尾部搜索;不区分字符大小写;
n:下一个
N:上一个
?keyword:以keyword指定的字符串为关键字,从当前位置向文件首部搜索;不分区字符大小写;
n:跟搜索命令同方向,下一个
N:跟搜索命令反方向,上一个
info命令
info command
程序自身的帮助文档
/usr/share/doc/command-version
[root@localhost ~]# cd /usr/share/doc
[root@localhost doc]# ll
total 3864
drwxr-xr-x 2 root root 4096 Mar 15 16:45 a2ps-4.13b
drwxr-xr-x 2 root root 4096 Mar 15 16:44 acl-2.2.39
drwxr-xr-x 2 root root 4096 Mar 15 16:47 alacarte-0.10.0
drwxr-xr-x 2 root root 4096 Mar 15 16:45 alchemist-1.0.36
drwxr-xr-x 2 root root 4096 Mar 15 16:45 alsa-lib-1.0.17
基础命令:
date:
date [option]...[+format]:显示
format:显示符号
%y 两位的年份
[root@localhost doc]# date +%y
18
%Y 四位的年份
[root@localhost doc]# date +%Y
2018
%m 月份
[root@localhost doc]# date +%m
03
%h 月份英文缩写
[root@localhost doc]# date +%h
Mar
%d 当月的几号
[root@localhost doc]# date +%d
19
%H 小时
[root@localhost doc]# date +%H
11
%M 分钟
[root@localhost doc]# date +%M
03
%S 秒钟
[root@localhost doc]# date +%S
28
%s unix元年到现在的秒数
[root@localhost doc]# date +%s
1521428720
%D 日期
[root@localhost doc]# date +%D
03/19/18
%F 日期
[root@localhost doc]# date +%F
2018-03-19
%T 时间
[root@localhost doc]# date +%T
11:07:57
date [MMDDhhmm[[cc]yy][.ss]] :设置日期时间
MM:月份
DD:几号
hh:小时
mm:分钟
yy:两位年份
ccyy:四位年份
.ss:秒钟
[root@localhost doc]# date 0319112018.00
Mon Mar 19 11:20:00 CST 2018
[root@localhost doc]# date 031911202018.00
Mon Mar 19 11:20:00 CST 2018
Linux的两种时钟:
系统时钟:由Linux内核CPU的工作频率进行的计时:date
硬件时钟:hwclock
[root@localhost doc]# date;hwclock
Mon Mar 19 11:22:51 CST 2018
Mon 19 Mar 2018 11:23:12 AM CST -0.812703 seconds
hwclock:显示硬件时钟
-s,--hctosys 以硬件时钟为准设置系统时钟
[root@localhost doc]# hwclock -s
[root@localhost doc]# date;hwclock
Mon Mar 19 11:24:59 CST 2018
Mon 19 Mar 2018 11:25:00 AM CST -0.634669 seconds
-w,--systohc 以系统始终为准设置硬件时钟
[root@localhost doc]# hwclock -w
[root@localhost doc]# date;hwclock
Mon Mar 19 11:26:14 CST 2018
Mon 19 Mar 2018 11:26:16 AM CST -0.537715 seconds
cal:显示日历
[root@localhost doc]# cal
March 2018
Su Mo Tu We Th Fr Sa
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 31
目录相关的命令
当前目录或工作目录
主目录,家目录;HOME
root:/root
普通用户:/home/username、/home/tom
~:表示用户的家目录
cd
cd或cd~:表示切换到当前用户的主目录
[root@localhost ~]# cd
[root@localhost ~]# pwd
/root
[root@localhost tom]# cd ~
[root@localhost ~]# pwd
/root
cd ~username:表示切换到指定用户的家目录
[root@localhost ~]# cd ~tom
[root@localhost tom]# pwd
/home/tom
cd - :在上一个目录和当前目录之间来回切换
[root@localhost home]# cd -
/root
[root@localhost ~]# cd -
/home
.:表示当前目录
..:表示当前目录的父目录
相关的环境变量
PWD:保存了当前目录路径
OLDPWD:上一次所在目录路径
[root@localhost home]# echo $PWD
/home
[root@localhost home]# echo $OLDPWD
/root
pwd:显示当前工作目录
ls:list 显示指定路径下的文件列表
ls [option]...[dir]...
-a,--all:显示所有文件,包括隐藏文件
[root@localhost home]# ls -a
. .. tom
[root@localhost home]# ls --all
. .. tom
-l:长格式
-rw-r--r-- 1 root root 37412 Mar 15 16:47 install.log
-rw-r--r--
最左侧的第一位:文件类型
-:普通文件
d:目录文件
l:链接文件
b:块设备
c:字符设备
p:管道文件
s:套接字文件
后面的9位,访问权限,perm
数字1:文件被硬链接的次数;
左root:文件的owner,文件的属主
右root:文件的group,文件的属组
37412:文件的size
Mar 15 16:47:文件的最近一次被修改的时间
install.log:文件名
-h,--human-readable:单位换算,以人类易读的格式显示文件大小,通常与选项-l一起使用
[root@localhost ~]# ls -lh
total 72K
-rw------- 1 root root 1.3K Mar 15 16:47 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.0K Mar 15 16:53 Desktop
-rw-r--r-- 1 root root 37K Mar 15 16:47 install.log
-rw-r--r-- 1 root root 3.6K Mar 15 16:47 install.log.syslog
-rw-r--r-- 1 root root 195 Mar 15 16:51 scsrun.log
-d:显示目录自身的相关属性;通常要与-l一起使用;
[root@localhost ~]# ls -ld
drwxr-x--- 14 root root 4096 Mar 16 08:30 .
-r,--reverse:逆序显示
[root@localhost ~]# ls -lr
total 72
-rw-r--r-- 1 root root 195 Mar 15 16:51 scsrun.log
-rw-r--r-- 1 root root 3672 Mar 15 16:47 install.log.syslog
-rw-r--r-- 1 root root 37412 Mar 15 16:47 install.log
drwxr-xr-x 2 root root 4096 Mar 15 16:53 Desktop
-rw------- 1 root root 1264 Mar 15 16:47 anaconda-ks.cfg
-R,--recurisve:递归显示
[root@localhost ~]# ls -R /root
/root:
anaconda-ks.cfg Desktop install.log install.log.syslog scsrun.log
/root/Desktop:
stat /path/to/somefile:获取指定文件的元数据属性信息:
[root@localhost ~]# stat /etc/fstab
File: `/etc/fstab'
Size: 534 Blocks: 16 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 652169 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-03-19 09:01:14.000000000 +0800
Modify: 2018-03-15 16:44:22.000000000 +0800
Change: 2018-03-15 16:47:52.000000000 +0800
文件查看命令:cat、tac
cat [option]...[file]...
[root@localhost ~]# cat /etc/fstab /etc/issue
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
Kernel \r on an \m
[root@localhost ~]# tac /etc/fstab /etc/issue
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
LABEL=/boot /boot ext3 defaults 1 2
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
Kernel \r on an \m
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
-E:显示行结束符$
[root@localhost ~]# cat -E /etc/issue
Red Hat Enterprise Linux Server release 5.7 (Tikanga)$
Kernel \r on an \m$
$
-n:对显示出的每一行进行编号
[root@localhost ~]# cat -n /etc/issue
1 Red Hat Enterprise Linux Server release 5.7 (Tikanga)
2 Kernel \r on an \m
3
文件内容类型查看命令:file
file /path/to/somewhere
[root@localhost ~]# file /etc/issue
/etc/issue: ASCII text
[root@localhost ~]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
[root@localhost ~]# file /dev/console
/dev/console: character special (5/1)
回显命令:echo
-n:禁止自动添加换行符号
[root@localhost ~]# echo "how are you?"
how are you?
[root@localhost ~]# echo -n "how are you?"
how are you?[root@localhost ~]#
-e:允许使用转义符
\n 换行
[root@localhost ~]# echo -e "how \nare you?"
how
are you?
\b 退格
[root@localhost ~]# echo -e "how\b are you?"
ho are you?
\t 制表符 Tab键
[root@localhost ~]# echo -e "how\t are you?"
how are you?
echo "$VAR_NAME":变量会替换,双引号表示弱引用
[root@localhost ~]# echo "$PS1"
[\u@\h \W]\$
echo '$VAR_NAME' :变量不会替换,单引号表示强引用
[root@localhost ~]# echo '$PS1'
$PS1
which:显示命令对应的程序文件路径
which [option] command
[root@localhost ~]# which ls
alias ls='ls --color=tty'
/bin/ls
[root@localhost ~]# which cat
/bin/cat
--skip-alias:禁止显示别名
[root@localhost ~]# which ls
alias ls='ls --color=tty'
/bin/ls
[root@localhost ~]# which --skip-alias ls
/bin/ls
whatis:whatis命令显示手册部分的页眉行。然后可以发出man命令以获取附加的信息
[root@localhost ~]# whatis cd
cd (1p) - change the working directory
cd [builtins] (1) - bash built-in commands, see bash(1)
[root@localhost ~]# whatis mkdir
mkdir (1) - make directories
mkdir (1p) - make directories
mkdir (2) - create a directory
mkdir (3p) - make a directory
[root@localhost ~]# whatis useradd
useradd (8) - create a new user or update default new user information
使用makewathis命令可将当前系统上所有的帮助手册及与之对应的关键字创建为一个数据库。