echo $PATH
[root@VM_0_17_centos ~]# echo $PATH
/root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
alias 别名
格式:**alias 别名=‘cmd’ **
例如:进入到system目录下
alias cdsystem="cd /usr/lib/systemd/system"
注意:临时文件,退出系统后失效
放入root/.bashrc
文件中
[root@VM_0_17_centos ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cdsystem="cd /usr/lib/systemd/system"
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
退出重新登录后即可生效
或
使用source .bashrc
即可生效
补充:.bashrc是在root用户下修改的,其他用户无法使用cdsystem
命令,如果想给全部用户
使用别名(不建议
),则编辑/etc/bashrc文件即可
例如:在文件末尾加上
[root@VM_0_17_centos ~]# cd /etc/
[root@VM_0_17_centos etc]# vi bashrc
略...
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
unset HISTCONTROL
alias cdsystem="cd /usr/lib/systemd/system"
生效:
[root@VM_0_17_centos etc]# source /etc/bashrc
全局的配置不建议设置别名
unalias [-a] name 取消别名
-a: 取消所有别名
source 配置文件生效
文件生效:将内容加载到内存中
持久生效:将内容写入文件中
格式:source .bashrc
或 . .bashrc
source == .
[root@VM_0_17_centos ~]# type source
source is a shell builtin
enable 内部命令
[root@VM_0_17_centos system]# enable
enable .
enable :
enable [
...略
enable source
enable suspend
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
可以看到.
和resource
在其中
enable -n 命令
: 禁用内部命令
enable -n alias
enable 命令
:启用命令
enable alias
选项:-短格式 - -长格式
短格式:-
长格式:- -
例如:
[root@VM_0_17_centos ~]# ls -a
~ .autojump .bash_logout .bashrcX .cshrc git .local .mysql_history .pki
[root@VM_0_17_centos ~]# ls --all
~ .autojump .bash_logout .bashrcX .cshrc git .local .mysql_history .pki
命令的格式
command [选项] [对象]
其中[]
表示可选
例如:
[root@VM_0_17_centos ~]# ls -a dump.rdb
dump.rdb
有些选项可以简写
例如: -l -a
[root@VM_0_17_centos ~]# ls -la
total 304
drwxr-xr-x 3 root root 4096 Jun 25 12:17 ~
dr-xr-x---. 18 root root 4096 Jul 23 14:49 .
dr-xr-xr-x. 23 root root 4096 Jul 23 15:43 ..
drwxr-xr-x 6 root root 4096 Jul 5 11:48 .autojump
-rw------- 1 root root 102287 Jul 23 15:43 .bash_history
略...
多命令顺序执行
命令间用分号隔开,适用于一些命令执行时间较长,想在执行完毕后紧接着执行后面的命令
格式:命令1;命令2;...
[root@VM_0_17_centos ~]# ls;pwd;echo "执行完毕"
deploy_scripts dump.rdb git logs projects root
/root
执行完毕
一个命令分为多行
利用 \
换行
例如:
[root@VM_0_17_centos ~]# hostname
VM_0_17_centos
[root@VM_0_17_centos ~]# host\
> name
VM_0_17_centos
date 系统时间 clock硬件时间
date 操作系统时间
[root@VM_0_17_centos ~]# date
Tue Jul 23 15:55:06 CST 2019
[root@VM_0_17_centos ~]# clock
Tue 23 Jul 2019 03:55:14 PM CST -0.559879 seconds
修改时间
格式:date MMDDHHmmYYYY.ss
-w: 读取操作系统时间覆盖硬件时间
clock -w
-s: 读取硬件时间覆盖操作系统时间
clock -s
日历
一年的日历:cay -y
[root@VM_0_17_centos ~]# cal -y
2019
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
某一月的日历:cal 月 年
[root@VM_0_17_centos ~]# cal 08 2019
August 2019
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
很惊奇的一年
[root@VM_0_17_centos ~]# cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
查看linux版本
cat /etc/centos-release
[root@VM_0_17_centos ~]# cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)
df 查看分区
[root@VM_0_17_centos ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 51474044 17604292 31248388 37% /
devtmpfs 3995440 0 3995440 0% /dev
tmpfs 4005204 24 4005180 1% /dev/shm
tmpfs 4005204 704 4004500 1% /run
tmpfs 4005204 0 4005204 0% /sys/fs/cgroup
tmpfs 801044 0 801044 0% /run/user/0
lsblk
[root@VM_0_17_centos ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.3M 0 rom
vda 253:0 0 50G 0 disk
└─vda1 253:1 0 50G 0 part
runlevel 运行模式
5图形 3字符界面
[root@VM_0_17_centos ~]# runlevel
N 3
模式切换
init 5
echo 显示字符串
[root@VM_0_17_centos ~]# echo "hello word"
hello word
显示系统变量(
[root@VM_0_17_centos ~]# echo $PATH
/root/.autojump/bin:/root/.autojump/bin:/root/.autojump/bin:/usr/local/maven/apache-maven...
命令执行顺序
别名alias —> 内部命令(shell命令) —> hash(看是否把路径hash到路径中了) —> $PATH
-n 换行
[root@VM_0_17_centos ~]# echo hello world # 默认添加了换行
hello world
[root@VM_0_17_centos ~]# echo -n hello world
hello world[root@VM_0_17_centos ~]#
-e
执行完毕后发出警告音
[root@VM_0_17_centos ~]# sleep 10;echo -e "\a"
单引号' '
和双引号" "
- 单引号:强引用,变量不会被替换
- 双引号:弱引用,变量会被其中的值替换
例如:
[root@VM_0_17_centos ~]# echo "$PATH"
/root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
[root@VM_0_17_centos ~]# echo '$PATH'
$PATH
[root@VM_0_17_centos ~]# echo echo $PATH
echo /root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
反单引号 ` `
能识别里面的命令: 通常在命令中调用命令时使用
例如:
root@VM_0_17_centos ~]# echo `echo $PATH`
/root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
- 先执行
` echo $PATH`
结果是:/root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
- 再执行
`echo /root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin `
- 结果是:
/root/.autojump/bin:/usr/local/maven/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/jdk1.8.0_141/bin:/root/bin
命令调用命令:查看某文件的详细信息
[root@VM_0_17_centos ~]# ls -l `echo /bin/bash`
-rwxr-xr-x 1 root root 960392 Aug 3 2016 /bin/bash
$(command) 等价于 反单引号
例如:
[root@VM_0_17_centos ~]# ls -l $(echo $SHELL)
-rwxr-xr-x 1 root root 960392 Aug 3 2016 /bin/bash
iconv 编码
查看所有编码
[root@VM_0_17_centos ~]# iconv -l
The following list contain all the coded character sets known. This does
not necessarily mean that all combinations of these names can be used for
the FROM and TO command line parameters. One coded character set can be
listed with several different names (aliases).
437, 500, 500V1, 850, 851, 852, 855, 856, 857, 860, 861, 862, 863, 864, 865,
866, 866NAV, 869, 874, 904, 1026, 1046, 1047, 8859_1, 8859_2, 8859_3, 8859_4,
8859_5, 8859_6, 8859_7, 8859_8, 8859_9, 10646-1:1993, 10646-1:1993/UCS4,
ANSI_X3.4-1968, ANSI_X3.4-1986, ANSI_X3.4, ANSI_X3.110-1983, ANSI_X3.110,
ARABIC, ARABIC7, ARMSCII-8, ASCII, ASMO-708, ASMO_449, BALTIC, BIG-5,
BIG-FIVE, BIG5-HKSCS, BIG5, BIG5HKSCS, BIGFIVE, BRF, BS_4730, CA, CN-BIG5,
...
转化文件编码:将gb2312编码的文件1转成文件2
[root@VM_0_17_centos ~]# iconv -f gb2312 文件1 -o 文件2
{} 花括号
例如:
[root@VM_0_17_centos ~]# echo {1,2,3}
1 2 3
[root@VM_0_17_centos ~]# echo file{1,2,3}
file1 file2 file3
多个{}
组合使用: (笛卡尔积)
[root@VM_0_17_centos ~]# echo file{1,2,3}.{txt,log}
file1.txt file1.log file2.txt file2.log file3.txt file3.log
范围:
[root@VM_0_17_centos ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@VM_0_17_centos ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@VM_0_17_centos ~]# echo {z..a}
z y x w v u t s r q p o n m l k j i h g f e d c b a
[root@VM_0_17_centos ~]# echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
间隔:
[root@VM_0_17_centos ~]# echo {1..10..2}
1 3 5 7 9
[root@VM_0_17_centos ~]# echo {A..Z..2}
A C E G I K M O Q S U W Y
whatis 帮助
[root@VM_0_17_centos ~]# whatis rm
rm (1) - remove files or directories
如果没有whatis命令,则:
centos6
[root@VM_0_17_centos ~]# makewhatis
centos7
[root@VM_0_17_centos ~]# mandb
man 帮助
cat /etc/man_db.conf
RM(1) User Commands RM(1)
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... FILE...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories.
...略
man -f
等价于whatis
[root@VM_0_17_centos ~]# man -f rm
rm (1) - remove files or directories
- man文档更新
[root@VM_0_17_centos ~]# yum install man-pages
...
Is this ok [y/d/N]: y
Downloading packages:
man-pages-3.53-5.el7.noarch.rpm | 5.0 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : man-pages-3.53-5.el7.noarch 1/1
Verifying : man-pages-3.53-5.el7.noarch 1/1
Installed:
man-pages.noarch 0:3.53-5.el7
Complete!
info
help 内部命令帮助
- enable 命令
[root@VM_0_17_centos ~]# help enable
enable: enable [-a] [-dnps] [-f filename] [name ...]
Enable and disable shell builtins.
Enables and disables builtin shell commands. Disabling allows you to
execute a disk command which has the same name as a shell builtin
without using a full pathname.
Options:
-a print a list of builtins showing whether or not each is enabled
-n disable each NAME or display a list of disabled builtins
-p print the list of builtins in a reusable format
-s print only the names of Posix `special' builtins
Options controlling dynamic loading:
-f Load builtin NAME from shared object FILENAME
-d Remove a builtin loaded with -f
Without options, each NAME is enabled.
To use the `test' found in $PATH instead of the shell builtin
version, type `enable -n test'.
Exit Status:
Returns success unless NAME is not a shell builtin or an error occurs.
- history 命令
[root@VM_0_17_centos ~]# help history
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Display or manipulate the history list.
Display the history list with line numbers, prefixing each modified
entry with a `*'. An argument of N lists only the last N entries.
Options:
-c clear the history list by deleting all of the entries
-d offset delete the history entry at offset OFFSET.
-a append history lines from this session to the history file
-n read all history lines not already read from the history file
-r read the history file and append the contents to the history
list
-w write the current history to the history file
and append them to the history list
-p perform history expansion on each ARG and display the result
without storing it in the history list
-s append the ARGs to the history list as a single entry
If FILENAME is given, it is used as the history file. Otherwise,
if $HISTFILE has a value, that is used, else ~/.bash_history.
If the $HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry. No time stamps are printed otherwise.
Exit Status:
Returns success unless an invalid option is given or an error occurs.
外部命令帮助
history 执行命令的历史
记录在用户的根目录
下的 .bash_history
[root@VM_0_17_centos ~]# history
...略
3116 2019-07-25 17:00:55 man rm
3117 2019-07-25 17:01:15 man -f rm
3118 2019-07-25 17:01:52 man rm
3119 2019-07-25 17:05:21 help enable
3121 2019-07-25 17:06:45 history
3122 2019-07-25 17:08:10 help history
3123 2019-07-25 17:09:54 ls -a
3124 2019-07-25 17:16:52 history
注意:history命令显示的是所有内存中的记录,当用户登录后首先会将 .bash_history
中的记录同步到内存中,当用户退出后才会将将内存中的记录写入到.bash_history
中。所以用户看到的都是内存中的记录!
调用history
中的3123
这条记录,写法 !数字
。
[root@VM_0_17_centos ~]# !3123
ls -a
~ A牛角读书服务器 .bashrc .cshrc .java .m2 .pki .ssh .zcompdump-VM_0_17_centos-5.0.2 .zshrc.omz-uninstalled-2019-07-08_16-07-22
. .bash_history .bashrcX deploy_scripts .lesshst .mysql_history projects .tcshrc
查看history
可以保存的最大条数:$HISTSIZE
[root@VM_0_17_centos ~]# echo $HISTSIZE
3000
调整大小:/etc/profile
[root@VM_0_17_centos ~]# HISTSIZE=1000
历史命令
!!
!n
!-n
history -c
清空内存中的历史- 删除
.bash_history
history -c
删除所有的历史 history -d n
删除第n条历史history -a
将内存中的命令添加到.bash_history
history -r
从.bash_history
读取到内存中history -n
从.bash_history
读取未读取的记录
HISTCONTRL=ignoreboth
: 忽略连续重复执行
的命令和以空格开头
的命令
永久生效:nano ~/.bash_profile
... 略
PATH=$PATH:$HOME/bin
HISTCONTROL=ignoreboth
export PATH
- -help
[root@VM_0_17_centos ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
...略
- -help
不通用
date
- 当前时间
[root@VM_0_17_centos ~]# date
Fri Jul 26 17:04:56 CST 2019
- 前天是周几
[root@VM_0_17_centos ~]# date -d "-2 day" +%A
Wednesday
[root@VM_0_17_centos ~]# date -d "-2 day" +%
3
bash 命令