基础命令及其用法
文章目录
永久改主机名命令
[root@wangaifei ~]# hostnamectl set-hostname wangaifei
[root@wangaifei ~]# shotdown -r now
1.ls
功能:显示当前目录所含的文件和文件夹
[root@wangaifei ~]# ls
a anaconda-ks.cfg b bbb c ccc d dd kong
长格式显示
[root@wangaifei ~]# ls -l
总用量 16
drwxr-xr-x. 4 root root 46 10月 31 17:30 a
-rw-------. 1 root root 1451 10月 29 05:23 anaconda-ks.cfg
做单位转换
[root@wangaifei ~]# ls -hl
总用量 16K
drwxr-xr-x. 4 root root 46 10月 31 17:30 a
-rw-------. 1 root root 1.5K 10月 29 05:23 anaconda-ks.cfg
显示隐藏文件
[root@wangaifei ~]# ls -a
. .. a anaconda-ks.cfg b .bash_history .bash_logout .bash_profile .bashrc bbb c ccc .cshrc d dd kong .tcshrc
显示目录自身属性
[root@wangaifei ~]# ls -d
.
以时间排序
[root@wangaifei ~]# ls -t
bbb dd ccc c d b a kong anaconda-ks.cfg
逆序显示
[root@wangaifei ~]# ls -r
kong dd d ccc c bbb b anaconda-ks.cfg a
2.history
功能:用于查看命令历史
[root@wangaifei ~]# history
1 vi /etc/sysconfig/network-scripts/ifcfg-ens33
2 ifconfig
[root@wangaifei ~]# history -d 1
[root@wangaifei ~]# history
1 ifconfig
[root@wangaifei ~]# history -c
[root@wangaifei ~]# history
1 history
-w:保存命令至~/.bash_history中
3. HISTSIZE
功能:定义命令历史最多能存多少条,默认为1000条
4.SHELL
功能:当前系统使用的shell
5.cp
功能:复制文件,一个文件到另一个文件,多个文件到一个目录
文件到文件
[root@wangaifei ~]# ls
a anaconda-ks.cfg bb bbb c d dd e hello hh kong
[root@wangaifei ~]# cp hello world
[root@wangaifei ~]# ls
a anaconda-ks.cfg bb bbb c d dd e hello hh kong world
文件到目录
[root@wangaifei ~]# cp hello world hw
[root@wangaifei ~]# ls
a anaconda-ks.cfg bb bbb c d dd e hello hh hw kong world
[root@wangaifei ~]# ls hw
hello world
递归拷贝 目录到目录
[root@wangaifei ~]# cp -r a hw
[root@wangaifei ~]# ls
a anaconda-ks.cfg bb bbb c d dd e hello hh hw kong world
[root@wangaifei ~]# ls hw
a hello world
6.cd
功能:切换目录
[root@wangaifei ~]# cd a
[root@wangaifei a]#
7.pwd
功能:打印当前工作目录
[root@wangaifei a]# pwd
/root/a
8.mkdir
功能:创建目录
[root@wangaifei ~]# mkdir e
[root@wangaifei ~]# ls
a anaconda-ks.cfg b bbb c ccc d dd e kong
-p 创建目录时若父目录不存在则自动创建
[root@wangaifei ~]# mkdir -p a/b/c
[root@wangaifei ~]# cd a
[root@wangaifei a]# ls
aaa b bbb c
[root@wangaifei a]# cd b
[root@wangaifei b]# ls
c
-v 显示创建目录的过程
[root@wangaifei b]# mkdir -pv a/b/c
mkdir: 已创建目录 "a"
mkdir: 已创建目录 "a/b"
mkdir: 已创建目录 "a/b/c"
9.touch
功能:创建新文件或更新时间轴
[root@wangaifei ~]# touch f
[root@wangaifei ~]# ls -lh
总用量 16K
-rw-r--r--. 1 root root 0 10月 31 20:48 f
[root@wangaifei ~]# touch f
[root@wangaifei ~]# ls -hl
总用量 16K
-rw-r--r--. 1 root root 0 10月 31 20:50 f
10.stat
功能:显示文件或文件系统的状态
[root@wangaifei ~]# stat a
文件:"a"
大小:46 块:0 IO 块:4096 目录
设备:fd00h/64768d Inode:50850407 硬链接:4
权限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2019-10-31 17:31:06.272466599 +0800
最近更改:2019-10-31 17:30:55.916251036 +0800
最近改动:2019-10-31 17:30:55.916251036 +0800
创建时间:-
11.rm
功能:删除文件
-r 递归删除,删除目录时必须使用此选项
[root@wangaifei ~]# rm -r f
rm:是否删除普通空文件 "f"?y
[root@wangaifei ~]# ls
a anaconda-ks.cfg b bbb c ccc d dd e kong
-f 强制删除,不询问
[root@wangaifei ~]# rm -f ccc
[root@wangaifei ~]# ls
a anaconda-ks.cfg b bbb c d dd e kong
14 mv
功能:移动文件
[root@wangaifei ~]# mv b e
[root@wangaifei ~]# ls
a anaconda-ks.cfg bbb c d dd e kong
[root@wangaifei ~]# cd e
[root@wangaifei e]# ls
b
[root@wangaifei ~]# mv ab a //重命名
[root@wangaifei ~]# ls
1 2 3 4 5 5} 6 7 a anaconda.ks.cfg passwd xixi
[root@wangaifei ~]# mv a abc
[root@wangaifei ~]# ls
1 2 3 4 5 5} 6 7 abc anaconda.ks.cfg passwd xixi
13.cat
功能:拼接文件内容并输出至标准输出
[root@wangaifei ~]# ls
a anaconda-ks.cfg bbb c d dd e hh kong
[root@wangaifei ~]# cat hh
hhhhh
以带行号的形式查看文件内容
[root@wangaifei ~]# cat -n hh
1 hhhhh
2 xxxxxxxhhhhh
3 xhhh
4 xhhh
5 xhhh
6 xhhh
将多个文件的内容显示到屏幕上
[root@wangaifei ~]# cat hh dd
hhhhh
xxxxxxxhhhhh
xhhh
xhhh
xhhh
xhhh
hello world
nihao
xhhh
xhhh
xhhh
xhhh
xhhh
将多个文件的内容合并写到一个新文件中
[root@wangaifei ~]# cat hh dd >>bb
[root@wangaifei ~]# cat bb
hhhhh
xxxxxxxhhhhh
xhhh
xhhh
xhhh
xhhh
hello world
nihao
xhhh
xhhh
xhhh
xhhh
xhhh
取文件前三行
[root@wangaifei ~]# cat -n anaconda-ks.cfg | head -3
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
tailf/tail -f 实时追踪一个文件内容
14.more
功能:全屏查看文件内容,只能从前往后看,看完自动退出
查看时以百分比形式显示文章进度
15.less
功能:全屏查看文本文件内容,可从前往后看,也可以从后往前看,看完不会自动退出
16.head
[root@wangaifei ~]# cat -n anaconda-ks.cfg | head
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
4 repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
5 repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
6 # Use CDROM installation media
7 cdrom
8 # Use graphical install
9 graphical
10 # Run the Setup Agent on first boot
打印文件前5行内容
[root@wangaifei ~]# cat -n anaconda-ks.cfg |head -5
1 #version=DEVEL
2 # System authorization information
3 auth --enableshadow --passalgo=sha512
4 repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
5 repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
功能:从文件首部开始打印文件内容,默认打印十行
17.tail
功能:从文件尾部开始打印文件内容,默认打印十行
[root@wangaifei ~]# cat -n anaconda-ks.cfg |tail
41
42 %addon com_redhat_kdump --enable --reserve-mb='auto'
43
44 %end
45
46 %anaconda
47 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
48 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
49 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
50 %end
打印文件后4行的内容
[root@wangaifei ~]# cat -n anaconda-ks.cfg |tail -4
47 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
48 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
49 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
50 %end
18.wc
功能:文本统计
[root@wangaifei ~]# wc anaconda-ks.cfg
50 128 1451 anaconda-ks.cfg
统计文本字节数
[root@wangaifei ~]# wc -l anaconda-ks.cfg
50 anaconda-ks.cfg
统计文本单词数
[root@wangaifei ~]# wc -c anaconda-ks.cfg
1451 anaconda-ks.cfg
统计文本行数
[root@wangaifei ~]# wc -w anaconda-ks.cfg
128 anaconda-ks.cfg
19.du
功能:查看文件或目录占用的磁盘空间大小
显示总的占用空间的大小
[root@wangaifei ~]# du -s anaconda-ks.cfg
4 anaconda-ks.cfg
单位转换,以更友好的方式显示大小
[root@wangaifei ~]# du -sh anaconda-ks.cfg
4.0K anaconda-ks.cfg
20. df
功能:报告文件系统磁盘空间使用情况
[root@wangaifei ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17811456 973620 16837836 6% /
devtmpfs 922456 0 922456 0% /dev
tmpfs 933524 0 933524 0% /dev/shm
tmpfs 933524 8888 924636 1% /run
tmpfs 933524 0 933524 0% /sys/fs/cgroup
/dev/sda1 1038336 145856 892480 15% /boot
tmpfs 186708 0 186708 0% /run/user/0
[root@wangaifei ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 951M 17G 6% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.7M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 183M 0 183M 0% /run/user/0
[root@wangaifei ~]# df -T
文件系统 类型 1K-块 已用 可用 已用% 挂载点
/dev/mapper/rhel-root xfs 17811456 973620 16837836 6% /
devtmpfs devtmpfs 922456 0 922456 0% /dev
tmpfs tmpfs 933524 0 933524 0% /dev/shm
tmpfs tmpfs 933524 8888 924636 1% /run
tmpfs tmpfs 933524 0 933524 0% /sys/fs/cgroup
/dev/sda1 xfs 1038336 145856 892480 15% /boot
tmpfs tmpfs 186708 0 186708 0% /run/user/0
21.hostname
功能:查看或临时修改主机名,重开终端有效,重启失效
[root@wangaifei ~]# hostname
wangaifei
[root@wangaifei ~]# hostname WAF
[root@wangaifei ~]# hostname
WAF
[root@wangaifei ~]# bash
[root@WAF ~]#
22.hostnamectl
功能:查看或永久修改主机名,重开终端有效,重启依然有效
[root@WAF ~]# hostnamectl set-hostname wangaifei
[root@WAF ~]# bash
[root@wangaifei ~]# hostname
wangaifei
[root@wangaifei ~]#
23.clear
功能:清屏
24.whoami
功能:显示当前登录用户
[root@wangaifei ~]# whoami
root
查看当前用户登录的详细信息
[root@wangaifei ~]# who am i
root pts/2 2019-11-01 14:18 (192.168.119.1)
25.who
功能:查看当前在线用户
[root@wangaifei ~]# who
root tty1 2019-11-01 13:49
root pts/0 2019-10-31 19:30 (192.168.119.1)
root pts/1 2019-11-01 13:51 (192.168.119.1)
root pts/2 2019-11-01 14:18 (192.168.119.1)
26.w
功能:显示当前在线用户并显示其正在运行的命令
[root@wangaifei ~]# w
14:57:30 up 3:06, 4 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 13:49 1:06m 0.01s 0.01s -bash
root pts/0 192.168.119.1 四19 17:36m 0.33s 0.33s -bash
root pts/1 192.168.119.1 13:51 55:14 0.09s 0.09s -bash
root pts/2 192.168.119.1 14:18 2.00s 0.15s 0.02s w
27.which
功能;显示指定命令的绝对路径
[root@wangaifei ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
28.cal
功能:打印日历
[root@wangaifei ~]# cal
十一月 2019
日 一 二 三 四 五 六
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
[root@wangaifei ~]# cal 2022
2022
一月 二月 三月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
四月 五月 六月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
七月 八月 九月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
十月 十一月 十二月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
29.ldd
功能:查看指定程序有哪些依赖库文件
[root@wangaifei ~]# ldd /usr/bin/ls
linux-vdso.so.1 => (0x00007ffc5ab1b000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc63a87d000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007fc63a678000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007fc63a46e000)
libc.so.6 => /lib64/libc.so.6 (0x00007fc63a0ab000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fc639e49000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fc639c44000)
/lib64/ld-linux-x86-64.so.2 (0x000055b84337d000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007fc639a3f000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc639823000)
30.date
功能:显示或设置日期与时间
[root@wangaifei ~]# date
2019年 11月 01日 星期五 15:05:59 CST
[root@wangaifei ~]# date
2019年 11月 01日 星期五 15:05:59 CST
[root@wangaifei ~]# date '+%Y'
2019
[root@wangaifei ~]# date '+%m'
11
[root@wangaifei ~]# date '+%d'
01
[root@wangaifei ~]# date '+%y%m%d'
191101
[root@wangaifei ~]# date '+%y-%m-%d'
19-11-01
[root@wangaifei ~]# date '+%y-%m-%d %H'
19-11-01 15
[root@wangaifei ~]# date '+%y-%m-%d %H:%M'
19-11-01 15:10
[root@wangaifei ~]# date '+%y-%m-%d %H:%M:%S'
19-11-01 15:10:30
归档与压缩
常见的归档与压缩文件格式:
- .gz
- .bz2
- .xz
- .zip
- .Z
压缩工具之gzip
- 格式:gzip 选项 文件
- -d 解压缩
[root@wangaifei ~]# gzip 1
[root@wangaifei ~]# ls
10 12 14 16 18 1.gz 20 4 6 8 a bb c dd hello hw world
11 13 15 17 19 2 3 5 7 9 anaconda-ks.cfg bbb d e hh kong
[root@wangaifei ~]# file 1.gz
1.gz: gzip compressed data, was "1", from Unix, last modified: Fri Nov 1 18:04:48 2019
解压缩:
[root@wangaifei ~]# gzip -d 1
[root@wangaifei ~]# ls
1 11 13 15 17 19 20 4 6 8 a bb c dd hello hw world
10 12 14 16 18 2 3 5 7 9 anaconda-ks.cfg bbb d e hh kong
bzip2
- 格式:bzip2 选项 文件
- -k 压缩时保留原文件
- -d 解压缩
[root@wangaifei ~]# bzip2 2
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 4 6 8 a bb c dd hello hw world
10 12 14 16 18 20 3 5 7 9 anaconda-ks.cfg bbb d e hh kong
解压缩
[root@wangaifei ~]# bzip2 -d 2.bz2
[root@wangaifei ~]# ls
1 11 13 15 17 19 20 4 6 8 a bb c dd hello hw world
10 12 14 16 18 2 3 5 7 9 anaconda-ks.cfg bbb d e hh kong
压缩并保留原文件
[root@wangaifei ~]# bzip2 -k 3
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong
10 12 14 16 18 20 3 4 6 8 a bb c dd hello hw world
xz
- 格式:xz 选项 文件
- -k 压缩时保留原文件
- -d 解压缩
压缩为4.xz原文件会被删除
[root@wangaifei ~]# xz 4
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong
10 12 14 16 18 20 3 4.xz 6 8 a bb c dd hello hw world
解压缩
[root@wangaifei ~]# xz -d 4.xz
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong
10 12 14 16 18 20 3 4 6 8 a bb c dd hello hw world
zip
- 格式:zip filename.zip file1 file2…
- zip filename.zip DIR/*
- zip压缩包要用unzip工具来解压
压缩文件名字为1-9 的文件到number。zip文件中
[root@wangaifei ~]# touch number
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong world
10 12 14 16 18 20 3 4 6 8 a bb c dd hello hw number
[root@wangaifei ~]# zip number.zip [0-9]
adding: 1 (stored 0%)
adding: 3 (stored 0%)
adding: 4 (stored 0%)
adding: 5 (stored 0%)
adding: 6 (stored 0%)
adding: 7 (stored 0%)
adding: 8 (stored 0%)
adding: 9 (stored 0%)
[root@wangaifei ~]# ls
1 11 13 15 17 19 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong number.zip
10 12 14 16 18 20 3 4 6 8 a bb c dd hello hw number world
归档工具之tar
- tar [OPTION] [FILE]
- 常用的OPTION
- -c 创建归档文件
- -f file.tar 指定要操作的归档文件
- -x 还原归档
- -v显示归档过程
- -p归档时保留权限信息,只有管理员才有权限使用此选项
tar zcf 使用gzip格式压缩
tar jcf 使用bzip2格式压缩
tar Jcf 使用xz格式压缩
tar xf 解压缩
[root@wangaifei ~]# tar -zcf 1.tar.gz *
[root@wangaifei ~]# ls
1 11 13 15 17 19 20 3 4 6 8 a bb c dd hello hw number world
10 12 14 16 18 1.tar.gz 2.bz2 3.bz2 5 7 9 anaconda-ks.cfg bbb d e hh kong number.zip
[root@wangaifei ~]# file 1.tar.gz
1.tar.gz: gzip compressed data, from Unix, last modified: Fri Nov 1 18:34:10 2019
sort命令
功能:对文件内容进行排序
- -n升序
- rn 降序
[root@wangaifei ~]# cat 9
5
4
3
6
7
3
7
4
2
4
[root@wangaifei ~]# sort 9
2
3
3
4
4
4
5
6
7
7
降序
[root@wangaifei ~]# sort -rn 9
7
7
6
5
4
4
4
3
3
2
[root@wangaifei ~]#
只显示重复的行,使用uniq必须先用sort排序
[root@wangaifei ~]# sort -n 9 | uniq -d
3
4
7
[root@wangaifei ~]# cat 9
5
4
3
6
7
3
7
4
2
4
只显示不重复的行
[root@wangaifei ~]# cat 9
5
4
3
6
7
3
7
4
2
4
[root@wangaifei ~]# sort -n 9 | uniq -u
2
5
6
显示每个数字出现的个数
[root@wangaifei ~]# sort -n 9 | uniq -c
1 2
2 3
3 4
1 5
1 6
2 7