Linux 小知识 丨目录文件常用命令 Linux 小知识

这是我参与11月更文挑战的第12天,活动详情查看:2021最后一次更文挑战

Linux 小知识 丨常用文件命令

目录常用命令

常见命令 作用
ls 列出目录
cd 切换目录
pwd 显示目前的目录
mkdir 创建新目录
rmdir 删除空目录
cp 复制文件或目录
rm 删除文件或目录
mv 移动文件或目录

ls 命令_列出目录里的内容

选项参数

  • -a : 显示所有文件或目录 (包含隐藏)
  • -d : 仅列出目录本身, 而不是列出目录内的文件数据(常用)
  • -l : 长数据传列出, 包含文件的树形与权限等等数据(常用)

常用选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bash复制代码[root@VM-8-10-centos ~]# ls -1
app
# list one file per line. Avoid '\n' with -q or -b
[root@VM-8-10-centos ~]# ls -l
total 12
drwxr-xr-x 3 root root 4096 11月 1 11:44 app

[root@VM-8-10-centos ~]# ls -a -1 # 全部显示包括隐藏的
.
..
app
.bash_history
.bash_logout
.bash_profile
.bashrc
.cache
.config

[root@VM-8-10-centos ~]# ls -al
total 132
权限 属主 属组 大小 最后一次访问时间 文件或目录名字
dr-xr-x---. 10 root root 4096 11月 4 10:48 .
dr-xr-xr-x. 19 root root 4096 11月 4 17:52 ..
drwxr-xr-x 3 root root 4096 11月 1 11:44 app
# d 表示目录
# - 表述文件

pwd 命令

输出当前目录路径

1
2
bash复制代码[root@VM-8-10-centos ~]# pwd
/root

cd 命令 [相对路径|绝对路径]_切换目录

1
2
3
4
5
6
7
8
9
10
11
bash复制代码# 相对路径
[root@VM-8-10-centos ~]# pwd
/root
[root@VM-8-10-centos ~]# cd app
[root@VM-8-10-centos app]# pwd
/root/app

# 绝对路径
[root@VM-8-10-centos app]# cd /root/app
[root@VM-8-10-centos app]# pwd
/root/app

mkdir 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bash复制代码mkdir [-p] 文件夹名字  创建目录
# 创建一个文件夹
[root@VM-8-10-centos app]# mkdir test
[root@VM-8-10-centos app]# ls -1
http
test

# 创建多级文件夹
[root@VM-8-10-centos app]# mkdir -p test2/aaa
[root@VM-8-10-centos app]# ls
http test test2
[root@VM-8-10-centos app]# cd test2
[root@VM-8-10-centos test2]# ls
aaa
[root@VM-8-10-centos test2]# cd aaa
[root@VM-8-10-centos aaa]# ls
[root@VM-8-10-centos aaa]# pwd
/root/app/test2/aaa

rmdir 命令

1
2
3
4
5
6
bash复制代码rmdir [-p] 文件夹名字   # 删除空的目录 
[root@VM-8-10-centos aaa]# cd ..
[root@VM-8-10-centos test2]# rmdir aaa
[root@VM-8-10-centos test2]# ls

rmdir -p bbb/ccc # 删除多级空目录 , 前提是 ccc 也是空的

rm 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
bash复制代码rm [选项] 文件/目录  #删除文件或目录
-i 删除前逐一询问确认
-f 即使原文件属性设为只读,也可以直接删除, 无需逐一确认
-r 将目录及一下文件逐一删除

rm -rf *

[root@VM-8-10-centos test2]# touch aaa.txt
[root@VM-8-10-centos test2]# ls -1
aaa.txt
[root@VM-8-10-centos test2]# rm aaa.txt
rm: remove regular empty file 'aaa.txt'? yes
[root@VM-8-10-centos test2]# ls

touch 命令

1
bash复制代码touch aaa.txt # 创建文件

cp 命令

1
2
3
4
5
6
7
8
9
bash复制代码# 语法: cp [选项] 数据源 目的地  文件复制
# 参数选项
-a # 此选项通常在复制目录时使用,但它保留链接,文件属性,并复制目录下的所有内容
-d # 复制时保留链接(相当于windows中的快捷方式)。
-f # 覆盖已经存在的目标文件而不给出提示
-i # 与 -f 相反,在覆盖目标文件之前给出提示,要求用户确认是否覆盖,回答"Y"时目标文件将被覆盖。
-p # 除复制文件的内容外还把修正改时间和访问权限也复制到新文件中
-r/R # 若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件
-I # 不复制文件,只是生成链接文件(创建快捷方式)

示例

1
2
bash复制代码cp aaa/* ccc #只能拷贝文件
cp -r aaa/* ccc # 拷贝全部包括文件夹

mv 命令

1
2
3
4
5
bash复制代码# 相当于windows中的剪切功能
# 语法: mv [选项] 数据源 目的地 改名 | 移动文件或文件夹
# 参数选项
-i # 若指定目录已有同名文件,则先询问是否覆盖旧文件
-f # 若指定目录已有同名文件,直接覆盖不提示

示例

1
2
bash复制代码mv a.txt b.txt # 重命名
mv a.txt /home/a.txt # 移动(剪切) a.txt 到 home 下
命令格式 运行结果
mv 文件名 文件名 将源文件名改为目标文件名
mv 文件名 目录名 将文件移动到目标目录
mv 目录名 目录名 目标目录已存在,将源目录移动到目标目录,目标目录不存在则改名
mv 目录名 文件名 出错

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%