Linux相关知识的第二回合

描述Linux发行版的系统目录名称命名规则以及用途

Linux系统目录名称命名的规则

文件和目录被组织成一个单根倒置的树结构
文件系统从根目录开始,用/表示
根文件系统(rootfs):root filesystem
标准Linux文件系统(eg:ext4),文件名称大小写敏感,eg:MAIL、Mail、mail、maIL都是代表不同的目录
.开头的文件为隐藏文件
路径分隔/
文件名最长不能超过255字节;包括路径在内的文件名称最长不能超过4095字节
不同颜色的文件或目录代表着不同的文件类型:蓝色代表目录;绿色代表可执行文件;红色代表压缩文件;浅蓝色代表链接文件;灰色代表其他文件;决定这些颜色的配置文件:/etc/DIR_COLORS
除了/空格,所有字符都有效;但不推荐使用特殊字符命名目录或者文件,特殊字符需要转移符来引用
每个文件都有两类相关数据:
   元数据:metadata,即属性,eg:文件的大小、创建时间、属主等
   数据:data,即文件内容
Linux目录配置标准:FHS Filesystem Hierarchy Standard

常见的文件系统目录的用途

tree -L 2 /
/
├── bin -> usr/bin  # 所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
├── boot    # 引导文件存放目录,内核文件(vmlinuz),引导加载器(bootloader,grub)都存放在此目录
├── data    # 自定义目录
├── dev # 设备文件及特殊文件目录;eg:硬盘;
├──     # b:block device,随机访问,c:character device,线性访问
├── etc # 系统配置文件目录
├── home    # 普通用户的家目录
├── lib -> usr/lib  # 启动程序时依赖的基本共享库文件及内核模块文件(`/lib/modules`)
├── lib64 -> usr/lib64  # 专用于X86_64系统上的辅助共享库文件目录
├── media   # 便携式移动设备挂在点;eg:光驱、U盘等
├── mnt # 临时文件系统挂在点
├── opt # 第三方应用程序的安装目录 
├── proc    # 用于输出内核与进程信息相关的虚拟文件系统
├── root    # 管理员的家目录
├── run # 运行中的进程相关数据,通常用于存储进程pid文件
├── sbin -> usr/sbin    # 管理类的基本命令,不能关联至独立分区,OS启动即会用到的程序
├── srv  # 系统上运行的服务用到的数据
├── sys # 用于输出当前系统上硬件设备相关信息虚拟文件系统
├── tmp # 临时文件目录
├── usr # universal shared,read-only data  全局共享的只读数据路径
│   ├── bin # 保证系统拥有完整功能而提供的应用程序
│   ├── etc # 配置文件
│   ├── games
│   ├── include # C程序的头文件(header files)
│   ├── lib  # 32位使用
│   ├── lib64   # 专用于X86_64系统
│   ├── libexec
│   ├── local   # 第三方应用程序的安装目录
│   ├── sbin
│   ├── share   # 结构化独立的数据,eg:doc,man等
│   ├── src
│   └── tmp -> ../var/tmp
└── var # variable data files
    ├── adm
    ├── cache   # 应用程序缓存数据目录
    ├── crash
    ├── db
    ├── empty
    ├── games
    ├── gopher
    ├── kerberos
    ├── lib # 应用程序状态信息数据
    ├── local   # 专用于为/usr/local下的应用程序存储可变数据
    ├── lock -> ../run/lock # 锁文件
    ├── log # 日志文件及目录
    ├── mail -> spool/mail
    ├── nis
    ├── opt # 专用于为/opt下的应用程序存储可变数据
    ├── preserve
    ├── run -> ../run   # 运行中的进程相关数据,通常用于存储进程pid文件
    ├── spool   # 应用程序数据池
    ├── tmp # 保存系统两次重启之间产生的临时数据
    └── yp

描述文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息?

元数据信息内容及含义

文件的完整名称:file
文件数据大小:size
占用磁盘的块数:blocks
IO块的大小:IO block
所在设备:device
文件的类型:regular
文件的节点:indoe
文件的连接数:links
文件的访问权限:access
文件的属主:uid
文件的属组:gid
文件的访问时间:access time atime 读取文件内容的时间
文件的修改时间:modify time mtime 修改文件内容的时间
文件的改变时间:change time ctime 元数据发生改变的时间
   修改文件的权限、属主、属组、添加软连接等,都会修改ctime的时间
   注意⚠️:修改文件内容,除了mtime会改变,同时ctime也会改变的

查看一个文件或目录的元数据,我们可以使用stat这个命令

ll /data/sh/pyenv-installer
-rwxr-xr-x 1 root root 2.4K Feb 25 14:50 /data/sh/pyenv-installer

stat /data/sh/pyenv-installer
  File: ‘/data/sh/pyenv-installer’
  Size: 2454      	Blocks: 8          IO Block: 4096   regular file
Device: fd03h/64771d	Inode: 1567        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-25 14:50:38.559596243 +0800
Modify: 2021-02-25 14:50:23.105408493 +0800
Change: 2021-02-25 14:50:29.777489547 +0800
 Birth: -
 
 stat /data/sh
  File: ‘/data/sh’
  Size: 29        	Blocks: 0          IO Block: 4096   directory
Device: fd03h/64771d	Inode: 67          Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-16 10:00:16.279965428 +0800
Modify: 2021-03-08 10:04:43.938121224 +0800
Change: 2021-03-08 10:04:43.938121224 +0800
 Birth: -
 
 stat /dev/vda
  File: ‘/dev/vda’
  Size: 0         	Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d	Inode: 8871        Links: 1     Device type: fc,0
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2021-02-24 16:11:56.986000000 +0800
Modify: 2021-02-24 16:11:56.967000000 +0800
Change: 2021-02-24 16:11:56.967000000 +0800
 Birth: -

修改文件的时间戳信息
想要修改一个文件的访问、修改、改变时间的话,可以使用touch 这个命令

touch 这个命令不仅可以创建文件,如果文件存在的话,会刷新文件的时间
touch <existfile> 刷新文件的所有时间
touch -a <existfile> 仅刷新文件的atime及ctime
touch -m <existfile> 仅刷新文件的mtime及ctime
touch -t [[CC]YY]MMDDhhmm[.ss] <existfile> 指定刷新文件的atime及mtime的时间戳
touch -c 如果文件不存在,则不予创建

cp -rp /var/log/cron-20210228 /tmp/

# 查看cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-05-25 20:29:14.902541963 +0800
Modify: 2021-02-28 03:43:01.559588497 +0800
Change: 2021-03-17 16:33:30.474142243 +0800
 Birth: -
 
# 刷新文件的时间
touch cron-20210228
# 再次查看 cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:34:54.407097490 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:34:54.407097490 +0800
 Birth: -

# 仅刷新atime及ctime时间 
touch -a cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:42:56.627585679 +0800
 Birth: -
 
 # 仅刷新mtime及ctime时间
touch -m cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:44:13.308458381 +0800
Change: 2021-03-17 16:44:13.308458381 +0800
 Birth: -
 
 # 刷新atime及mtime为02282008的时间戳
touch -t 02282008 cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-28 20:08:00.000000000 +0800
Modify: 2021-02-28 20:08:00.000000000 +0800
Change: 2021-03-17 16:45:49.738555861 +0800
 Birth: -

总结软、硬连接区别,并用实例操作说明

linux系统中,链接分两种:一种被称为硬链接(Hard Link),另一种被称为符号链接或软链接(Symbolic Link)
使用ln命令来创建链接,ln默认创建的是硬链接;ln -s创建的是软链接
硬链接文件与源文件的inode节点号相同,而软链接文件的inode节点号与源文件不同
删除软链接文件,对源文件及硬链接文件无任何影响
删除文件的硬链接文件,对源文件及软链接文件无任何影响
删除链接文件的原文件,对硬链接文件无影响,会导致其软链接失效(红底白字闪烁状)
同时删除原文件及其硬链接文件,整个文件才会被真正的删除
软连接可以跨文件系统,硬链接不可以跨文件系统
软链接可以对目录进行连接,硬连接不可以
很多硬件设备中的快照功能,使用的就类似硬链接的原理
软链接可以对一个不存在的文件名进行链接,硬链接不行

实例:

[hooper@magedu-demo ~]$ mkdir link_dir
[hooper@magedu-demo ~]$ cd link_dir/
[hooper@magedu-demo link_dir]$ ll
总用量 0
[hooper@magedu-demo link_dir]$ touch hooper
# 查看文件的连接数及inode号,连接数为1,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 1 hooper hooper 0 3月  17 18:18 hooper
# 创建硬链接hooper_hard_link
[hooper@magedu-demo link_dir]$ ln hooper hooper_hard_link
# 查看文件的连接数及inode号,连接数为2,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 2 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 2 hooper hooper 0 3月  17 18:18 hooper_hard_link
# 再次创建一个硬链接hooper_hard_link_1
[hooper@magedu-demo link_dir]$ ln hooper hooper_hard_link_1
# 再次查看文件的连接数及inode号,连接数为3,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link_1
# 创建一个软连接hooper_soft_link
[hooper@magedu-demo link_dir]$ ln -s hooper hooper_soft_link
# 再次查看文件的连接数及inode号,硬链接连接数为3,软连接文件的inode为75
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link_1
75 lrwxrwxrwx 1 hooper hooper 6 3月  17 18:19 hooper_soft_link -> hooper
# 写入数据到原文件中
[hooper@magedu-demo link_dir]$ cat > hooper << EOF
> I am hooper
> EOF
# 分别查看原文件、硬、软链接文件
[hooper@magedu-demo link_dir]$ cat hooper
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_hard_link_1
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
I am hooper
# 删除硬链接文件
[hooper@magedu-demo link_dir]$ rm -rf hooper_hard_link_1
# 查看文件的连接数及inode号,硬链接连接数为2,其他不变
[hooper@magedu-demo link_dir]$ ls -li
总用量 8
74 -rw-rw-r-- 2 hooper hooper 12 3月  17 18:20 hooper
74 -rw-rw-r-- 2 hooper hooper 12 3月  17 18:20 hooper_hard_link
75 lrwxrwxrwx 1 hooper hooper  6 3月  17 18:19 hooper_soft_link -> hooper
# 分别查看文件内容
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper
I am hooper
# 删除原文件
[hooper@magedu-demo link_dir]$ rm -rf hooper
# 查看文件的连接数及inode号,连接数都变成1,inode分别为74、75;但软连接文件显示红色
[hooper@magedu-demo link_dir]$ ls -li
总用量 4
74 -rw-rw-r-- 1 hooper hooper 12 3月  17 18:20 hooper_hard_link
75 lrwxrwxrwx 1 hooper hooper  6 3月  17 18:19 hooper_soft_link -> hooper
# 分别查看文件内容
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
cat: hooper_soft_link: 没有那个文件或目录

软、硬链接的区别

Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关的实例演示

在Linux中,常见的文件管理类命令:ls、stat、file、touch、cp、mv、rename、rm

ls

功能说明

列出当前目录的内容及指定目录

语法

ls [OPTION]... [FILE]...

选项

-a 包含隐藏文件
-l 显示额外信息
-R 目录递归
-ld 目录和符号链接信息
-t 按mtime排序
-U 按目录存放顺序显示
–X 按文件后缀排序

实例

# 显示/etc/sysconfig/network-scripts/目录下所有文件信息
ls -la /etc/sysconfig/network-scripts/
total 240
drwxr-xr-x. 2 root root  4096 Feb 24 15:01 .
drwxr-xr-x. 6 root root  4096 Feb 24 15:13 ..
-rw-r--r--  1 root root   323 Feb 24 15:01 ifcfg-eth0
-rw-r--r--. 1 root root   254 Aug 19  2019 ifcfg-lo
lrwxrwxrwx. 1 root root    24 May 25  2020 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root   654 Aug 19  2019 ifdown-bnep
-rwxr-xr-x. 1 root root  6532 Aug 19  2019 ifdown-eth
-rwxr-xr-x. 1 root root   781 Aug 19  2019 ifdown-ippp
-rwxr-xr-x. 1 root root  4540 Aug 19  2019 ifdown-ipv6
lrwxrwxrwx. 1 root root    11 May 25  2020 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root  2130 Aug 19  2019 ifdown-post
-rwxr-xr-x. 1 root root  1068 Aug 19  2019 ifdown-ppp

# 按照mtime时间排序显示/var/log/目录下的内容
ls -lhrt /var/log/
total 1.9M
drwxr-xr-x. 2 chrony chrony    6 Aug  8  2019 chrony
drwxr-xr-x. 2 root   root      6 Aug  8  2019 qemu-ga
drwxr-xr-x  2 root   root      6 Sep  6  2019 atop
-rw-r--r--. 1 root   root    193 May 25  2020 grubby_prune_debug
-rw-------. 1 root   utmp      0 May 25  2020 btmp-20210301
-rw-------. 1 root   root      0 May 25  2020 spooler-20210228
drwxr-xr-x. 2 root   root    204 May 25  2020 anaconda
drwxr-xr-x. 2 root   root      6 May 25  2020 rhsm
drwx------. 2 root   root     23 May 25  2020 audit
-rw-r-----. 1 root   root      0 May 25  2020 firewalld
drwxr-xr-x. 2 root   root     23 May 25  2020 tuned
-rw-------. 1 root   root   2.6K Feb 24 15:09 grubby
-rw-r--r--  1 root   root    34K Feb 24 15:12 dmesg.old
-rw-------. 1 root   root   1.3K Feb 24 15:12 maillog-20210228
-rw-------. 1 root   root    63K Feb 24 15:43 tallylog
-rw-r--r--  1 mysql  mysql     0 Feb 24 15:43 mysqld.log
-rw-r--r--  1 root   root    34K Feb 24 16:11 dmesg
-rw-------  1 root   root    59K Feb 25 03:15 boot.log-20210225
-rw-------. 1 root   root      0 Feb 25 03:15 boot.log
-rw-------. 1 root   root    23K Feb 26 11:00 secure-20210228
-rw-------. 1 root   root    65K Feb 28 03:43 cron-20210228
-rw-------  1 root   root      0 Feb 28 03:43 spooler-20210307
-rw-------  1 root   root      0 Feb 28 03:43 maillog-20210307
-rw-------. 1 root   root   611K Feb 28 03:43 messages-20210228
-rw-------. 1 root   root    12K Mar  2 11:49 yum.log
-rw-------  1 root   utmp    384 Mar  3 14:50 btmp
-rw-------  1 root   root    14K Mar  5 17:52 secure-20210307
-rw-------  1 root   root   121K Mar  7 03:15 cron-20210307
-rw-------  1 root   root      0 Mar  7 03:15 spooler-20210314
-rw-------  1 root   root      0 Mar  7 03:15 maillog-20210314
-rw-------  1 root   root   281K Mar  7 03:15 messages-20210307
-rw-------  1 root   root   4.3K Mar 12 18:44 secure-20210314
-rw-------  1 root   root   122K Mar 14 03:23 cron-20210314
-rw-------  1 root   root      0 Mar 14 03:23 spooler
-rw-------  1 root   root      0 Mar 14 03:23 maillog
-rw-------  1 root   root   235K Mar 14 03:23 messages-20210314
drwxr-xr-x. 2 root   root   4.0K Mar 17 00:00 sa
-rw-rw-r--. 1 root   utmp    49K Mar 17 15:04 wtmp
-rw-------  1 root   root   3.7K Mar 17 15:04 secure
-rw-r--r--. 1 root   root   286K Mar 17 15:04 lastlog
-rw-------  1 root   root    62K Mar 17 17:01 cron
-rw-------  1 root   root   122K Mar 17 17:01 messages

stat

功能说明

查看一个文件或目录的元数据

语法

stat [OPTION]... FILE...

选项

上面文档已经写过,再次不再赘述

实例

上面文档已经写过,再次不再赘述

file

功能说明

查看一个文件的类型

语法

file [options] <filename>...

选项

-b:列出文件辨识结果时,不显示文件名称
-f filelist: 列出文件filelist中文件名的文件类型
-F:使用指定分隔符号替换输出文件名后默认的:分隔符
-L:查看对应软连接对应的文件类型
--help:显示命令的在线帮助

实例

file /usr/bin/cal
/usr/bin/cal: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ee47c2bd90a4f3981d89e8fbb5e241365871eb7f, stripped

file /data/sh/pyenv-installer
/data/sh/pyenv-installer: a /usr/bin/env bash script, ASCII text executable

file /dev/vda
/dev/vda: block special

file /etc
/etc: directory

touch

功能说明

创建文件或刷新文件时间

语法

touch [OPTION]... FILE...

选项

-a 仅刷新文件的atime及ctime
-m 仅刷新文件的mtime及ctime
-t [[CC]YY]MMDDhhmm[.ss] 指定刷新文件的atime及mtime的时间戳
-c 如果文件不存在,则不予创建

实例

cp -rp /var/log/cron-20210228 /tmp/

# 查看cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-05-25 20:29:14.902541963 +0800
Modify: 2021-02-28 03:43:01.559588497 +0800
Change: 2021-03-17 16:33:30.474142243 +0800
 Birth: -
 
# 刷新文件的时间
touch cron-20210228
# 再次查看 cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:34:54.407097490 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:34:54.407097490 +0800
 Birth: -

# 仅刷新atime及ctime时间 
touch -a cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:42:56.627585679 +0800
 Birth: -
 
 # 仅刷新mtime及ctime时间
touch -m cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:44:13.308458381 +0800
Change: 2021-03-17 16:44:13.308458381 +0800
 Birth: -
 
 # 刷新atime及mtime为02282008的时间戳
touch -t 02282008 cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-28 20:08:00.000000000 +0800
Modify: 2021-02-28 20:08:00.000000000 +0800
Change: 2021-03-17 16:45:49.738555861 +0800
 Birth: -

cp

功能说明

复制文件或者目录

语法

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

选项

-a:此参数的效果和同时指定"-dpR"参数相同;
-d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录;
-f:强行复制文件或目录,不论目标文件或目录是否已存在;
-i:覆盖既有文件之前先询问用户;
-l:对源文件建立硬连接,而非复制文件;
-p:保留源文件或目录的属性;
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理;
-s:对源文件建立符号连接,而非复制文件;
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;
-b:覆盖已存在的文件目标前将目标文件备份;
-v:详细显示命令执行的操作

实例

# 备份当天/etc目录下所有的文件到/data目录下独立的子目录下,以当天的时间作为目录格式
cp -av /etc/ /data/backup_$(date +%F)

mv

功能说明

移动或重命名文件或目录
同一分区移动数据,速度很快,数据位置没有变化
不同分区移动数据,速度相对慢,数据位置发生了变化

语法

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

选项

-i 交互式
-f 强制
-b 目标存在,覆盖前先备份

实例

# 重命名cron-20210228为cron-20210228.bak
mv cron-20210228{,.bak}
stat cron-20210228.bak
  File: ‘cron-20210228.bak’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-28 20:08:00.000000000 +0800
Modify: 2021-02-28 20:08:00.000000000 +0800
Change: 2021-03-17 17:25:13.807838155 +0800
 Birth: -

rename

功能说明

修改文件名

语法

rename [options] expression replacement file...

实例

# 去掉.bak后缀
ll
total 68K
drwx------ 3 root root  17 Feb 24 16:11 systemd-private-10b7a38c856e4ee09c37e65fe7f2947d-chronyd.service-c9nfLK
-rw------- 1 root root 65K Feb 28 20:08 cron-20210228.bak
rename '.bak' '' *.bak
ll
total 68K
drwx------ 3 root root  17 Feb 24 16:11 systemd-private-10b7a38c856e4ee09c37e65fe7f2947d-chronyd.service-c9nfLK
-rw------- 1 root root 65K Feb 28 20:08 cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-28 20:08:00.000000000 +0800
Modify: 2021-02-28 20:08:00.000000000 +0800
Change: 2021-03-17 17:28:34.155516941 +0800
 Birth: -

rm

功能说明

删除文件

语法

rm [OPTION]... FILE...

选项

-i 交互式
-f 强制删除
-r 递归
--no-preserve-root 删除/

实例

# 删除cron-20210228
ll
total 68K
drwx------ 3 root root  17 Feb 24 16:11 systemd-private-10b7a38c856e4ee09c37e65fe7f2947d-chronyd.service-c9nfLK
-rw------- 1 root root 65K Feb 28 20:08 cron-20210228
rm cron-20210228
rm: remove regular file ‘cron-20210228’? y
ll
total 0
drwx------ 3 root root 17 Feb 24 16:11 systemd-private-10b7a38c856e4ee09c37e65fe7f2947d-chronyd.service-c9nfLK

复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符

cp -rp /etc/profile /tmp/
# 非交互式,直接在命令行
sed -i 's/^\s*//g' profile
# 使用vim,交互式
vim profile
# 在扩展模式下输入
:%s/^\s*//g

在vim中设置tab缩进为4个字符

# 修改全局模式里的vim配置文件,修改前先备份
cp /etc/vimrc /etc/vimrc.`date +"%F_%H-%M-%S"`
vim /etc/vimrc
# 添加如下设置
set tabstop=4
# 修改当前用户的vim配置文件,修改前先备份
cp ~/.vimrc ~/.vimrc.`date +"%F_%H-%M-%S"`
vim ~/.vimrc
# 添加如下设置
set tabstop=4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值