---
url: /diary/linux/2/index.md
description: Linux 基础学习笔记第二篇，文件目录操作命令，包括 pwd、ls、cd、mkdir、rmdir、touch、cp、rm、mv 等。
---
## 八.文件目录类

### 1.pwd 指令

功能描述：显示当前工作目录的绝对路径

#### 应用实例

```shell
#显示当前工作目录的绝对路径
[root@jhoncc 公共]# pwd
/root/公共
```

### 2.ls 指令

#### 基本语法

```shell
ls [选项] [目录或是文件]
```

#### 常用选项

```shell
-a：显示当前目录所有的文件和目录，包括隐藏的。
-l：以列表的方式显示信息
```

#### 应用实例

```shell
[root@jhoncc ~]# ls -i
33574987 anaconda-ks.cfg       52048233 公共  52048234 视频     78391 文档  17935240 音乐
33575001 initial-setup-ks.cfg  33574991 模板  33574994 图片  17935239 下载     78390 桌面
```

结果

```shell
[root@jhoncc ~]# ls -l
总用量 8
-rw-------. 1 root root 1547 5月  10 2020 anaconda-ks.cfg
-rw-r--r--. 1 root root 1560 5月   9 22:38 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 5月   9 22:40 公共
drwxr-xr-x. 2 root root    6 5月   9 22:40 模板
drwxr-xr-x. 2 root root    6 5月   9 22:40 视频
drwxr-xr-x. 2 root root    6 5月   9 22:40 图片
drwxr-xr-x. 2 root root    6 5月   9 22:40 文档
drwxr-xr-x. 2 root root    6 5月   9 22:40 下载
drwxr-xr-x. 2 root root    6 5月   9 22:40 音乐
drwxr-xr-x. 2 root root    6 5月   9 22:40 桌面
```

### 3.cd 指令

#### 基本语法

cd \[参数]

##### 参数

cd ~ 回到自己的家目录

cd .. 回到当前目录的上级目录

### 4.mkdir 指令

> 创建目录

#### 基本语法

mkdir \[选项] 要创建的目录

#### 常用选项

-p：创建多级目录

### 5.rmdir 指令

> 删除空目录

#### 基本语法

rmdir \[选项] 要删除的空目录

如果不是空目录，则要使用

```shell
rm -rf 目录
```

### 6.touch 指令

> 创建空文件

#### 基本语法

```shell
touch 文件名称 文件名称 文件名称
```

#### 应用实例

```shell
[root@jhoncc eg]# touch 1.txt 2.txt 3.txt
[root@jhoncc eg]# ls
1.txt  2.txt  3.txt
```

### 7.cp 指令

> 拷贝文件到指定目录

#### 基本语法

```shell
cp [选项] source dest
```

#### 常用选项

> -r 递归复制整个文件夹

#### 应用实例

```shell
[root@jhoncc ~]# cp -r eg mc
[root@jhoncc ~]# cd mc
[root@jhoncc mc]# ls
eg
[root@jhoncc mc]# cd eg/
[root@jhoncc eg]# ls
1.txt  2.txt  3.txt
```

#### 强制覆盖

`\cp`

```shell
[root@jhoncc ~]#\cp -r eg mc
```

### 8.rm 指令

> 移除文件或目录

#### 基本语法

rm \[选项] 要删除的文件或目录

#### 常用选项

-r ：递归删除整个文件夹

-f ：强制删除不提示

#### 应用实例

```shell
[root@jhoncc ~]# rm -r eg
rm：是否进入目录"eg"? yes
rm：是否删除普通空文件 "eg/1.txt"？yes
rm：是否删除普通空文件 "eg/2.txt"？yes
rm：是否删除普通空文件 "eg/3.txt"？yes
rm：是否删除目录 "eg"？yes
[root@jhoncc ~]# ls eg
ls: 无法访问eg: 没有那个文件或目录
```

```shell
[root@jhoncc ~]# rm -rf eg
[root@jhoncc ~]# ls eg
ls: 无法访问eg: 没有那个文件或目录
```

### 9.mv 指令

> 移动文件与目录或重命名

#### 基本语法

##### 重命名

```shell
mv oldFileName newFileName
```

##### 移动文件

```shell
mv source dest
```

#### 应用实例

##### 重命名

```shell
[root@jhoncc mc]# ls
eg
[root@jhoncc mc]# mv eg aaa
[root@jhoncc mc]# ls
aaa
```

##### 移动文件

```shell
[root@jhoncc mc]# mv aaa /root/mc/bbb
[root@jhoncc mc]# ls
bbb
```

### 10.cat 指令

> 查看文件的内容（只读的方式，无法修改）

#### 基本语法

cat \[选项] 要查看的文件

#### 常用选项

* `-n` ：显示行号
* `| more` ：分页显示 按空格建换页
* `-f` ：强制删除不提示

#### 应用实例

```shell
[root@jhoncc etc]# cat numad.conf
# Config file for numad
#
# Default INTERVAL is 15
# modify below to change it
INTERVAL=15
```

```shell
[root@jhoncc etc]# cat -n numad.conf
     1  # Config file for numad
     2  #
     3  # Default INTERVAL is 15
     4  # modify below to change it
     5  INTERVAL=15
```

```shell
[root@jhoncc etc]# cat ntp.conf | more
#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
--More--
```

### 11.more指令

> more 指令是一个基于vi编辑器的文本过滤器，它以全屏幕的方式按页显示文本文件的内容。
>
> more指令中内置了若干快捷键。

#### 基本用法

```shell
more 要查看的文件
```

#### 应用实例

```shell
[root@jhoncc ~]# more
用法：more [选项] 文件...

选项：
  -d        显示帮助，而不是响铃
  -f        统计逻辑行数而不是屏幕行数
  -l        抑制换页(form feed)后的暂停
  -p        不滚屏，清屏并显示文本
  -c        不滚屏，显示文本并清理行尾
  -u        抑制下划线
  -s        将多个空行压缩为一行
  -NUM      指定每屏显示的行数为 NUM
  +NUM      从文件第 NUM 行开始显示
  +/STRING  从匹配搜索字符串 STRING 的文件位置开始显示
  -V        输出版本信息并退出
```

### 12.less指令

> less 指令用来分屏查看文件内容，它的功能与 more 指令相似，但是比 more 更强大，支持各种显示终端。 less
> 指令在显示文件内容时，并不是一次将整个文件加载之后才显示，而是根据显示需要加载内容，对于显示大文件具有较高的效率。

#### 基本语法

```shell
less 要查看的文件
```

#### 应用实例

```shell
less /opt/骨王.txt
```

### 13.`>`输出重定向和`>>`追加指令

#### 基本语法：

| 语法           | 功能                    |
|--------------|-----------------------|
| ls -l>文件     | 列表的内容写入文件 a.txt （覆盖写） |
| ls -al>>文件   | 列表的内容追加到文件 aa.txt的末尾  |
| cat 文件1>文件2  | 将文件1 的内容覆盖到 文件2       |
| echo “内容”>文件 | 覆盖写入                  |

### 14.echo 指令

> 输出内容到控制台

#### 基本语法：

echo \[选项] \[输出内容]

#### 应用实例：

使用echo 指令输出环境变量：

```shell
[root@jhoncc ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
```

使用echo 指令输出 hello,world！

```shell
[root@jhoncc ~]# echo hello,world
hello,world
```

### 15.head 指令

> head 用于显示文件的开头部分内容，默认情况下 head 指令 显示文件的前10行内容。

#### 基本语法

head 文件 （查看文件头10行内容）

head -n 5 文件 （查看文件头5行内容）

#### 应用实例

```shell
[root@jhoncc ~]# head -n 5 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/shellrcshell

```

### 16.tail 指令

> tail 用户输出文件尾部的内容，默认情况下 tail 指令显示文件的后10行内容。

#### 基本语法

* tail 文件 （查看文件后10行内容）
* tail -n 5 文件 （查看文件后5行内容）
* tail -f 文件 （实时追踪该文档的所有更新）！重要

#### 应用实例

```shell
[root@jhoncc ~]# tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
```

### 17.ln 指令

> 软连接也叫符号链接，类似于windows里的快捷方式，主要存放了链接其他文件的路劲。

#### 基本语法

ln -s \[源文件或目录] \[软连接名] （给源文件创建一个软连接）

#### 应用实例：

```shell
[root@jhoncc ~]# ln -s /root linkToRoot
[root@jhoncc ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  mc    模板  图片  下载  桌面
a.txt            linkToRoot            公共  视频  文档  音乐

[root@jhoncc ~]# cd linkToRoot/
[root@jhoncc linkToRoot]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  mc    模板  图片  下载  桌面
a.txt            linkToRoot            公共  视频  文档  音乐

[root@jhoncc linkToRoot]# pwd
/root/linkToRoot
```

### 18.history 指令

> 查看已经执行过的历史命令，也可以执行历史指令

#### 基本语法

history

#### 应用实例

##### 显示所有历史命令

```shell
[root@jhoncc ~]# history
    1  ifconfig
    2  reboot
    3  reboot
    4  clear
    5  main
    6  clear
    ......
```

##### 显示最近使用过的5个指令

```shell
[root@jhoncc ~]# history 10
   86  ls
   87  rm linkToRoot
   88  ls
   89  cd ..
   90  ls
   91  cd root/
   92  ls
   93  history
   94  ! 93
   95  history 10
```

##### 执行历史编号为88的指令

```shell
[root@jhoncc ~]# !88
ls
anaconda-ks.cfg  a.txt  initial-setup-ks.cfg  mc  公共  模板  视频  图片  文档  下载  音乐  桌面
```
