成为大佬之路--linux软件安装使用第000000040篇-linux操作文件和文件夹常用命令

1.创建文件

touch 文件名

下面命令创建了AscendKing.txt文件

然后过滤只要包含AscendKing的文件和文件夹

[root@VM-24-14-centos opt]# touch AscendKing.txt
[root@VM-24-14-centos opt]# ls -l AscendKing*
-rw-r--r-- 1 root root 0 7月  31 23:13 AscendKing.txt
[root@VM-24-14-centos opt]# 

2.创建文件夹

mkdir 文件夹名称

下面示例演示了创建"AscendKing1"文件夹

[root@VM-24-14-centos opt]# mkdir  AscendKing1
[root@VM-24-14-centos opt]# ls -l AscendKing*
-rw-r--r-- 1 root root    0 7月  31 23:13 AscendKing.txt

AscendKing1:
总用量 0

AscendKing9:
总用量 0
[root@VM-24-14-centos opt]# 

3.创建多级文件夹(级联创建)

mkdir -p 多级文件目录

[root@VM-24-14-centos AscendKing]# pwd
/opt/AscendKing
[root@VM-24-14-centos AscendKing]# mkdir -p ./AscendKing-test1/AscendKing-test2/AscendKing-test3
[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1
[root@VM-24-14-centos AscendKing]# cd AscendKing-test1/
[root@VM-24-14-centos AscendKing-test1]# ls
AscendKing-test2
[root@VM-24-14-centos AscendKing-test1]# cd AscendKing-test2/
[root@VM-24-14-centos AscendKing-test1]# ls
AscendKing-test3
[root@VM-24-14-centos AscendKing-test2]# cd AscendKing-test3/
[root@VM-24-14-centos AscendKing-test3]# 

4.复制文件

cp 需要复制的文件 新地址

cp -i强制询问是否覆盖

cp可以使用通配符

cp -R 可以递归复制,可以用来复制目录

[root@VM-24-14-centos AscendKing]# touch test.txt
[root@VM-24-14-centos AscendKing]# cp test.txt  test.txt 
cp: 'test.txt' 与'test.txt' 为同一文件
[root@VM-24-14-centos AscendKing]# cp test.txt  test2.txt 
[root@VM-24-14-centos AscendKing]# cp test.txt  -i test2.txt 
cp:是否覆盖'test2.txt'? 
[root@VM-24-14-centos AscendKing]# 

5.移动文件(重命名)

mv 需要复制的文件 新地址

[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1  test2.txt  test.txt
[root@VM-24-14-centos AscendKing]# mv test.txt test3.txt
[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1  test2.txt  test3.txt
[root@VM-24-14-centos AscendKing]# 

6.删除文件

rm 文件名

示例

[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1  test2.txt  test3.txt
[root@VM-24-14-centos AscendKing]# rm -rf ./*.txt
[root@VM-24-14-centos AscendKing]# 

7.删除文件夹

方式1:使用rm

方式2:使用rmdir

[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1
[root@VM-24-14-centos AscendKing]# mkdir test
[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1  test
[root@VM-24-14-centos AscendKing]# rmdir test
[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1

8.查看文件类型

file 文件

[root@VM-24-14-centos AscendKing]# ls
AscendKing-test1  test
[root@VM-24-14-centos AscendKing]# file AscendKing-test1/
AscendKing-test1/: directory
[root@VM-24-14-centos AscendKing]# touch test.txt
[root@VM-24-14-centos AscendKing]# file test.txt 
test.txt: empty
[root@VM-24-14-centos AscendKing]# vim test.txt 
[root@VM-24-14-centos AscendKing]# file test.txt 
test.txt: ASCII text

9.查看文件内容

9.1cat

cat 文件

cat -n 文件 显示所有行行号

cat -b 文件 只显示有文本的行号

cat -T 文件 不显示制表符

[root@VM-24-14-centos AscendKing]# cat test.txt 
111
[root@VM-24-14-centos AscendKing]# 

9.2more

我的文件内容少,直接展示了全部,如果文件内容多直接enter继续阅读下面

[root@VM-24-14-centos AscendKing]# more test.txt 
开水
第1行内容
第2行内容
第3行内容
第4行内容
第5行内容
第6行内容
结束
[root@VM-24-14-centos AscendKing]# 

9.3less

[root@VM-24-14-centos AscendKing]# less test.txt 
开水
第1行内容
第2行内容
第3行内容
第4行内容
第5行内容
第6行内容
结束
[root@VM-24-14-centos AscendKing]# 

9.4查看文件最后几行tail

[root@VM-24-14-centos AscendKing]# tail -5f test.txt
第3行内容
第4行内容
第5行内容
第6行内容
结束

9.5查看文件开头几行head

[root@VM-24-14-centos AscendKing]# head -5 test.txt
开始
第1行内容
第2行内容
第3行内容
第4行内容
[root@VM-24-14-centos AscendKing]# 
相关推荐
code monkey.4 分钟前
【寻找Linux的奥秘】第一章:基础指令
linux·运维·服务器
qziovv4 分钟前
Ubuntu通过局域网共享文件夹实现文件夹的连接
linux·运维·ubuntu
海鸥819 分钟前
在 k8s中查看最大 CPU 和内存的极限
linux·容器·kubernetes
森焱森11 分钟前
AArch64架构及其编译器
linux·c语言·单片机·架构
rkmhr_sef21 分钟前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
Lin桐39 分钟前
②Modbus TCP转Modbus RTU/ASCII网关同步采集无需编程高速轻松组网
linux·网络协议·tcp/ip·网络安全·缓存·信息与通信·信号处理
罗技1231 小时前
Easysearch 使用 AWS S3 进行快照备份与还原:完整指南及常见错误排查
linux·云计算·aws·es·easysearch
anguruanjian1 小时前
安固软件指南:确保外发文件安全的全面策略
服务器·网络·安全·企业微信·安固软件
孙同学_1 小时前
【Linux篇】调试器-gdb/cgdb使用
linux·运维·服务器
自由鬼1 小时前
免费开源抓包工具Wireshark介绍
运维·服务器·网络·测试工具·网络安全·wireshark