成为大佬之路--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]# 
相关推荐
神秘剑客_CN26 分钟前
使用windows笔记本让服务器上网
运维·服务器·windows
黑牛先生2 小时前
【Linux】动静态库
linux·运维·服务器
vortex52 小时前
Shell基础:中括号的使用
linux·运维·bash·shell
基哥的奋斗历程2 小时前
Docker 常用命令
运维·docker·容器
HEX9CF4 小时前
【Docker】快速部署 Nacos 注册中心
运维·docker·容器
从未止步..5 小时前
Jenkins未在第一次登录后设置用户名,第二次登录不进去怎么办?
java·运维·jenkins
davenian5 小时前
< OS 有关> BaiduPCS-Go 程序的 菜单脚本 Script: BaiduPCS-Go.Menu.sh (bdgo.sh)
运维·shell script·baidupcs-go·linux ubuntu
-VE-6 小时前
myshell
linux·c++
喝醉酒的小白6 小时前
几种K8s运维管理平台对比说明
运维·容器·kubernetes
明 庭6 小时前
通过 Docker 部署 pSQL 服务器的教程
服务器·docker·容器