成为大佬之路--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]# 
相关推荐
PatrickYao042212 分钟前
记一次安装discuz时遇到的错误
服务器
c无序15 分钟前
【Linux进程控制】进程程序替换
linux
小宋10212 小时前
玩转RabbitMQ声明队列交换机、消息转换器
服务器·分布式·rabbitmq
m0_609000422 小时前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
小安运维日记3 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
kejijianwen3 小时前
JdbcTemplate常用方法一览AG网页参数绑定与数据寻址实操
服务器·数据库·oracle
CoolTiger、6 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_741768856 小时前
使用docker的小例子
运维·docker·容器
学习3人组7 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神7 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu