鱼C论坛

 找回密码
 立即注册
查看: 295|回复: 1

[技术交流] linux_学习之路_6(啊啊啊啊啊啊啊啊啊啊我要硬薅Linux)

[复制链接]
回帖奖励 1 鱼币 回复本帖可获得 1 鱼币奖励! 每人限 1 次
发表于 2025-1-21 20:26:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
继续继续

接上回:
  1. #创建命令
  2. root@yanxu:/# mkdir -pv /mnt/test/{a,d}_{b,c}
  3. mkdir: created directory '/mnt/test/a_b'
  4. mkdir: created directory '/mnt/test/a_c'
  5. mkdir: created directory '/mnt/test/d_b'
  6. mkdir: created directory '/mnt/test/d_c'
  7. root@yanxu:/# tree /mnt/test
  8. /mnt/test
  9. ├── a_b
  10. ├── a_c
  11. ├── d_b
  12. ├── d_c
  13. ├── x
  14. │   └── m
  15. └── y

  16. 8 directories, 0 files
复制代码


删除命令:

rmdir (remove directory):只能删除空目录
举例:
  1. root@yanxu:/# rmdir /mnt/test
  2. rmdir: failed to remove '/mnt/test': Directory not empty
  3. r
复制代码

  1. root@yanxu:/# rmdir /mnt/test/a_b
  2. root@yanxu:/# tree /mnt/test
  3. /mnt/test
  4. ├── a_c
  5. ├── d_b
  6. ├── d_c
  7. ├── x
  8. │   └── m
  9. └── y

  10. 7 directories, 0 files
复制代码


如果我使用rmdir -p 来删除,只能是删除指定目录,如果这个目录删除完了父目录是空目录则继续删除;
举例:
  1. root@yanxu:/# rmdir -p /mnt/test/x/m
  2. rmdir: failed to remove directory '/mnt/test': Directory not empty
  3. root@yanxu:/# tree /mnt/test
  4. /mnt/test
  5. ├── a_c
  6. ├── d_b
  7. ├── d_c
  8. └── y

  9. 5 directories, 0 files
复制代码

说明已经删除了x/m 这个单一目录

文件的创建和删除:
touch : touch - change file timestamps(改变文件时间戳)
touch a:如果文件不存在,touch后会创建一个空文件目录

stat:stat - display file or file system status:(显示文件或者文件系统状态)
stat  a
  1. root@yanxu:/# stat a
  2.   File: a
  3.   Size: 4096            Blocks: 8          IO Block: 4096   directory
  4. Device: 8,32    Inode: 33554465    Links: 3
  5. Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
  6. Access: 2025-01-21 15:07:52.179993555 +0800
  7. Modify: 2025-01-21 15:07:52.179993555 +0800
  8. Change: 2025-01-21 15:07:52.179993555 +0800
  9. Birth: 2025-01-19 06:58:05.298910966 +0800
复制代码

Access是访问时间,Modify:修改时间,change:改变时间;
date一下当前时间:
  1. root@yanxu:/# date
  2. Tue Jan 21 15:11:20 CST 2025
复制代码

然后touch一下a文件,再stat a得到:
  1. root@yanxu:/# touch a
  2. root@yanxu:/# stat a
  3.   File: a
  4.   Size: 4096            Blocks: 8          IO Block: 4096   directory
  5. Device: 8,32    Inode: 33554465    Links: 3
  6. Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
  7. Access: 2025-01-21 15:11:49.499996056 +0800
  8. Modify: 2025-01-21 15:11:49.499996056 +0800
  9. Change: 2025-01-21 15:11:49.499996056 +0800
  10. Birth: 2025-01-19 06:58:05.298910966 +0800
复制代码

touch -a 改变访问时间
touch -m改变修改时间
无法改变改变时间,
touch -t 修改为自己设置的时间eg:
  1. root@yanxu:/# touch -mt 202401211527.30 a
  2. root@yanxu:/# stat a
  3.   File: a
  4.   Size: 4096            Blocks: 8          IO Block: 4096   directory
  5. Device: 8,32    Inode: 33554465    Links: 3
  6. Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
  7. Access: 2025-01-21 15:21:33.640008348 +0800
  8. Modify: 2024-01-21 15:27:30.000000000 +0800
  9. Change: 2025-01-21 15:32:44.630029666 +0800
  10. Birth: 2025-01-19 06:58:05.298910966 +0800
复制代码

可以看到修改时间已经改回我们自己设置的时间

创建文件也可以使用文件编辑器创建:
        ASCII:美国信息交换标准代码:有128个 不同的字符:
                二进制需要2^7 => 0,127  .
                即000 0000 - 111 1111
                一个字节8个位,
                0000 1001 : 代表了一个字符;所以ASCII表标准化;
        2^16次方:65536个,因此需要两个字节基本就可以存储所有汉字
        0000 1001 0000 1110:这个如何看是ASCII码还是字符;字符转换器
        产生标准:GB18030,GBK,GB1312,Unicode(世界上所有的字符都收录再这里),
        汉字如何在显示器显示出来的(待解决)

nano: nano - Nano's ANOther editor, inspired by Pico nano的文本编辑器,灵感来自于pico
        nano hello
  1. ^G Help        ^O Write Out   ^W Where Is    ^K Cut         ^T Execute     ^C Location    M-U Undo       M-A Set Mark
  2. ^X Exit        ^R Read File   ^\ Replace     ^U Paste       ^J Justify     ^/ Go To Line  M-E Redo       M-6 Copy   
复制代码

^代表的是ctrl键,ctrl+o保存文件  ctrl+x:退出
  1. root@yanxu:/# file hello
  2. hello: ASCII text
复制代码


如何删除文件:
rm +文件名
eg rm hello
  1. root@yanxu:/# rm hello
复制代码

rm -i :  -i     prompt before every removal-我在每次删除前提示
rm -f: -f, --force
              ignore nonexistent files and arguments, never prompt,忽略不存在的文件和参数,从不提示,
rm -r:-r, -R, --recursive
              remove directories and their contents recursively:递归删除目录及内容;
eg rm -rf:递归删除目录不提示;
rm -rf / :删除根目录以及根目录的所有文件
  1. root@yanxu:/# rm -rf /mnt/test
  2. root@yanxu:/# ls /mnt
  3. c  d  f  wsl  wslg
复制代码

生产过程中:敲回车时候需要提前思考一下;


复制和移动文件:
cp -copy
命令 cp srcfile dstfile
将一个文件一次复制多个文件?
可不可以将多个文件复制到一个目录?
可不可以将多个文件复制为一个文件?
cp:SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

copy file1 file2 file3 file4   file1,2,3都是源文件,file4是目标文件:
只能复制一个文件到一个文件,或者多个文件到某个目录;
eg
  1. root@yanxu:/# cp /etc/passwd  /tmp/
  2. root@yanxu:/# ls tmp
  3. passwd
  4. snap-private-tmp
  5. systemd-private-f12a58575a9347f79537ff62a0352ae7-chrony.service-laFQ0y
  6. systemd-private-f12a58575a9347f79537ff62a0352ae7-polkit.service-3kOvvP
  7. systemd-private-f12a58575a9347f79537ff62a0352ae7-systemd-logind.service-WkTLy5
  8. systemd-private-f12a58575a9347f79537ff62a0352ae7-systemd-resolved.service-a9EQEB
  9. systemd-private-f12a58575a9347f79537ff62a0352ae7-wsl-pro.service-UH1cbq
  10. root@yanxu:/#
复制代码


cp /etc/passwd /tmp/test
多个文件赋值的时候,目标必须是目录,如果是单个源文件复制到目标,目标可以是目录也可以是文件名
cp命令默认是不会复制目录的,只复制文件,
如果要复制目录,
cp -R, -r, --recursive
              copy directories recursively:递归复制目录;
eg:
  1. root@yanxu:/# cp -r /etc/init.d /tmp/hello
  2. root@yanxu:/# ls /tmp/hello
  3. apparmor  console-setup.sh  kea-ctrl-agent        kea-dhcp6-server   nmbd    samba-ad-dc          uuidd
  4. apport    cron              kea-dhcp-ddns-server  keyboard-setup.sh  procps  smbd                 vsftpd
  5. chrony    dbus              kea-dhcp4-server      kmod               rsync   unattended-upgrades  x11-common
复制代码

目标的目录若是不存在则会自动创建目录。只会将内容复制到新目录名称下
如果目标目录存在就只会复制源文件到目录下,此时会将父目录名称和子目录名称打包复制到存在的目录下。
如果源目录要复制到的目标是个文件则会爆出异常:
  1. root@yanxu:/# cp -r etc/init.d /tmp/56
  2. cp: cannot overwrite non-directory '/tmp/56' with directory 'etc/init.d'
  3. r
复制代码

cp -f :  -f, --force
              if  an existing destination file cannot be opened, remove it and try again (this option is ignored when
              the -n option is also used):如果目标文件无法被打开,将会把目标文件删除。
cp -i:交互式复制,(复制目录的时候每项都询问)
cp -P: -P, --no-dereference
              never follow symbolic links in SOURCE  :不遵信源符号链接
cp-p: -p     same as --preserve=mode,ownership,timestamps l类似于保留模式,保留模式(属主和属组),所有权和时间戳
cp -a:       -a, --archive
              same as -dR --preserve=all:保留文件所有的原来的属性
cp -d:  -d     same as --no-dereference --preserve=links:
cp -L: -L, --dereference
              always follow symbolic links in SOURCE总是跟随源的符号链接(符号链接相当于快捷方式,文件并不存在,只是指向目标文件的路径)
eg:复制链接
  1. root@yanxu:/tmp# cp /etc/vtrgb /tmp/hello
  2. root@yanxu:/tmp# ls -l /tmp/hello/vtrgb
  3. -rw-r--r-- 1 root root 158 Jan 21 20:07 /tmp/hello/vtrgb
复制代码

直接使用cp 复制链接,复制过去的是(链接指向的)文件。
如果使用cp -L ,则会复制链接过去(非文件)


运行程序
设备管理
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-21 20:27:05 | 显示全部楼层
Linux 学习之路 6:文件与目录操作以及文件复制与移动

1. 创建目录  
使用 mkdir -pv 创建嵌套目录:

  1. root@yanxu:/# mkdir -pv /mnt/test/{a,d}_{b,c}
复制代码

创建结果使用 tree 查看目录结构:

  1. /mnt/test
  2. ├── a_b
  3. ├── a_c
  4. ├── d_b
  5. ├── d_c
  6. ├── x
  7. │   └── m
  8. └── y
复制代码


---

2. 删除目录  
- rmdir 只能删除空目录。  
尝试删除非空目录时会报错:

  1. root@yanxu:/# rmdir /mnt/test
  2. rmdir: failed to remove '/mnt/test': Directory not empty
复制代码


- 使用 rmdir -p 删除空目录并逐层向上检查:

  1. root@yanxu:/# rmdir -p /mnt/test/x/m
  2. # 剩余结构:
  3. /mnt/test
  4. ├── a_c
  5. ├── d_b
  6. ├── d_c
  7. └── y
复制代码


---

3. 创建文件  
- touch: 改变文件时间戳并创建空文件。  
- 查看文件状态可使用 stat:  

  1. root@yanxu:/# touch a
  2. root@yanxu:/# stat a
  3. Access: 2025-01-21 15:11:49
  4. Modify: 2025-01-21 15:11:49
  5. Change: 2025-01-21 15:11:49
复制代码


- 改变时间戳:  
   - 修改访问时间:
  1. touch -a a
复制代码

   - 修改修改时间:
  1. touch -m a
复制代码

   - 设置自定义时间:
  1. touch -mt 202401211527.30 a
复制代码


---

4. 编辑器创建文件  
- 推荐使用 nano 文本编辑器:
  1. nano hello
复制代码

   - Ctrl+O 保存文件  
   - Ctrl+X 退出编辑器  

---

5. 删除文件  
- rm 用于删除文件:  

  1. rm hello
  2. rm -i hello   # 删除前确认
  3. rm -f hello   # 强制删除
  4. rm -r /mnt/test   # 递归删除非空目录
复制代码


注意: 小心使用 rm -rf /,这会删除根目录的所有文件。

---

6. 复制文件和目录  
- 复制单一文件:  

  1. cp /etc/passwd /tmp/
复制代码


- 复制多个文件到目录:  

  1. cp file1 file2 /target_dir
复制代码


- 复制目录需要使用 -r 递归选项:  

  1. cp -r /etc/init.d /tmp/hello
复制代码

自动创建不存在的目录。

- 保留文件属性:  
  - -p: 保留模式和时间戳  
  - -a: 保留所有属性  
  - -d: 保留符号链接  
  - -L: 跟随符号链接  

示例:  

  1. cp -a /etc/init.d /tmp/hello
复制代码


---

7. 符号链接复制  
- 普通拷贝:

  1. cp /etc/vtrgb /tmp/hello
复制代码

结果是复制文件本身,而非链接文件。

- 跟随符号链接:

  1. cp -L /etc/vtrgb /tmp/hello
复制代码

结果是复制符号链接而非文件内容。  

---

以上便是本节的内容,继续加油,Linux 知识点总能薅完! (警惕危险命令)

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 15:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表