万字肝货 | 超全总结,Linux常用磁盘命令、文件命令!(四)

简介: 万字肝货 | 超全总结,Linux常用磁盘命令、文件命令!(四)

9、文件命令

1)mkdir:创建目录

-p:递归创建目录

操作如下:


[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
[root@image_boundary Desktop]# mkdir cc
[root@image_boundary Desktop]# ll
total 12
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:21 cc
# 不写-p会报错。
[root@image_boundary Desktop]# mkdir dd/ee     
mkdir: cannot create directory `dd/ee': No such file or directory
# 参数-p表示递归产生一个目录。
# 创建一个递归目录dd/ee。
[root@image_boundary Desktop]# mkdir -p dd/ee
[root@image_boundary Desktop]# cd dd
[root@image_boundary dd]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 20:22 ee


2)touch:创建文件

[root@image_boundary Desktop]# ll
total 16
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:21 cc
drwxr-xr-x. 3 root root 4096 Oct 15 20:22 dd
[root@image_boundary Desktop]# cd cc
[root@image_boundary cc]# ll
total 0
[root@image_boundary cc]# touch sum.txt
[root@image_boundary cc]# ll
total 4
-rw-r--r--. 1 root root 0 Oct 15 20:28 sum.txt


3)file:查看文件类型

-b:不显示文件名,只显示文件类型。

操作如下:


"file 文件名:可以显示其到底是一个文件,还是一个目录"
[root@image_boundary ~]$ file aa
aa: directory
[root@image_boundary cc]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 15 20:28 sum.txt
"查看当前文件夹的类型(这里指的就是cc):文件夹"
[root@image_boundary cc]# file .      
.: directory    
"如果sum.txt中没有内容,显示empty"
[root@image_boundary cc]# file sum.txt   
./sum.txt: empty
"如果sum.txt中有内容,显示sum.txt的文件类型"
[root@image_boundary cc]# file sum.txt  
./sum.txt: ASCII text
[root@image_boundary cc]# file -b sum.txt
ASCII text



4)rmdir:删除空目录

-p:递归删除空目录

操作如下:


"注意以下两个命令的区别"
rmdir dd/ee/ff      表示删除dd下面ee下面的这一个空目录ff。
rmdir -p dd/ee/ff   表示同时递归删除dd/ee/ff这3个目录。


5)rm:删除文件或目录

rm 文件名

-i:询问。(这个是默认情况,不写就是表示要询问)

-r:递归删除。

-f:强制删除(不提示删除)。

“下面这条命令:慎用!!!除非真的知道你在干嘛。”

rm -rf 目录/文件

操作如下:


[root@image_boundary bb]# ll
total 12
-rw-r--r--. 1 root root 149 Oct 15 20:00 a.txt
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt
[root@image_boundary bb]# rm a.txt
rm: remove regular file `a.txt'? n
[root@image_boundary bb]# ll
total 12
-rw-r--r--. 1 root root 149 Oct 15 20:00 a.txt
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt
[root@image_boundary bb]# rm a.txt 
rm: remove regular file `a.txt'? y
[root@image_boundary bb]# ll
total 8
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt
"rm -rf bb会删除bb目录下所有的目录和文件,问都不问,毫不留情。一定不要轻易使用该命令"
[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 20:53 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:30 cc
[root@image_boundary Desktop]# rm -rf bb
[root@image_boundary Desktop]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 20:30 cc



6)cp:复制文件或目录

注意:某个文件或目录被复制后,原始文件或目录依然存在。

cp 源文件(目录) 目标文件(目录)

-i:提示。

-r/-R参数:当【复制目录】的时候,必须用到这个参数。

-r/-R:递归复制目录。

-f参数:在搭建集群时,修改时区的时候用到

-f:覆盖已存在的目标文件,而不给出提示。

① 同一文件,复制到同一目录下,需要改名;否则,会报错。

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:00 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:00 bb
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# ll
total 4
-rw-r--r--. 1 root root 21 Oct 15 21:00 sum.txt
-rw-r--r--. 1 root root  0 Oct 15 20:59 sum.txt~
"把当前目录下的sum.txt文件,复制到当前目录下。"
"假如不修改文件名,会报错。"
[root@image_boundary aa]# cp sum.txt sum.txt  
cp: `sum.txt' and `sum.txt' are the same file
"修改文件名后,才不会报错。"
[root@image_boundary aa]# cp sum.txt sum1.txt
[root@image_boundary aa]# ll
total 8
-rw-r--r--. 1 root root 21 Oct 15 21:04 sum1.txt
-rw-r--r--. 1 root root 21 Oct 15 21:00 sum.txt
-rw-r--r--. 1 root root  0 Oct 15 20:59 sum.txt~


② 同一文件,复制到不同目录下,不需要改名;

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:04 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:00 bb
"将aa目录下的sum.txt文件,复制到bb目录下,不需要修改名称。"
[root@image_boundary Desktop]# cp aa/sum.txt bb/
[root@image_boundary Desktop]# cd bb
[root@image_boundary bb]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:01 bbb
-rw-r--r--. 1 root root   21 Oct 15 21:07 sum.txt


③ 递归复制bb目录中的东西(既包括文件,也包括目录),到aa目录中去。

"由于复制目录,因此必须使用参数【-r/-R】"
[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:52 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:07 bb
"将bb目录复制到aa目录下。"
"注意:复制目录的时候,必须要使用参数-r或者-R"
[root@image_boundary Desktop]# cp -r bb aa/
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# ll
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 21:53 bb
-rw-r--r--. 1 root root    0 Oct 15 20:59 sum.txt~


7)mv:移动某个文件或目录

-i:提示。

-f:强制移动。

-u:新覆盖旧,不存在时移动。

① 将同一个文件,移动到同级目录下,必须修改文件名,效果相当于重命名。

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:55 aa
drwxr-xr-x. 2 root root 4096 Oct 15 21:54 bb
[root@image_boundary Desktop]# mv bb/sum.txt bb/sum1.txt


相关文章
|
2月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
349 1
二、Linux文本处理与文件操作核心命令
|
2月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
264 137
|
2月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
595 57
|
1月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
273 2
|
2月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
154 0
Linux内存问题排查命令详解
|
2月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
204 16
|
3月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
473 0
|
3月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
150 0
|
3月前
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
4月前
|
Linux C++
每天一个linux命令(8):cp 命令
cp 命令是 Linux 中用于复制文件或目录的命令。它的名字来源于英文单词 copy。这个命令非常常用,特别是在需要备份文件或创建文件副本时。
208 0