目录操作 #

一、创建目录 #

bash
#!/bin/bash

# 创建目录
mkdir directory

# 创建多级目录
mkdir -p /path/to/directory

# 创建并设置权限
mkdir -m 755 directory

二、删除目录 #

bash
#!/bin/bash

# 删除空目录
rmdir directory

# 删除非空目录
rm -rf directory

三、目录遍历 #

bash
#!/bin/bash

# 遍历目录
for item in /path/*; do
    echo "$item"
done

# 递归遍历
find /path -type f

# 使用globstar(Bash 4.0+)
shopt -s globstar
for file in /path/**/*.txt; do
    echo "$file"
done

四、目录信息 #

bash
#!/bin/bash

# 当前目录
pwd

# 切换目录
cd /path/to/directory

# 目录大小
du -sh directory

# 目录树
tree directory

下一步 #

你已经掌握了目录操作,接下来让我们学习 grep命令

最后更新:2026-03-27