常用命令
-
pwd: printing working director
功能: 显示工作目录
[root@localhost ~]# cd / [root@localhost /]# pwd / -
cd: change directory
功能: 切换目录
cd [/PATH/TO/SOMEDIR]
cd: 不加任何参数, 表示切换到家目录
cd ~ 也是表示切换到家目录
cd ~用户名 管理员可以切换到任意的家目录
cd - 在上一次所在目录与当前所在目录可以来回切换
bash中有两个变量名称保存了上次路径和当前路径的值
cd - 就相当于 cd $ OLDPWD 当前的路径变量名是 $PWD
cd .. 切换到上一级目录
[root@localhost ~]# cd /usr [root@localhost usr]# pwd /usr [root@localhost usr]# cd [root@localhost ~]# pwd /root [root@localhost ~]# cd ~centos [root@localhost centos]# [root@localhost centos]# cd [root@localhost ~]# cd - /home/centos [root@localhost centos]# cd - /root [root@localhost man1]# echo $PWD /usr/share/man/man1 [root@localhost man1]# echo $OLDPWD /usr/share/man -
ls : - list directory contents
功能: 显示目录下的文件内容
用法: ls [OPTION]... [FILE]... ...表示可以多个参数之间用空格分隔, 可以指定几个文件路径显示
参数说明:
-a: 显示所有的文件, 包括以 . 开头的隐藏文件
-A: 显示出了. 和 .. 之外的所有文件
-l: --long 显示文件信息的长格式, 显示文件的详细信息..
-h: --human-readable 人类可读的形式, 就是换算成G M这些单位, 会损失精度
-d: 显示文件夹本身的信息, 一般与-l连用
-r: reverse 将文件排序后逆序输出, 自身按照首字母从小到大排下来
-R: recursive 将当前路径的文件递归的显示出来, 即包括所有子文件夹
-rw-r--r--. 1 root root 592 Jan 1 2010 test.txt drwxr-xr-x. 2 root root 6 Aug 3 14:20 Videos -/d: 表示文件类型, 文件类型有 -, d, b, c, l, s, p rwxr-xr-x: 这个分为3组 前面3位: rwx. 文件数组的权限 中间3位: r-x. 文件属组的权限 后面3位: r-x. 其他用户(非属主, 属组)的权限 最后一个.: 表示拥有隐藏文件 数字1: 表示文件被硬链接的次数 root: 文件属主 root: 文件属组 592: 文件的大小, 单位字节 6 Aug 3 14:20: 表示文件最后修改的时间 -
cat: concatenate files and print on the standard output
功能: 查看文本文件的内容, 显示在标准输出. 可以连接多个文本并显示
cat [OPTION]... [FILE]...
-n: --number 显示行号
-E: --show-ends 在每一行的结尾加上$符号
[root@localhost man1]# cat -n /etc/issue /etc/fstab
1 \S
2 Kernel \r on an \m
3
4
5 #
6 # /etc/fstab
7 # Created by anaconda on Sat Aug 3 12:13:44 2019
```
5. **tac**:
功能: 与cat正好相反, 把文件的内容逆序输出
用法与cat相同
```bash
[root@localhost man1]# cat /etc/issue
\S
Kernel \r on an \m
[root@localhost man1]# tac /etc/issue
Kernel \r on an \m
\S
```
6. **file**: determine file type
功能: 查看一个文件内容类型.
file [FILE]... 文件可以添加多个
```bash
[root@localhost man1]# file /bin/ls /bin/cd
/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
/bin/cd: POSIX shell script, ASCII text executable
```
7. **echo**: display a line of text
功能: 回显一行文本内容
echo [SHORT-OPTION]... [STRING]...
参数说明:
-n : 不再显示新的行数, 默认自带换行
-e : 让转义字符生效, 默认转义字符是不生效的.
STRING 后的内容可以加上引号
单引号: 表示强引用: 里面的变量不会被替换
双引号: 表示弱引用: 里面的变量会被替换
```bash
[root@localhost man1]# echo hello world
hello world
[root@localhost man1]# echo -n hello world
hello world[root@localhost man1]#
[root@localhost man1]# echo 'hello \nworld'
hello \nworld
[root@localhost man1]# echo -e 'hello \nworld'
hello
world
[root@localhost man1]# echo '$SHELL'
$SHELL
[root@localhost man1]# echo "$SHELL"
/bin/bash
```
8. **shutdown**: shutdown - Halt, power-off or reboot the machine
功能: 关机或者重启机器
shutdown [OPTIONS...] [TIME] [WALL...]
OPTIONS:
-h: halt 直接关机
-r: rebot 重启
-c: cancel 取消关机
TIME:
now 立即关机
hh:mm 绝对时间后关机
+m: 几分钟后关机
WALL:
wall 本身就是一个命令..
给所有终端发送的关机提示信息
9. 日期相关的命令:
1. data: print or set the system date and time
功能: 显示和设置时间
格式:
date [OPTION]... [+FORMAT]
显示时间
格式化的时间:
%Y: 年
%T: 相当于 %H:%M:%S
%F: 相当于 %Y-%m-%d
```bash
[root@localhost ~]# date
Mon Aug 5 17:03:58 CST 2019
[root@localhost ~]# date +%Y-%m/%d
2019-08/05
[root@localhost ~]# date +'%T'
17:04:34
[root@localhost ~]# date +%s
1564995890
```
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
设定时间
```bash
[root@localhost ~]# date 010101012019.01
# 设定为1月1号1点1分2019年, 1秒钟
Tue Jan 1 01:01:01 CST 2019
```
2. clock/hwclock: query or set the hardware clock (RTC)
功能: 显示或查询硬件时钟时间
参数:
-s: --hctosys 硬件时钟转换为系统时钟
-w: --systohc
3. cal: display a calendar
功能: 显示日历
用法: [options] [[[day] month] year]
默认显示当前月份的日历
```bash
cal 2019
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
```
10. which: shows the full path of (shell) commands.
功能: 显示完整的shell命令路径
```bash
[root@localhost man1]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
```
11. whereis: whereis - locate the binary, source, and manual page files for a command
功能: 获取一个命令的二进制文件, 源代码以及帮助文档的位置
参数:
-b: 只查找二进制文件
-m: 只查找帮助手册
-s: 只查找源代码位置
```bash
[root@localhost man1]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost man1]# whereis -mb ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost man1]# whereis -s ls
ls:[root@localhost man1]#
```
12. who: show who is logged on
功能: 显示当前用户的登录信息
SYNOPSIS:
who [OPTION]... [ FILE | ARG1 ARG2 ]
OPTIONO:
-a: 显示所有消息
-b: 系统上次启动的时间
-u: 列出于用户登录的时间
-T: 列出用户的信息状态
```bash
[root@localhost man1]# who -buT
system boot 2019-08-05 14:27
(unknown) ? :0 2019-08-05 14:27 ? 1681 (:0)
root + pts/0 2019-08-05 14:29 . 1987 (192.168.12.30)
root + pts/1 2019-08-05 15:32 00:12 3466 (192.168.12.3
```
13. w : Show who is logged on and what they are doing.
功能: 显示用户的登录状态以及他们正在干的事情
```bash
[root@localhost man1]# w
21:08:05 up 6:41, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.12.30 14:29 5.00s 1.06s 0.02s w
root pts/1 192.168.12.30 15:32 1:41 0.40s 0.02s less -s
```

浙公网安备 33010602011771号