Linux系统-基本指令(4)
文章目录
- touch 指令(2-2.25.00)
- 知识点(普通文件和文本文件)
- mkdir 指令
- 知识点(tree指令)
- rm 指令
- man 指令(3-0.00.00)
- 知识点(Linux下,一切皆为文件)
- cp 指令(3-1.15.00)
- 知识点(指令是什么?)
touch 指令(2-2.25.00)
1. touch
指令常用的功能有两个,一个是创建文件,一个是修改时间,目前就只需要关注创建文件
[root@hcss-ecs-28ce ~]# ls
dir hello test.c txt.c[root@hcss-ecs-28ce ~]# touch log.txt[root@hcss-ecs-28ce ~]# ls
dir hello log.txt test.c txt.c
2. 更新已经存在文件的时间,命令stat log.txt
查看该文件更加详细的属性信息
知识点(普通文件和文本文件)
1. 以-
开头的文件类型称作普通文件
,而文本文件
包括二进制可执行程序,动静态库,视频,音频,图片等
2. 在Linux
系统中,文件类型与文件后缀无关!命令mv a.out a.txt
,将文件名a.out
改为a.txt
,指令mv
有剪切的作用(后面介绍)
[root@hcss-ecs-28ce ~]# ls
a.out dir hello log.txt test.c txt.c[root@hcss-ecs-28ce ~]# ./a.out
hello world[root@hcss-ecs-28ce ~]# mv a.out a.txt[root@hcss-ecs-28ce ~]# ls
a.txt dir hello log.txt test.c txt.c[root@hcss-ecs-28ce ~]# ./a.txt
hello world[root@hcss-ecs-28ce ~]# mv a.txt a.jpg[root@hcss-ecs-28ce ~]# ls
a.jpg dir hello log.txt test.c txt.c[root@hcss-ecs-28ce ~]# ./a.jpg
hello world
3. 在Linux
系统中,文件类型与文件后缀无关,但不代表Linux
系统中的软件对文件后缀没有要求,建议带上后缀
[root@hcss-ecs-28ce ~]# ls
dir hello test.c
[root@hcss-ecs-28ce ~]# mv test.c test.txt
[root@hcss-ecs-28ce ~]# ls
dir hello test.txt
[root@hcss-ecs-28ce ~]# cat test.txt
#include<stdio.h>
int main()
{printf("hello world\n");return 0;
}
[root@hcss-ecs-28ce ~]# gcc test.txt
test.txt: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
mkdir 指令
1. 命令mkdir mydir
创建一个目录,该目录的名字是mydir
[root@hcss-ecs-28ce ~]# ls
dir hello test.txt
[root@hcss-ecs-28ce ~]# mkdir mydir
[root@hcss-ecs-28ce ~]# ls
dir hello mydir test.tx
2. 如果想在当前路径下去创建一连串目录,是直接输入命令mkdir a/b/c/d
吗?这样行不通,必须得带上选项p
,即mkdir -p a/b/c/d
[root@hcss-ecs-28ce ~]# ls
dir hello mydir test.txt[root@hcss-ecs-28ce ~]# mkdir a/b/c/d
mkdir: cannot create directory ‘a/b/c/d’: No such file or directory[root@hcss-ecs-28ce ~]# mkdir -p a/b/c/d[root@hcss-ecs-28ce ~]# ls
a dir hello mydir test.txt[root@hcss-ecs-28ce ~]# tree a
a
└── b└── c└── d3 directories, 0 files
知识点(tree指令)
1.tree + 指定目录
,以树形结构,展示文件和目录结构
[root@hcss-ecs-28ce ~]# tree .
.
├── a
│ └── b
│ └── c
│ └── d
├── dir
│ ├── a.out
│ └── code.c
├── hello
│ └── file.c
├── mydir
└── test.txt7 directories, 4 files
2. 如果出现信息tree: command not found
,则代表你没安装tree
指令,输入以下命令安装:yum install -y tree
3.如果指定的目录非常庞大,比如tree /
可能就会出现刷屏操作,所以在命令行中,如果出现非法或者刷屏操作,直接ctrl + c
│ │ │ ├── projid_map
│ │ │ ├── root -> /
│ │ │ ├── sched
│ │ │ ├── schedstat
│ │ │ ├── sessionid
│ │ │ ├── setgroups
│ │ │ ├── smaps
^C[root@hcss-ecs-28ce ~]#
rm 指令
1.rmdir
指令只能去删除一个空目录,不能删除一连串目录,rm
指令也是如此,如果想递归的去删除目录,必须带上选项r
[root@hcss-ecs-28ce ~]# ls
a dir hello mydir test.txt
[root@hcss-ecs-28ce ~]# rmdir mydir[root@hcss-ecs-28ce ~]# rmdir a
rmdir: failed to remove ‘a’: Directory not empty[root@hcss-ecs-28ce ~]# rm a
rm: cannot remove ‘a’: Is a directory[root@hcss-ecs-28ce ~]# tree a
a
└── b└── c└── d3 directories, 0 files[root@hcss-ecs-28ce ~]# rm -r a
rm: descend into directory ‘a’? y
rm: descend into directory ‘a/b’? y
rm: descend into directory ‘a/b/c’? y
rm: remove directory ‘a/b/c/d’? y
rm: remove directory ‘a/b/c’? y
rm: remove directory ‘a/b’? y
rm: remove directory ‘a’? y
[root@hcss-ecs-28ce ~]# ls
dir hello test.txt
2. 咱们发现要删除一连串目录时每次都会询问,那有时就是不想让它询问,直接进行递归强制删除,则需要带上选项f
,即rm -rf a
。强制删除普通文件也需带上选项-f
,即rm -f test.txt
[root@hcss-ecs-28ce ~]# ls
dir hello test.txt[root@hcss-ecs-28ce ~]# mkdir -p a/b/c/d
[root@hcss-ecs-28ce ~]# tree a
a
└── b└── c└── d3 directories, 0 files[root@hcss-ecs-28ce ~]# rm -rf a
[root@hcss-ecs-28ce ~]# ls
dir hello test.txt
3. 要特别注意这个递归强制删除指令可别随便乱用。比如rm -rf /
,如果递归删除了根目录,那基本上把大部分文件给删除了(有些文件就算是管理员也删除不了),这个系统基本上也就崩溃了,就算没崩溃那基本上也是个大坑,因为你也不知道未来的坑会出现在哪个地方
man 指令(3-0.00.00)
1. man
它是一个在线查找手册,可以用它来查找对应的指令和选项,或者去查系统调用的接口(man 2 fork
),退出的话直接输入q
,回车键(方向键)向下滑动
[root@hcss-ecs-28ce ~]# man pwd
[root@hcss-ecs-28ce ~]# man ls
[root@hcss-ecs-28ce ~]# man touch
[root@hcss-ecs-28ce ~]# man rm
2. 如果你也不知道man
能查找什么,那就查找它自己man man
,还可以用来查C语言的函数(不过咱们一般也不会在上面查,一般会通过相应的网站和离线手册)。查找的顺序是依次从上至下去查找,这也是为啥man printf(在1部分)
查出来的是指令而不是C语言函数接口(3)
[root@hcss-ecs-28ce ~]# man man
[root@hcss-ecs-28ce ~]# man 3 printf
3. 前面三个常用选项的粗略阐述,另外如果你查找不到,可能存在两个问题,第一个是你要查找的指令输错了。第二可能是你man
手册需要更新,重新安装一次就OK
- 1 是普通的命令
- 2 是系统调⽤,如
open,write
之类的(通过这个⾄少可以很⽅便的查到调⽤这个函数,需要加什么头⽂件) - 3 是库函数,如
printf,fread4
是特殊⽂件,也就是/dev
下的各种设备⽂件
4. 可能会没有安装man
手册,直接输入指令yum install -y man-pages
(仅限于超级用户root
),如果是普通用户则需要提权才能去安装
知识点(Linux下,一切皆为文件)
1.Linux
下,一切皆为文件。打印到显示器上→显示器也是文件→写入到显示器文件中。从键盘读取数据→键盘也是文件→从键盘文件读取数据
2. 往显示器文件中写入指定的内容,显示器文件的内容会打印到显示器(屏幕)上
[root@hcss-ecs-28ce ~]# echo hello world
hello world
3. echo
可以向显示器文件中写入,也可以向指定文件中写入,这种操作被称作重定向操作中的输出重定向
- 如果指定文件不存在的话,就新建文件
- 如果指定文件存在的话,就清空文件内容再写入要输入的内容
[root@hcss-ecs-28ce ~]# ls
dir hello
[root@hcss-ecs-28ce ~]# echo hello world > log.txt
[root@hcss-ecs-28ce ~]# ls
dir hello log.txt[root@hcss-ecs-28ce ~]# cat log.txt
hello world[root@hcss-ecs-28ce ~]# echo This is my world > log.txt
[root@hcss-ecs-28ce ~]# cat log.txt
This is my world
4. 那如果echo
向指定文件中写入,不想让它将文件内容清空,咋办?这里就需要用到追加重定向
[root@hcss-ecs-28ce ~]# cat log.txt
This is my world
[root@hcss-ecs-28ce ~]# echo hello world >> log.txt
[root@hcss-ecs-28ce ~]# cat log.txt
This is my world
hello world
5. 可以往指定文件中写入,也可以从指定文件中读取数据,cat < log.txt(cat log.txt)
就是读取log.txt
中的文件并写入到显示器文件中,这就是输入重定向
[root@hcss-ecs-28ce ~]# ls
dir hello log.txt[root@hcss-ecs-28ce ~]# cat log.txt
This is my world
hello world[root@hcss-ecs-28ce ~]# cat < log.txt
This is my world
hello world
6. cat
指令后面什么都不跟,默认从键盘文件读,再写入到显示器文件中,ctrl + c 或 ctrl + z
停止写入
[root@hcss-ecs-28ce ~]# cat
abcdef
abcdef
This is my world
This is my world
hello world
hello world
^C
7. 若log.txt
文件不存在,>log.txt
则新建文件log.txt
,若文件存在,则清空文件内容。ls -l > log.txt
,将原本写入到显示器中的文件写入到log.txt
文件中
[root@hcss-ecs-28ce ~]# ls
dir hello log.txt text.txt
[root@hcss-ecs-28ce ~]# cat log.txt
This is my world
hello world[root@hcss-ecs-28ce ~]# > log.txt
[root@hcss-ecs-28ce ~]# cat log.txt[root@hcss-ecs-28ce ~]# > code.c
[root@hcss-ecs-28ce ~]# ls
code.c dir hello log.txt text.txt[root@hcss-ecs-28ce ~]# ls -l > code.c
[root@hcss-ecs-28ce ~]# cat code.c
total 8
-rw-r--r-- 1 root root 0 May 31 12:04 code.c
drwxr-xr-x 2 root root 4096 May 27 21:09 dir
drwxr-xr-x 2 root root 4096 May 27 18:06 hello
-rw-r--r-- 1 root root 0 May 31 12:03 log.txt
-rw-r--r-- 1 root root 0 May 31 11:58 text.txt
8. 下面的图片中的命令操作是为了验证Linux
下,一切皆为文件。看不懂没关系
// oper_tty.c
int main()
{FILE *fp = fopen("/dev/pts/1", "w");if (fp == NULL){perror("fopen");return 1;}char buffer[1024];while (1){printf("Enter# ");scanf("%s", buffer);fputs(buffer, fp);fflush(fp);}fclose(fp);return 0;
}
cp 指令(3-1.15.00)
1. 拷贝普通文件,cp + 普通文件 + 指定路径
[root@hcss-ecs-28ce ~]# ls
dir hello[root@hcss-ecs-28ce ~]# cd hello
[root@hcss-ecs-28ce hello]# pwd
/root/hello[root@hcss-ecs-28ce hello]# ls
file.c[root@hcss-ecs-28ce hello]# cp file.c ../
[root@hcss-ecs-28ce hello]# cd ..
[root@hcss-ecs-28ce ~]# ls
dir file.c hello
2. 拷贝目录需要带上选项r
,进行递归拷贝。指定路径下可能已经存在该目录(复制过去会覆盖该目录),可能会询问你是否进行覆盖,可带上选项f
进行强制拷贝
[root@hcss-ecs-28ce hello]# ls
file.c[root@hcss-ecs-28ce hello]# mkdir -p a/b/c/d
[root@hcss-ecs-28ce hello]# ls
a file.c[root@hcss-ecs-28ce hello]# cp -rf a ../
[root@hcss-ecs-28ce hello]# cd ..[root@hcss-ecs-28ce ~]# ls
a dir hello
[root@hcss-ecs-28ce ~]# tree a
a
└── b└── c└── d3 directories, 0 files
3. 可以以指定名称进行拷贝(拷过去 + 改名称),若该指定名称与指定路径下的其中一个文件名称相同则会覆盖该文件
[root@hcss-ecs-28ce hello]# ls
file.c[root@hcss-ecs-28ce hello]# ls ..
a dir hello[root@hcss-ecs-28ce hello]# cp file.c ../log.txt
[root@hcss-ecs-28ce hello]# ls ..
a dir hello log.txt
知识点(指令是什么?)
1. 指令是什么?指令就是一个文本文件,它就是一个可执行程序
2. 通过which
指令就可以快速找到指定的命令文件所处的路径
[root@hcss-ecs-28ce /]# which mkdir
/usr/bin/mkdir
3. 指令它就是特定系统路径下的程序,只不过这个程序不是咱们写的,而是参与这个开源项目的工程师写的,再预装到这个系统中,所以可以直接执行这个程序
4. 咱们是不建议将自己写的程序或指令放到/usr/bin
路径下,会污染系统的指令池。因为系统那么多的指令,时间一久就忘记了,哎这里怎么还有这个命令?
[root@hcss-ecs-28ce ~]# gcc code.c
[root@hcss-ecs-28ce ~]# ll
total 28
drwxr-xr-x 3 root root 4096 May 31 13:51 a
-rwxr-xr-x 1 root root 8360 May 31 14:40 a.out
-rw-r--r-- 1 root root 76 May 31 14:40 code.c
drwxr-xr-x 2 root root 4096 May 27 21:09 dir
drwxr-xr-x 2 root root 4096 May 31 14:06 hello[root@hcss-ecs-28ce ~]# a.out
-bash: /usr/bin/a.out: No such file or directory[root@hcss-ecs-28ce ~]# cp a.out /usr/bin/
[root@hcss-ecs-28ce ~]# a.out
hello world[root@hcss-ecs-28ce ~]# rm -f /usr/bin/a.out
[root@hcss-ecs-28ce ~]# a.out
-bash: /usr/bin/a.out: No such file or directory
5. 上面指令中,咱们用到了命令ll
,而它的作用既然和命令ls -l
的效果是一致的,这是咋个回事呢?可以理解为命令ll
为命令ls -l
的别名
[root@hcss-ecs-28ce ~]# ll
total 28
drwxr-xr-x 3 root root 4096 May 31 13:51 a
-rwxr-xr-x 1 root root 8360 May 31 14:40 a.out
-rw-r--r-- 1 root root 76 May 31 14:40 code.c
drwxr-xr-x 2 root root 4096 May 27 21:09 dir
drwxr-xr-x 2 root root 4096 May 31 14:06 hello[root@hcss-ecs-28ce ~]# ls -l
total 28
drwxr-xr-x 3 root root 4096 May 31 13:51 a
-rwxr-xr-x 1 root root 8360 May 31 14:40 a.out
-rw-r--r-- 1 root root 76 May 31 14:40 code.c
drwxr-xr-x 2 root root 4096 May 27 21:09 dir
drwxr-xr-x 2 root root 4096 May 31 14:06 hello[root@hcss-ecs-28ce dir]# which ll
alias ll='ls -l --color=auto'/usr/bin/ls
6. 这是因为在Linux
系统中,可以对指定的命令和选项组合起别名。不建议自己随便对命令起别名,alias
译为别名,化名。当你自己对命令起别名后,退出xshell
,你起的别名就自动清除
[root@hcss-ecs-28ce dir]# ls
a.out code.c
[root@hcss-ecs-28ce dir]# alias zhangsan='ls -a -l'(等号和单引号之间不要有空格)
[root@hcss-ecs-28ce dir]# zhangsan
total 36
drwxr-xr-x 2 root root 4096 May 27 21:09 .
dr-xr-x---. 8 root root 4096 May 31 14:40 ..
-rwxr-xr-x 1 root root 8360 May 27 21:09 a.out
-rw-r--r-- 1 root root 76 May 27 21:08 code.c
-rw-r--r-- 1 root root 12288 May 27 21:19 .code.c.swp[root@hcss-ecs-28ce dir]# which zhangsan
alias zhangsan='ls -a -l'/usr/bin/ls