当前位置: 首页 > news >正文

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

在这里插入图片描述

http://www.xdnf.cn/news/746695.html

相关文章:

  • 评标专家随机抽选系统-建设方案——仙盟创梦IDE
  • WEB3——简易NFT铸造平台之nft.storage
  • 【知识点进阶】
  • Java 中 Redis 过期策略深度解析(含拓展-redis内存淘汰策略列举)
  • TI MSPM0G3507 简易PID项目显示和按键控制
  • [SLAM自救笔记0]:开端
  • 安装win11之后,电脑经常会跳出“无法在此设备上加载驱动程序”的提示。无法加载的驱动程序分别为“pcdsrvc_x64.pkms”“iqvw64e.sys”
  • OpenHarmony标准系统-HDF框架之音频驱动开发
  • 2.2HarmonyOS NEXT高性能开发技术:编译优化、内存管理与并发编程实践
  • Spring Cache核心原理与快速入门指南
  • Leetcode 1908. Nim 游戏 II
  • 【shell】让 CPU 运行到满负荷状态
  • 传统液晶瓶颈待破?铁电液晶如何实现显示技术逆袭
  • 快速掌握 GO 之 RabbitMQ
  • 嵌入式编译工具链熟悉与游戏移植
  • Python训练第四十天
  • Jmeter requests
  • LLMs之Tool:Workflow Use的简介、特点、安装和使用方法、以及案例应用
  • c++ typeid运算符
  • 如何打包conda环境从一台电脑到另外一台电脑
  • 电力高空作业安全检测(3)RT-DETR模型
  • MySQL高级查询技巧:分组、聚合、子查询与分页【MySQL系列】
  • 深入理解CSS常规流布局
  • 【系统架构设计师】第一章 计算机硬件 1.1 计算机硬件 - CPU - 校验码
  • Unity 模拟高度尺系统开发详解——实现拖动、范围限制、碰撞吸附与本地坐标轴选择
  • Linux基本指令/下
  • 信息安全之为什么引入公钥密码
  • Linux系统下安装配置 Nginx
  • AUTOSAR图解==>AUTOSAR_EXP_AIADASAndVMC
  • 数组题解——最大子数组和​【LeetCode】