文章目录
- Linux常用基本指令
- 1. ls
- 2. pwd
- 3. cd
- 4. touch
- 5. tree
- 6. mkdir
- 7. rmdir
- 8. rm
- 9. man
- 10. cp
- 11. mv
- 12. cat
- 13. echo
- 14. wc
- 15. more
- 16. less
- 17. head
- 18. tail
- 19. date
- 20. cal
- 21. sort
- 22. uniq
- 23. find
- 24. which
- 25. whereis
- 26. alias
- 27. grep
- 28. zip/unzip
- 29. tar
- 30. bc
- 31. uname -r
- 32. 热键
- 33. 创建私人用户
Linux常用基本指令
前言:linux不止命令,命令这是工具文件
1. ls
ls [选项] [目录或文件]
作用:列出该目录下所有的子目录与文件
常见选项:
-a 列出目录下的所有文件,包括以.开头的隐含文件
[root@VM-12-12-centos ~]# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc jyh .pip .pydistutils.cfg .ssh .tcshrc
-l 列出文件详细信息(ls -l = ll)
[root@VM-12-12-centos ~]# ls -l
total 4
drwxr-xr-x 3 root root 4096 Nov 11 16:26 jyh
-R 列出所有子目录下的文件(递归)
[root@VM-12-12-centos ~]# tree
.
`-- jyh
`-- instruction
|-- a.out
|-- README.txt
`-- test.c
2 directories, 3 files
[root@VM-12-12-centos ~]# ls -R
.:
jyh
./jyh:
instruction
./jyh/instruction:
a.out README.txt test.c
-i 输出文件的i节点的索引信息。如ls -ai 指定文件
[root@VM-12-12-centos ~]# ls -ai
393219 . 2 .. 393318 .bash_history 397151 .bash_logout 397152 .bash_profile 397153 .bashrc 393280 .cache 393313 .config 397154 .cshrc 656510 jyh 393690 .pip 394368 .pydistutils.cfg 135701 .ssh 397155 .tcshrc
-r 对目录反向排序
[root@VM-12-12-centos jyh]# ls -r
test instruction
[root@VM-12-12-centos jyh]# ls
instruction test
-n 用数字的UID,GID代替名称(Linux 系统中,每个用户的 ID 细分为 2 种,分别是用户 ID(User ID,简称 UID)和组 ID(Group ID,简称 GID))
[root@VM-12-12-centos jyh]# ls -n
total 8
drwxr-xr-x 2 0 0 4096 Nov 11 16:57 instruction
drwxr-xr-x 2 0 0 4096 Nov 11 17:26 test
[root@VM-12-12-centos jyh]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Nov 11 16:57 instruction
drwxr-xr-x 2 root root 4096 Nov 11 17:26 test
2. pwd
作用:显示用户当前所在目录
[root@VM-12-12-centos ~]# pwd
/root
建议:每一次登录,或者长时间没操作都指向此指令
3. cd
cd 目录名
作用:改变工作目录
.表示当前路径,…表示上级路径
cd … :返回上级目录
[root@VM-12-12-centos ~]# cd jyh
[root@VM-12-12-centos jyh]# pwd
/root/jyh
[root@VM-12-12-centos jyh]# cd test
[root@VM-12-12-centos test]# pwd
/root/jyh/test
[root@VM-12-12-centos test]# cd ..
[root@VM-12-12-centos jyh]# pwd
/root/jyh
cd /root/jyh/linux/ : 绝对路径(/表示Linux的路径分隔符)
[root@VM-12-12-centos ~]# ls -R
.:
jyh
./jyh:
instruction test
./jyh/instruction:
a.out README.txt test.c
./jyh/test:
test.txt
[root@VM-12-12-centos ~]# pwd
/root
[root@VM-12-12-centos ~]# cd jyh/test/
[root@VM-12-12-centos test]# pwd/root/jyh/test
cd …/day01/ : 相对路径
[root@VM-12-12-centos ~]# ls -R
.:
jyh
./jyh:
instruction test
./jyh/instruction:
a.out README.txt test.c
./jyh/test:
test.txt
[root@VM-12-12-centos ~]# pwd
/root
[root@VM-12-12-centos ~]# cd jyh/instruction/
[root@VM-12-12-centos instruction]# pwd
/root/jyh/instruction
[root@VM-12-12-centos instruction]# cd ../test/
[root@VM-12-12-centos test]# pwd
/root/jyh/test
cd ~ : 进入用户家目录
[root@VM-12-12-centos ~]# ls -R
.:
jyh
./jyh:
instruction test
./jyh/instruction:
a.out README.txt test.c
./jyh/test:
test.txt
[root@VM-12-12-centos ~]# cd jyh/test/
[root@VM-12-12-centos test]# pwd
/root/jyh/test
[root@VM-12-12-centos test]# cd ~
[root@VM-12-12-centos ~]# pwd
/root
cd - :返回最近访问目录
[root@VM-12-12-centos ~]# pwd
/root
[root@VM-12-12-centos ~]# ls -R
.:
jyh
./jyh:
instruction test
./jyh/instruction:
a.out README.txt test.c
./jyh/test:
test.txt
[root@VM-12-12-centos ~]# cd jyh/instruction/
[root@VM-12-12-centos instruction]# pwd
/root/jyh/instruction
[root@VM-12-12-centos instruction]# cd ../test/
[root@VM-12-12-centos test]# pwd
/root/jyh/test
[root@VM-12-12-centos test]# cd -
/root/jyh/instruction
[root@VM-12-12-centos instruction]# pwd
/root/jyh/instruction
补充:/ : 表示linux的根目录
[root@VM-12-12-centos /]# pwd
/
4. touch
touch [选项]
作用:在当前路径下创建一个普通文件
5. tree
作用:以树状结构显示Linux执行的目录结构(当没有此指令时,在root用户下,输入yum install -y tree
[root@VM-12-12-centos ~]# pwd
/root
[root@VM-12-12-centos ~]# whoami
root
[root@VM-12-12-centos ~]# tree
.
`-- jyh
|-- instruction
| |-- a.out
| |-- README.txt
| `-- test.c
|-- study_2022_11_11
`-- test
`-- test.txt
4 directories, 4 files
6. mkdir
mkdir [选项] 目录名
功能:在当前目录下创建一个目录
mkdir -p test/test1/ :递归建立多个目录
7. rmdir
rmdir [选项] [文件名]
作用:删除空目录
8. rm
rm [选项] [目录/文件名]
作用:删除文件或目录
-f : 强制删除
-r : 删除目录及其下所有文件
9. man
man [选项] 命令
作用:查看联机手册
没有此指令可以直接指向该指令下载:
yum install -y man-pages
手册分为八册:
- 普通命令
- 系统调用
- 库函数
- 特殊文件
- 文件的格式和约定
- 游戏
- 杂项(包括宏包和约定)
- 系统管理命令
- 内核例程[非标准]
man 1 printf(在1号手册中查找printf指令)
10. cp
cp [选项] 源文件或目录 目标文件或目录
作用:复制文件或目录
//操作:把instruction1目录中的cp.txt复制到mydir目录中
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# tree
.
|-- cp.txt
`-- mydir
1 directory, 1 file
[root@VM-12-12-centos instruction1]# cp cp.txt mydir/
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# tree
.
|-- cp.txt
`-- mydir
`-- cp.txt
1 directory, 2 files
-f 或 --force 强行复制文件或目录
-r 递归处理 将指定目录下的文件与子目录一并处理
//操作:把当前目录下的文件复制到上级目录
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# cd ..
[root@VM-12-12-centos study_2022_11_11]# ll
total 8
drwxr-xr-x 3 root root 4096 Nov 16 21:05 instruction1
-rw-r--r-- 1 root root 53 Nov 12 16:58 README.txt
[root@VM-12-12-centos study_2022_11_11]# cd instruction1/
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# cp cp.txt ..
[root@VM-12-12-centos instruction1]# cd ..
[root@VM-12-12-centos study_2022_11_11]# ll
total 8
-rw-r--r-- 1 root root 0 Nov 16 21:11 cp.txt
drwxr-xr-x 3 root root 4096 Nov 16 21:05 instruction1
-rw-r--r-- 1 root root 53 Nov 12 16:58 README.txt
//把当前目录下的目录复制到上级目录中
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# tree
.
|-- cp.txt
`-- mydir
`-- cp.txt
1 directory, 2 files
[root@VM-12-12-centos instruction1]# cd ..
[root@VM-12-12-centos study_2022_11_11]# ll
total 8
drwxr-xr-x 3 root root 4096 Nov 16 21:05 instruction1
-rw-r--r-- 1 root root 53 Nov 12 16:58 README.txt
[root@VM-12-12-centos study_2022_11_11]# cd instruction1/
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# tree
.
|-- cp.txt
`-- mydir
`-- cp.txt
1 directory, 2 files
[root@VM-12-12-centos instruction1]# cp mydir ..
cp: omitting directory ‘mydir’ //报错:省略目录
[root@VM-12-12-centos instruction1]# cp -r mydir/
cp: missing destination file operand after ‘mydir/’ //报错:没有目标文件
Try 'cp --help' for more information.
[root@VM-12-12-centos instruction1]# cp -r mydir ..
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 15 20:19 cp.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# cd ..
[root@VM-12-12-centos study_2022_11_11]# ll
total 12
drwxr-xr-x 3 root root 4096 Nov 16 21:05 instruction1
drwxr-xr-x 2 root root 4096 Nov 16 21:16 mydir
-rw-r--r-- 1 root root 53 Nov 12 16:58 README.txt
11. mv
mv [选项] 源文件或目录 目标文件或目录
-f : 强制剪切
作用一:剪切目录或文件
//剪切文件和目录到上级目录下
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 16 21:24 mv.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# ls .. //上级目录
instruction1 README.txt
[root@VM-12-12-centos instruction1]# mv mv.txt
mv: missing destination file operand after ‘mv.txt’ //报错:没有目标文件或目录
Try 'mv --help' for more information.
[root@VM-12-12-centos instruction1]# mv mv.txt ..
[root@VM-12-12-centos instruction1]# ls ..
instruction1 mv.txt README.txt
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# mv mydir/ .. //默认递归剪切
[root@VM-12-12-centos instruction1]# ls ..
instruction1 mv.txt mydir README.txt
[root@VM-12-12-centos instruction1]# ll
total 0
[root@VM-12-12-centos instruction1]# cd .. //回到上级目录
[root@VM-12-12-centos study_2022_11_11]# tree
.
|-- instruction1
|-- mv.txt
|-- mydir
| `-- cp.txt
`-- README.txt
作用二:剪切自身且目标文件在当前目录中不存在的时候就是重命名
//重命名文件和目录
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 0 Nov 16 21:24 mv.txt
drwxr-xr-x 2 root root 4096 Nov 16 21:06 mydir
[root@VM-12-12-centos instruction1]# mv mydir dir
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 16 21:06 dir
-rw-r--r-- 1 root root 0 Nov 16 21:24 mv.txt
[root@VM-12-12-centos instruction1]# mv mv.txt mymv.txt
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 16 21:06 dir
-rw-r--r-- 1 root root 0 Nov 16 21:24 mymv.txt
[root@VM-12-12-centos instruction1]# tree
.
|-- dir
| `-- cp.txt
`-- mymv.txt
1 directory, 2 files
12. cat
cat [选项] [文件]
-n : 对输出的所有行进行编号
作用:查看目标文件的内容
[root@VM-12-12-centos instruction1]# nano myfile.txt //编辑
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 31 Nov 16 21:35 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
这是我要展示的内容!
[root@VM-12-12-centos instruction1]# cat -n myfile.txt
1 这是我要展示的内容!
13. echo
echo 内容
作用:在显示器上显示内容(用于查看短文本)
-e : 打印出转义字符
-n : 去掉echo末尾的换行符
[root@VM-12-12-centos instruction1]# clear
[root@VM-12-12-centos instruction1]# echo "hello Linux"
hello Linux
[root@VM-12-12-centos instruction1]# echo "hello Linux\n"
hello Linux\n
[root@VM-12-12-centos instruction1]# echo -e "hello Linux\n"
hello Linux
[root@VM-12-12-centos instruction1]#
[root@VM-12-12-centos instruction1]# echo -e -n "hello Linux\n"
hello Linux
[root@VM-12-12-centos instruction1]#
补充:> :输出重定向(覆盖式写入)
>文件名:清空文件内容
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 31 Nov 16 21:35 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
这是我要展示的内容!
[root@VM-12-12-centos instruction1]# echo "hello Linux" > myfile.txt
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 12 Nov 16 21:49 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
hello Linux
[root@VM-12-12-centos instruction1]#
怎么去理解这个输出重定向呢?
//现象一:覆盖式写入(写入之前会先清空文件)
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 12 Nov 16 21:49 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
hello Linux
[root@VM-12-12-centos instruction1]# echo "hello" > myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
hello
[root@VM-12-12-centos instruction1]# echo "qqqweef" > myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
[root@VM-12-12-centos instruction1]#
//现象二:追加
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 16 21:59 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
[root@VM-12-12-centos instruction1]# echo "hello" >> myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
hello
[root@VM-12-12-centos instruction1]# echo "hello" >> myfile.txt
[root@VM-12-12-centos instruction1]# echo "hello" >> myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
hello
hello
hello
[root@VM-12-12-centos instruction1]#
补充: >> :追加重定向
补充: < :输入重定向
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 16 22:02 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
hello
hello
hello
[root@VM-12-12-centos instruction1]# cat < myfile.txt //把文件内容读到cat打印
qqqweef
hello
hello
hello
14. wc
作用:计算字数
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 16 22:02 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
qqqweef
hello
hello
hello
[root@VM-12-12-centos instruction1]# wc -l myfile.txt
4 myfile.txt
[root@VM-12-12-centos instruction1]#
-c或–bytes 或–chars 只显示bytes数
-l或–lines 显示行数
-w或–words 只显示字数
–version 显示版本信息
15. more
more [选项] [文件]
作用:查看长文本内容
-n : 对输出的所有行编号
q : 退出more
//查看一下myfile.txt大文本
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 16 22:02 myfile.txt
[root@VM-12-12-centos instruction1]# pwd
/root/study_2022_11_11/instruction1
[root@VM-12-12-centos instruction1]# cnt=0; while [ $cnt -le 100 ]; do echo "hello $cnt"; let cnt++; done > myfile.txt
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
hello 11
hello 12
hello 13
hello 14
hello 15
hello 16
hello 17
hello 18
hello 19
hello 20
hello 21
hello 22
hello 23
hello 24
hello 25
hello 26
hello 27
hello 28
hello 29
hello 30
hello 31
hello 32
hello 33
hello 34
hello 35
hello 36
hello 37
hello 38
hello 39
hello 40
hello 41
hello 42
hello 43
hello 44
hello 45
hello 46
hello 47
hello 48
hello 49
hello 50
hello 51
hello 52
hello 53
hello 54
hello 55
hello 56
hello 57
hello 58
hello 59
hello 60
hello 61
hello 62
hello 63
hello 64
hello 65
hello 66
hello 67
hello 68
hello 69
hello 70
hello 71
hello 72
hello 73
hello 74
hello 75
hello 76
hello 77
hello 78
hello 79
hello 80
hello 81
hello 82
hello 83
hello 84
hello 85
hello 86
hello 87
hello 88
hello 89
hello 90
hello 91
hello 92
hello 93
hello 94
hello 95
hello 96
hello 97
hello 98
hello 99
hello 100
[root@VM-12-12-centos instruction1]# more myfile.txt
16. less
less [选项] 文件
作用:查看大文本内容
- 可以使用 pageup和pagedown等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!
- less 工具也是对文件或其它输出进行分页显示的工具
/字符串:向下搜索“字符串”
?字符串:向上搜索”字符串“
q : 退出
17. head
head [选项] [文件]
作用:用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行
-n<行数> 显示的行数
//原本myfile.txt文件中有一百行,head默认显示前十行
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# head myfile.txt
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[root@VM-12-12-centos instruction1]#
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# head -n3 myfile.txt
hello 0
hello 1
hello 2
[root@VM-12-12-centos instruction1]#
18. tail
tail [必要选项] [选择选项] [文件]
作用: 用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件
-f : 循环读取
-n<行数> 显示行数
//myfile.txt文件中有100行hello
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# tail myfile.txt
hello 91
hello 92
hello 93
hello 94
hello 95
hello 96
hello 97
hello 98
hello 99
hello 100
//默认是文本最后十行
补充:| : 此符号表示命令行管道
root@VM-12-12-centos instruction1]# clear
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt | wc -l
101
[root@VM-12-12-centos instruction1]#
怎么理解管道呢?
//管道用途:显示大文本中的中间内容或任意想提取的内容
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# wc -l myfile.txt
101 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt | head -n50 | tail -n10
hello 40
hello 41
hello 42
hello 43
hello 44
hello 45
hello 46
hello 47
hello 48
hello 49
[root@VM-12-12-centos instruction1]#
19. date
data [选项] [+format]
作用:显示时间
- %H : 小时
- %M : 分钟
- %S : 秒
- %X : 相当于 %H:%M:%S
- %d : 日
- %m : 月份
- %Y : 完整年份
- %F :相当于%Y-%m-%d
[root@VM-12-12-centos instruction1]# date +%F/%X
2022-11-18/12:00:31 AM
补充:时间戳
时间->时间戳:date +%s
时间戳->时间:date -d@时间戳
[root@VM-12-12-centos ~]# date +%s
1668781140
[root@VM-12-12-centos ~]# date -d@1668781140
Fri Nov 18 22:19:00 CST 2022
[root@VM-12-12-centos ~]#
20. cal
cal [参数] [月份] [年份]
作用:用于查看日历等时间信息
-y 显示当前年份的日历
-j 显示当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
[root@VM-12-12-centos ~]# cal -y 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
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
[root@VM-12-12-centos ~]#
21. sort
sort [选项] 文件名
作用:按照每行的第一个字母依次向后对齐比较ASCII码值(类似strcmp函数),默认升序
-r 按照降序排序
[root@VM-12-12-centos instruction1]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 18 22:26 test.txt
[root@VM-12-12-centos instruction1]# nano test.txt
[root@VM-12-12-centos instruction1]# cat test.txt
1111
111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]# sort test.txt
111
1111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]#
22. uniq
作用:相邻内容去重
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 40 Nov 18 22:29 test.txt
[root@VM-12-12-centos instruction1]# nano test.txt
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# clear
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# cat test.txt
1111
111
2222
22222
3333
4444
7777
9999
9999
0000
0000
1111
[root@VM-12-12-centos instruction1]# uniq test.txt
1111
111
2222
22222
3333
4444
7777
9999
0000
1111
[root@VM-12-12-centos instruction1]# sort test.txt | uniq
0000
111
1111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]#
23. find
find pathname -options filename
作用:用于在文件树中查看文件
-name 按照文件名查找文件
[root@VM-12-12-centos ~]# tree
.
`-- study_2022_11_11
|-- instruction1
| `-- test.txt
`-- README.txt
2 directories, 2 files
[root@VM-12-12-centos ~]# cd study_2022_11_11/instruction1/
[root@VM-12-12-centos instruction1]# pwd
/root/study_2022_11_11/instruction1
[root@VM-12-12-centos instruction1]# find ~ -name test.txt
/root/study_2022_11_11/instruction1/test.txt
[root@VM-12-12-centos instruction1]#
24. which
作用:搜索指令路径
[root@VM-12-12-centos instruction1]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@VM-12-12-centos instruction1]# which man
/usr/bin/man
[root@VM-12-12-centos instruction1]# which touch
/usr/bin/touch
[root@VM-12-12-centos instruction1]# which echo
/usr/bin/echo
[root@VM-12-12-centos instruction1]#
25. whereis
作用:查找文件
[root@VM-12-12-centos instruction1]# whereis test
test: /usr/bin/test /usr/share/man/man1/test.1.gz /usr/share/man/man1p/test.1p.gz
[root@VM-12-12-centos instruction1]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
26. alias
作用:给特定命令起别名
[root@VM-12-12-centos instruction1]# alias myls='ls -a -l -i -n'
[root@VM-12-12-centos instruction1]# myls
total 12
656403 drwxr-xr-x 2 0 0 4096 Nov 18 22:45 .
656417 drwxr-xr-x 3 0 0 4096 Nov 16 21:29 ..
656463 -rw-r--r-- 1 0 0 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# which myls
alias myls='ls -a -l -i -n'
/usr/bin/ls
[root@VM-12-12-centos instruction1]#
27. grep
grep [选项] 搜寻字符串 文件
作用:在文件中搜索字符串,将找到的行打印出来
-n 输出行号
-v 反向选择
-i 忽略大小写
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# cat test.txt
1111
111
2222
22222
3333
4444
7777
9999
9999
0000
0000
1111
[root@VM-12-12-centos instruction1]# grep '11' test.txt
1111
111
1111
[root@VM-12-12-centos instruction1]# grep -v '11' test.txt
2222
22222
3333
4444
7777
9999
9999
0000
0000
[root@VM-12-12-centos instruction1]#
28. zip/unzip
zip 压缩文件.zip 目录或文件
作用:将目录或文件压缩成zip格式
-r 递 归处理,将指定目录下的所有文件和子目录一并处理
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 18 23:02 test
[root@VM-12-12-centos instruction1]# zip test.zip .
zip error: Nothing to do! (test.zip) //err operation
[root@VM-12-12-centos instruction1]# zip test.zip test
adding: test/ (stored 0%)
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:02 test
-rw-r--r-- 1 root root 160 Nov 18 23:03 test.zip
[root@VM-12-12-centos instruction1]# tree
.
|-- test
| `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos instruction1]#
//打包目录需要递归打包
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
[root@VM-12-12-centos instruction1]# zip -r test.zip test
adding: test/ (stored 0%)
adding: test/test.txt (deflated 23%)
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# tree
.
|-- test
| `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# mkdir myfile
[root@VM-12-12-centos instruction1]# ll
total 12
drwxr-xr-x 2 root root 4096 Nov 18 23:11 myfile
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# mv test.zip myfile
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:11 myfile
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
[root@VM-12-12-centos instruction1]# tree
.
|-- myfile
| `-- test.zip
`-- test
`-- test.txt
2 directories, 2 files
[root@VM-12-12-centos instruction1]# cd myfile/
[root@VM-12-12-centos myfile]# ll
total 4
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos myfile]# unzip test.zip
Archive: test.zip
creating: test/
inflating: test/test.txt
[root@VM-12-12-centos myfile]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos myfile]# tree
.
|-- test
| `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos myfile]#
//递归处理后就拿到了目录中的文件
补充:
解压到tmp目录:unzip test2.zip -d /tmp
29. tar
tar [-cxtzjvf] 文件与目录
-c : 创建一个压缩文件
-x : 解开一个压缩文件
-t : 查看tarfile里面文件
-z : 是否同时具有gzip的属性以及是否同时需要用gzip压缩
-f : 使用档名,在f之后立即接档名(建立f选项放在所有选项后)
-v : 压缩的过程中显示文件
-C : 解压到指定目录(和zip中的-d选项一样作用)
[yinhan@VM-12-12-centos trunk]$ ll
total 4
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
[yinhan@VM-12-12-centos trunk]$ tar -czf tar.tgz tar
[yinhan@VM-12-12-centos trunk]$ ll=
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan 183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tree
.
|-- tar
| |-- file.txt
| |-- test.c
| `-- tmp
`-- tar.tgz
2 directories, 3 files
[yinhan@VM-12-12-centos trunk]$ rm -rf tar
[yinhan@VM-12-12-centos trunk]$ ll
total 4
-rw-rw-r-- 1 yinhan yinhan 183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tar -xzf tar.tgz
[yinhan@VM-12-12-centos trunk]$ ll
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan 183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tre
-bash: tre: command not found
[yinhan@VM-12-12-centos trunk]$ tree
.
|-- tar
| |-- file.txt
| |-- test.c
| `-- tmp
`-- tar.tgz
2 directories, 3 files
[yinhan@VM-12-12-centos trunk]$
固定格式就是这样压缩用-czf,解压用-xzf
//查看压缩包内容操作
[yinhan@VM-12-12-centos ~]$ ll
total 8
-rw-rw-r-- 1 yinhan yinhan 50 Nov 23 00:26 README.txt
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:53 trunk
[yinhan@VM-12-12-centos ~]$ tree
.
|-- README.txt
`-- trunk
|-- tar
| |-- file.txt
| |-- test.c
| `-- tmp
`-- tar.tgz
3 directories, 4 files
[yinhan@VM-12-12-centos ~]$ cd trunk/
[yinhan@VM-12-12-centos trunk]$ ll
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan 183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tar -tf tar.tgz
tar/
tar/test.c
tar/file.txt
tar/tmp/
[yinhan@VM-12-12-centos trunk]$ tar -tzvf tar.tgz
drwxrwxr-x yinhan/yinhan 0 2022-11-23 10:51 tar/
-rw-rw-r-- yinhan/yinhan 0 2022-11-23 10:51 tar/test.c
-rw-rw-r-- yinhan/yinhan 0 2022-11-23 10:51 tar/file.txt
drwxrwxr-x yinhan/yinhan 0 2022-11-23 10:51 tar/tmp/
30. bc
作用:进行运算
[yinhan@VM-12-12-centos trunk]$ echo "1+3+8" | bc
12
31. uname -r
uname [选项]
作用:uname用来获取电脑和操作系统的相关信息
-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类
型,硬件平台类型,操作系统名称
[yinhan@VM-12-12-centos trunk]$ uname -a
Linux VM-12-12-centos 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[yinhan@VM-12-12-centos trunk]$
32. 热键
ctrl + c : 强制终止非正常输入
tab : 补全文件或指令
ctrl + d : 退出的意思
向上或向下键:用来查阅上次或下次的指令
ctrl + insert : 复制
shift + insert : 粘贴
ctrl + r : 在历史指令中搜索
history : 列出所有历史指令
33. 创建私人用户
adduser + 用户 : 添加用户到home目录
passwd + 用户 : 设置密码 (这里设置密码是没有回显的)
userdel -r + 用户 : 删除用户