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 : 退出