鱼C论坛

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

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

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

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

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

x
继续继续

接上回:
#创建命令
root@yanxu:/# mkdir -pv /mnt/test/{a,d}_{b,c}
mkdir: created directory '/mnt/test/a_b'
mkdir: created directory '/mnt/test/a_c'
mkdir: created directory '/mnt/test/d_b'
mkdir: created directory '/mnt/test/d_c'
root@yanxu:/# tree /mnt/test
/mnt/test
├── a_b
├── a_c
├── d_b
├── d_c
├── x
│   └── m
└── y

8 directories, 0 files

删除命令:

rmdir (remove directory):只能删除空目录
举例:
root@yanxu:/# rmdir /mnt/test
rmdir: failed to remove '/mnt/test': Directory not empty
r
root@yanxu:/# rmdir /mnt/test/a_b
root@yanxu:/# tree /mnt/test
/mnt/test
├── a_c
├── d_b
├── d_c
├── x
│   └── m
└── y

7 directories, 0 files

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

5 directories, 0 files
说明已经删除了x/m 这个单一目录

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

stat:stat - display file or file system status:(显示文件或者文件系统状态)
stat  a
root@yanxu:/# stat a
  File: a
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 8,32    Inode: 33554465    Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-01-21 15:07:52.179993555 +0800
Modify: 2025-01-21 15:07:52.179993555 +0800
Change: 2025-01-21 15:07:52.179993555 +0800
 Birth: 2025-01-19 06:58:05.298910966 +0800
Access是访问时间,Modify:修改时间,change:改变时间;
date一下当前时间:
root@yanxu:/# date
Tue Jan 21 15:11:20 CST 2025
然后touch一下a文件,再stat a得到:
root@yanxu:/# touch a
root@yanxu:/# stat a
  File: a
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 8,32    Inode: 33554465    Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-01-21 15:11:49.499996056 +0800
Modify: 2025-01-21 15:11:49.499996056 +0800
Change: 2025-01-21 15:11:49.499996056 +0800
 Birth: 2025-01-19 06:58:05.298910966 +0800
touch -a 改变访问时间
touch -m改变修改时间
无法改变改变时间,
touch -t 修改为自己设置的时间eg:
root@yanxu:/# touch -mt 202401211527.30 a
root@yanxu:/# stat a
  File: a
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 8,32    Inode: 33554465    Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-01-21 15:21:33.640008348 +0800
Modify: 2024-01-21 15:27:30.000000000 +0800
Change: 2025-01-21 15:32:44.630029666 +0800
 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
^G Help        ^O Write Out   ^W Where Is    ^K Cut         ^T Execute     ^C Location    M-U Undo       M-A Set Mark
^X Exit        ^R Read File   ^\ Replace     ^U Paste       ^J Justify     ^/ Go To Line  M-E Redo       M-6 Copy    
^代表的是ctrl键,ctrl+o保存文件  ctrl+x:退出
root@yanxu:/# file hello
hello: ASCII text

如何删除文件:
rm +文件名
eg rm hello
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 / :删除根目录以及根目录的所有文件
root@yanxu:/# rm -rf /mnt/test
root@yanxu:/# ls /mnt
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
root@yanxu:/# cp /etc/passwd  /tmp/
root@yanxu:/# ls tmp
passwd
snap-private-tmp
systemd-private-f12a58575a9347f79537ff62a0352ae7-chrony.service-laFQ0y
systemd-private-f12a58575a9347f79537ff62a0352ae7-polkit.service-3kOvvP
systemd-private-f12a58575a9347f79537ff62a0352ae7-systemd-logind.service-WkTLy5
systemd-private-f12a58575a9347f79537ff62a0352ae7-systemd-resolved.service-a9EQEB
systemd-private-f12a58575a9347f79537ff62a0352ae7-wsl-pro.service-UH1cbq
root@yanxu:/#

cp /etc/passwd /tmp/test
多个文件赋值的时候,目标必须是目录,如果是单个源文件复制到目标,目标可以是目录也可以是文件名
cp命令默认是不会复制目录的,只复制文件,
如果要复制目录,
cp -R, -r, --recursive
              copy directories recursively:递归复制目录;
eg:
root@yanxu:/# cp -r /etc/init.d /tmp/hello
root@yanxu:/# ls /tmp/hello
apparmor  console-setup.sh  kea-ctrl-agent        kea-dhcp6-server   nmbd    samba-ad-dc          uuidd
apport    cron              kea-dhcp-ddns-server  keyboard-setup.sh  procps  smbd                 vsftpd
chrony    dbus              kea-dhcp4-server      kmod               rsync   unattended-upgrades  x11-common
目标的目录若是不存在则会自动创建目录。只会将内容复制到新目录名称下
如果目标目录存在就只会复制源文件到目录下,此时会将父目录名称和子目录名称打包复制到存在的目录下。
如果源目录要复制到的目标是个文件则会爆出异常:
root@yanxu:/# cp -r etc/init.d /tmp/56
cp: cannot overwrite non-directory '/tmp/56' with directory 'etc/init.d'
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:复制链接
root@yanxu:/tmp# cp /etc/vtrgb /tmp/hello
root@yanxu:/tmp# ls -l /tmp/hello/vtrgb
-rw-r--r-- 1 root root 158 Jan 21 20:07 /tmp/hello/vtrgb
直接使用cp 复制链接,复制过去的是(链接指向的)文件。
如果使用cp -L ,则会复制链接过去(非文件)


运行程序
设备管理
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

1. 创建目录  
使用 mkdir -pv 创建嵌套目录:
root@yanxu:/# mkdir -pv /mnt/test/{a,d}_{b,c}
创建结果使用 tree 查看目录结构:
/mnt/test
├── a_b
├── a_c
├── d_b
├── d_c
├── x
│   └── m
└── y

---

2. 删除目录  
- rmdir 只能删除空目录。  
尝试删除非空目录时会报错:
root@yanxu:/# rmdir /mnt/test
rmdir: failed to remove '/mnt/test': Directory not empty

- 使用 rmdir -p 删除空目录并逐层向上检查:
root@yanxu:/# rmdir -p /mnt/test/x/m
# 剩余结构:
/mnt/test
├── a_c
├── d_b
├── d_c
└── y

---

3. 创建文件  
- touch: 改变文件时间戳并创建空文件。  
- 查看文件状态可使用 stat:  
root@yanxu:/# touch a
root@yanxu:/# stat a
Access: 2025-01-21 15:11:49
Modify: 2025-01-21 15:11:49
Change: 2025-01-21 15:11:49

- 改变时间戳:  
   - 修改访问时间:
touch -a a

   - 修改修改时间:
touch -m a

   - 设置自定义时间:
touch -mt 202401211527.30 a


---

4. 编辑器创建文件  
- 推荐使用 nano 文本编辑器:
nano hello

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

---

5. 删除文件  
- rm 用于删除文件:  
rm hello
rm -i hello   # 删除前确认
rm -f hello   # 强制删除
rm -r /mnt/test   # 递归删除非空目录

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

---

6. 复制文件和目录  
- 复制单一文件:  
cp /etc/passwd /tmp/

- 复制多个文件到目录:  
cp file1 file2 /target_dir

- 复制目录需要使用 -r 递归选项:  
cp -r /etc/init.d /tmp/hello

自动创建不存在的目录。

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

示例:  
cp -a /etc/init.d /tmp/hello

---

7. 符号链接复制  
- 普通拷贝:
cp /etc/vtrgb /tmp/hello
结果是复制文件本身,而非链接文件。

- 跟随符号链接:
cp -L /etc/vtrgb /tmp/hello
结果是复制符号链接而非文件内容。  

---

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-22 22:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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