文件和目录的基本操作

发布时间 2023-08-18 15:12:14作者: depressiom

创建文件

cat命令 concatenate(连接)的缩写,即 combine pieces together

1)把碎片组合在一起(意味着可以使用cat创建一个小文件)
2)显示文件

──(root㉿kali)-[~]
└─# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.56.2


┌──(root㉿kali)-[~/work/exam]  组合在一起 ctrl+D 退出编辑
└─# cat > hackingskills 
welcome to sdxh!
Study security.                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cat hackingskills  
welcome to sdxh!
Study security.                                                                                               

┌──(root㉿kali)-[~/work/exam] >> 内容追加
└─# cat >> hackingskills 
depressioom
yes
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cat hackingskills   
welcome to sdxh!
Study security.depressioom
yes
      

touch 创建文件 创建一个空文件

这个命令最初是为了让用户可以简单的触摸一个文件更改细节,比如创建或修改时间,如果该文件不存在,该命令会默认创建该文件

┌──(root㉿kali)-[~/work/exam]
└─# ls -l        
总计 8
-rw-r--r-- 1 root root  16  8月18日 11:12 catfile
-rw-r--r-- 1 root root 105  8月18日 14:22 ex01.sh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# touch catfile
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# date         
2023年 08月 18日 星期五 14:31:55 CST
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls -l        
总计 8
-rw-r--r-- 1 root root  16  8月18日 14:31 catfile
-rw-r--r-- 1 root root 105  8月18日 14:22 ex01.sh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# touch f1                                                                
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls -l
总计 8
-rw-r--r-- 1 root root  16  8月18日 14:31 catfile
-rw-r--r-- 1 root root 105  8月18日 14:22 ex01.sh
-rw-r--r-- 1 root root   0  8月18日 14:32 f1


扩展 在脚本中使用cat创建一个文件

vi的基本使用

1、 当使用vi编辑一个文件时,默认进入的模式是命令模式
2、 由命令模式进入到插入模式(输入、编辑等 ) i (insert,插入)
3、 由插入模式返回到命令模式 ESC
4、 保存退出,按冒号 (? 进入到末行 (底行模式) 执行wq保存退出执行q!退出不保存

#! /bin/bash 第一行
// #!:告诉操作系统我会使用哪个解释器
// bash:是常用的一种 shell命令解释器 

┌──(root㉿kali)-[~/work/exam]
└─# vi ex01.sh               
                                                                                               
┌──(root㉿kali)-[~/work/exam] echo 打印显示
└─# cat ex01.sh
#! /bin/bash
# this is my first script
echo "hello,depressiom"

┌──(root㉿kali)-[~/work/exam] #没有x 执行位权限
└─# ls -l ex01.sh
-rw-r--r-- 1 root root 64  8月18日 10:51 ex01.sh
 
┌──(root㉿kali)-[~/work/exam] #chmod +x  添加可执行权限
└─# chmod +x ex01.sh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls -l ex01.sh   
-rwxr-xr-x 1 root root 64  8月18日 10:51 ex01.sh
     
┌──(root㉿kali)-[~/work/exam] # 1.在当前目录下执行脚本
└─# ./ex01.sh
hello,depressiom
    
┌──(root㉿kali)-[~/work/exam] # 2.输入脚本的完整路径 执行
└─# /root/work/exam/ex01.sh
hello,depressiom


┌──(root㉿kali)-[~/work/exam] # 3.脚本没有可执行权限的 执行方法
└─# chmod -x ex01.sh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ./ex01.sh
zsh: 权限不够: ./ex01.sh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# sh /root/work/exam/ex01.sh
hello,depressiom
    

┌──(root㉿kali)-[~/work/exam]
└─# cat ex01.sh     
#! /bin/bash
# this is my first script
cat > catfile << EOF # 创建catfile 以 EOF结束
I am depressiom # 文件的内容
EOF
ls catfile # 查看catfile
cat -n catfile #用cat 命令 查看catfile
  
┌──(root㉿kali)-[~/work/exam]
└─# ls           
catfile  ex01.sh
             

mkdir 创建目录

                                                                                              
┌──(root㉿kali)-[~/work/exam]
└─# mkdir -p dir{1..10}/{ah,sh,sc}xh/dir{1..100}
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls -l
总计 48
-rw-r--r-- 1 root root   16  8月18日 14:31 catfile
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir1
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir10
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir2
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir3
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir4
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir5
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir6
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir7
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir8
drwxr-xr-x 5 root root 4096  8月18日 14:35 dir9
-rw-r--r-- 1 root root  105  8月18日 14:22 ex01.sh
-rw-r--r-- 1 root root    0  8月18日 14:32 f1
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls dir1
ahxh  scxh  shxh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls ahxh
ls: 无法访问 'ahxh': 没有那个文件或目录
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls dir1/scxh
dir1    dir16  dir23  dir30  dir38  dir45  dir52  dir6   dir67  dir74  dir81  dir89  dir96
dir10   dir17  dir24  dir31  dir39  dir46  dir53  dir60  dir68  dir75  dir82  dir9   dir97
dir100  dir18  dir25  dir32  dir4   dir47  dir54  dir61  dir69  dir76  dir83  dir90  dir98
dir11   dir19  dir26  dir33  dir40  dir48  dir55  dir62  dir7   dir77  dir84  dir91  dir99
dir12   dir2   dir27  dir34  dir41  dir49  dir56  dir63  dir70  dir78  dir85  dir92
dir13   dir20  dir28  dir35  dir42  dir5   dir57  dir64  dir71  dir79  dir86  dir93
dir14   dir21  dir29  dir36  dir43  dir50  dir58  dir65  dir72  dir8   dir87  dir94
dir15   dir22  dir3   dir37  dir44  dir51  dir59  dir66  dir73  dir80  dir88  dir95

cp 复制文件

                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# touch oldfile
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cp oldfile /root/work/exam
cp: 'oldfile' 与 '/root/work/exam/oldfile' 为同一文件
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cp oldfile /root/work/exam newfile
cp: 目标 'newfile': 没有那个文件或目录
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cp oldfile /root/work/exam/newfile
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls          
newfile  oldfile

┌──(root㉿kali)-[~/work/exam]
└─# echo "hello world" > oldfile
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cat oldfile                       
hello world
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cp oldfile /root/work/exam/newfile
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# cat newfile                       
hello world

┌──(root㉿kali)-[~/work/exam]
└─# cp -a dir1  dir2
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls
dir1  dir2  newfile  oldfile

mv 文件重命名和文件移动

┌──(root㉿kali)-[~/work/exam] # 位置未发生变化 重命名
└─# mv oldfile oldfile1
                                                                                               
┌──(root㉿kali)-[~/work/exam] 
└─# ls                 
dir1  dir2  newfile  oldfile1

┌──(root㉿kali)-[~/work/exam] # 位置发生改变移动
└─# mv oldfile1 dir1/oldfile 
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls dir1     
oldfile
        

删除文件 rm

┌──(root㉿kali)-[~/work/exam]
└─# ls     
dir1  dir2  newfile
                                                                                               
┌──(root㉿kali)-[~/work/exam] # 删除文件
└─# rm newfile    
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls
dir1  dir2



┌──(root㉿kali)-[~/work/exam] # 帮助文档
└─# rm --help     
用法:rm [选项]... [文件]...
删除 (unlink) 一个或多个 <文件>。

  -f, --force           忽略不存在的文件和参数,且从不询问
  -i                    每次删除前询问
  -I                    在删除超过三个文件或者递归删除前询问一次;此选项比 -i
                          提示次数更少,但仍可以避免大多数错误的发生
      --interactive[=何时]  根据 <何时> 的值进行询问:never、once(同 -I)或者
                          always(同 -i);如果省略 <何时>,则默认为 always
      --one-file-system  递归删除目录时,跳过所有和该目录所对应的命令行参
                          数不在同一个文件系统上的目录
      --no-preserve-root  不要对 "/" 特殊处理
      --preserve-root[=all]  不要删除 "/"(默认行为);
                              如添加了 "all" 参数,将拒绝处理与其父目录位于
                              不同设备上的命令行参数
  -r, -R, --recursive   递归删除目录及其内容
  -d, --dir             删除空目录
  -v, --verbose         显示详细步骤
      --help        显示此帮助信息并退出
      --version     显示版本信息并退出


┌──(root㉿kali)-[~/work/exam] # 删除目录
└─# rm -rf dir1   
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# ls
dir2


┌──(root㉿kali)-[~/work/exam] # kali的保护机制
└─# echo $SHELL
/usr/bin/zsh
                                                                                               
┌──(root㉿kali)-[~/work/exam]
└─# rm -rf /   
rm: 在 '/' 进行递归操作十分危险
rm: 使用 --no-preserve-root 关闭此安全保护机制

文件基础操作 测试

测试

bash 技巧

  1. ESC + . (或 !$)调用上一个命令的参数
  2. 重定向 > 覆盖原内容
  3. 重定向 >> 追加内容