Post

linux(CentOS7) 目录结构

linux 目录结构

目录 功能说明
/ 根目录,整个文件系统的起点,包含所有其他目录和文件
/bin 存放基本的用户命令,如 lscpmvrm
/boot 启动加载器相关的文件,如内核镜像(vmlinuz)、初始化 RAM 磁盘镜像(initrd)
/dev 设备文件目录,包含各种设备的接口文件
/etc 系统配置文件和脚本,如网络配置文件、服务配置文件、用户账户信息等
/home 用户主目录,如 /home/username,每个用户的个人文件存放于此
/lib 系统程序使用的共享库文件和内核模块
/media 挂载点,用于临时挂载可移动媒体,如 CD-ROM、USB 存储设备
/mnt 挂载点,用于临时挂载文件系统
/opt 用于安装附加软件包或应用程序
/proc 虚拟文件系统,提供系统进程和内核信息,如 /proc/cpuinfo/proc/meminfo
/root 超级用户(root 用户)的主目录
/run 存放系统运行时的数据,如进程 ID 文件和套接字文件
/sbin 系统管理命令和守护进程程序,如 ifconfigrebootshutdown
/srv 存放特定服务相关的数据,如 HTTP、FTP 服务的数据
/sys 虚拟文件系统,提供与硬件和内核模块相关的信息和接口
/tmp 临时文件目录,系统重启后通常会清空
/usr 用户级程序和数据,如二进制文件(/usr/bin)、库文件(/usr/lib)、头文件(/usr/include)、文档(/usr/share/doc)等
/var 可变数据文件,如日志文件(/var/log)、缓存文件(/var/cache)、邮件(/var/mail)等

常用命令

cp

1
2
3
4
5
[root@Cc RedHad]#  cp /etc/nginx/conf.d/default.conf .
[root@Cc RedHad]# ll
总用量 4
-rw-r--r-- 1 root root 750 3月  24 22:17 default.conf
[root@Cc RedHad]#

cp –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@Cc RedHad]# cp --help;
用法:cp [选项]... [-T] 源文件 目标文件
 或:cp [选项]... 源文件... 目录
 或:cp [选项]... -t 目录 源文件...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                 等于-dR --preserve=all
      --attributes-only 仅复制属性而不复制数据      --backup[=CONTROL           为每个已存在的目标文件创建备份
  -b                            类似--backup 但不接受参数
      --copy-contents           在递归处理是复制特殊文件内容
  -d                            等于--no-dereference --preserve=links
  -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)
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber              不要覆盖已存在的文件(使前面的 -i 选项失效)
  -P, --no-dereference          不跟随源文件中的符号链接
  -p                            等于--preserve=模式,所有权,时间戳
      --preserve[=属性列表      保持指定的属性(默认:模式,所有权,时间戳),如果
                                        可能保持附加属性:环境、链接、xattr 等
  -c                           deprecated, same as --preserve=context
      --sno-preserve=属性列表   不保留指定的文件属性
      --parents                 复制前在目标目录创建来源文件路径中的所有目录
  -R, -r, --recursive           递归复制目录及其子目录内的所有内容
      --reflink[=WHEN]          控制克隆/CoW 副本。请查看下面的内如。
      --remove-destination      尝试打开目标文件前先删除已存在的目的地
                                        文件 (相对于 --force 选项)
      --sparse=WHEN             控制创建稀疏文件的方式
      --strip-trailing-slashes  删除参数中所有源文件/目录末端的斜杠
  -s, --symbolic-link           只创建符号链接而不复制文件
  -S, --suffix=后缀             自行指定备份文件的后缀
  -t,  --target-directory=目录  将所有参数指定的源文件/目录
                                           复制至目标目录
  -T, --no-target-directory     将目标目录视作普通文件
  -u, --update                  只在源文件比目标文件新,或目标文件
                                        不存在时才进行复制
  -v, --verbose         显示详细的进行步骤
  -x, --one-file-system 不跨越文件系统进行操作
  -Z                           set SELinux security context of destination
                                 file to default type
      --context[=CTX]          like -Z, or if CTX is specified then set the
                                 SELinux or SMACK security context to CTX
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

默认情况下,源文件的稀疏性仅仅通过简单的方法判断,对应的目标文件目标文件也
被为稀疏。这是因为默认情况下使用了--sparse=auto 参数。如果明确使用
--sparse=always 参数则不论源文件是否包含足够长的0 序列也将目标文件创文
建为稀疏件。
使用--sparse=never 参数禁止创建稀疏文件。

当指定了--reflink[=always] 参数时执行轻量化的复制,即只在数据块被修改的
情况下才复制。如果复制失败或者同时指定了--reflink=auto,则返回标准复制模式。

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

操作示例

1
2
3
4
5
6
7
8
9
10
11
[root@Cc cp]# tree
.
├── file1
│   └── file1.txt
├── file2
│   ├── file2.exe
│   └── file2.txt
└── file3

3 directories, 3 files
[root@Cc cp]#
复制多个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@Cc cp]# cp -v file1/file1.txt  file2/file2.txt file3
"file1/file1.txt" -> "file3/file1.txt"
"file2/file2.txt" -> "file3/file2.txt"
[root@Cc cp]# tree
.
├── file1
│   └── file1.txt
├── file2
│   ├── file2.exe
│   └── file2.txt
└── file3
    ├── file1.txt
    └── file2.txt

3 directories, 5 files
[root@Cc cp]#

复制文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@Cc cp]# cp -rv file3 file5
[root@Cc cp]# tree
.
├── file1
│   └── file1.txt
├── file2
│   ├── file2.exe
│   └── file2.txt
├── file3
│   ├── file1.txt
│   └── file2.txt
└── file5
    └── file3
        ├── file1.txt
        └── file2.txt

5 directories, 7 files
[root@Cc cp]#

保留属性复制
1
2
3
4
5
6
7
8
9
[root@Cc cp]# cp -a /etc/nginx/conf.d/default.conf .
[root@Cc cp]# ll
总用量 20
-rw-r--r-- 1 root root  750 3月  23 11:51 default.conf
drwxr-xr-x 2 root root 4096 3月  24 22:21 file1
drwxr-xr-x 2 root root 4096 3月  24 22:22 file2
drwxr-xr-x 2 root root 4096 3月  24 22:23 file3
drwxr-xr-x 3 root root 4096 3月  24 22:25 file5

并不复制本身,而是创建软链接

注释:修改软链接文件 本身文件也跟着改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@Cc cp]# ll
总用量 20
-rw-r--r-- 1 root root  750 3月  23 11:51 default.conf
drwxr-xr-x 2 root root 4096 3月  24 22:21 file1
drwxr-xr-x 2 root root 4096 3月  24 22:22 file2
drwxr-xr-x 2 root root 4096 3月  24 22:23 file3
drwxr-xr-x 3 root root 4096 3月  24 22:25 file5
lrwxrwxrwx 1 root root   12 3月  24 22:31 testCP_S -> default.conf
[root@Cc cp]# cat testCP_S
server {
      listen 443 ssl default;  # 1.1版本后这样写
      server_name lichaocheng.top; #填写绑定证书的域名
      ssl_certificate  /opt/lichaocheng.top/lichaocheng.top/sll/lichaocheng.top.pem;  # 指定证书的位置,绝对路径
      ssl_certificate_key /opt/lichaocheng.top/lichaocheng.top/sll/lichaocheng.top.key;  # 绝对路径,同上
      ssl_session_timeout 5m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
      ssl_prefer_server_ciphers on;
      location / {
          root   /opt/lichaocheng.top/lichaocheng.top; #站点目录,绝对路径
          index  index.html index.htm;
    }
}
[root@Cc cp]# cat default.conf
server {
      listen 443 ssl default;  # 1.1版本后这样写
      server_name lichaocheng.top; #填写绑定证书的域名
      ssl_certificate  /opt/lichaocheng.top/lichaocheng.top/sll/lichaocheng.top.pem;  # 指定证书的位置,绝对路径
      ssl_certificate_key /opt/lichaocheng.top/lichaocheng.top/sll/lichaocheng.top.key;  # 绝对路径,同上
      ssl_session_timeout 5m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
      ssl_prefer_server_ciphers on;
      location / {
          root   /opt/lichaocheng.top/lichaocheng.top; #站点目录,绝对路径
          index  index.html index.htm;
    }
}
[root@Cc cp]#

mv

mv –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@Cc cp]# mv --help
用法:mv [选项]... [-T] 源文件 目标文件
 或:mv [选项]... 源文件... 目录
 或:mv [选项]... -t 目录 源文件...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       为每个已存在的目标文件创建备份
  -b                           类似--backup 但不接受参数
  -f, --force                  覆盖前不询问
  -i, --interactive            覆盖前询问
  -n, --no-clobber             不覆盖已存在文件
如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
      --strip-trailing-slashes  去掉每个源文件参数尾部的斜线
  -S, --suffix=SUFFIX           替换常用的备份文件后缀
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出
  none, off       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份
[root@Cc cp]#

touch

touch –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@Cc cp]# touch --help;
用法:touch [选项]... 文件...
  -a                    只更改访问时间
  -c, --no-create       不创建任何文件
  -d, --date=字符串     使用指定字符串表示时间而非当前时间
  -f                    (忽略)
  -h, --no-dereference          会影响符号链接本身,而非符号链接所指示的目的地
                                (当系统支持更改符号链接的所有者时,此选项才有用)
  -m                    只更改修改时间
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

请注意,-d 和-t 选项可接受不同的时间/日期格式。

示例

时间格式[CCYYMMDDhhmm]touch -t "01231215" t3

  • CC 年份前两位
  • YY 年份后两位
  • MM 月
  • DD 天
  • hh 时
  • mm 分

    一位数 0 补全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@Cc touch]# touch t1
[root@Cc touch]# stat t1
  文件:"t1"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd01h/64769d      Inode:668181      硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2020-03-24 22:44:43.908405308 +0800
最近更改:2020-03-24 22:44:43.908405308 +0800
最近改动:2020-03-24 22:44:43.908405308 +0800
创建时间:-
[root@Cc touch]# date
2020年 03月 24日 星期二 22:49:18 CST
[root@Cc touch]#
[root@Cc touch]#
[root@Cc touch]# touch -d "2 day ago" t1
[root@Cc touch]# stat t1
  文件:"t1"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd01h/64769d      Inode:668185      硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2020-03-22 22:49:50.941398201 +0800
最近更改:2020-03-22 22:49:50.941398201 +0800
最近改动:2020-03-24 22:49:50.941121499 +0800
创建时间:-
[root@Cc touch]# date
2020年 03月 24日 星期二 22:50:01 CST
[root@Cc touch]# touch -t "01231215" t3
[root@Cc touch]# stat t3
  文件:"t3"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd01h/64769d      Inode:668187      硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2020-01-23 12:15:00.000000000 +0800
最近更改:2020-01-23 12:15:00.000000000 +0800
最近改动:2020-03-24 22:50:53.180699023 +0800
创建时间:-
[root@Cc touch]#

rm

rm –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@Cc touch]# rm --help;
用法:rm [选项]... 文件...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system         递归删除一个层级时,跳过所有不符合命令行参
                                数的文件系统上的文件
      --no-preserve-root  do not treat '/' specially
      --preserve-root   do not remove '/' (default)
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

默认时,rm 不会删除目录。使用--recursive(-r 或-R)选项可删除每个给定
的目录,以及其下所有的内容。

To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

请注意,如果使用rm 来删除文件,通常仍可以将该文件恢复原状。如果想保证
该文件的内容无法还原,请考虑使用shred。

查看文件

  • cat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@Cc touch]# cat --help
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。

  -A, --show-all           等于-vET
  -b, --number-nonblank    对非空输出行编号
  -e                       等于-vE
  -E, --show-ends          在每行结束处显示"$"
  -n, --number             对输出的所有行编号
  -s, --squeeze-blank      不输出多行空行
  -t                       与-vT 等价
  -T, --show-tabs          将跳格字符显示为^I
  -u                       (被忽略)
  -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

如果没有指定文件,或者文件为"-",则从标准输入读取。

示例:
  cat f - g  先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
  cat        将标准输入的内容复制到标准输出。
  • tac
1
2
3
4
5
6
7
8
9
10
11
[root@Cc touch]# tac --help
用法:tac [选项]... [文件]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --before                  在行前而非行尾添加分隔标志
  -r, --regex                   将分隔标志视作正则表达式来解析
  -s, --separator=字符串        使用指定字符串代替换行作为分隔标志
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出
  • more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@Cc touch]# more --help
more: 未知选项 -help
用法:more [选项] 文件...

选项:
  -d        显示帮助,而不是响铃
  -f        统计逻辑行数而不是屏幕行数
  -l        抑制换页(form feed)后的暂停
  -p        不滚屏,清屏并显示文本
  -c        不滚屏,显示文本并清理行尾
  -u        抑制下划线
  -s        将多个空行压缩为一行
  -NUM      指定每屏显示的行数为 NUM
  +NUM      从文件第 NUM 行开始显示
  +/STRING  从匹配搜索字符串 STRING 的文件位置开始显示
  -V        输出版本信息并退出
[root@Cc touch]#

  • less
1
这个好长
  • tac
1
2
3
4
5
6
7
[root@Cc touch]# tac --help
用法:tac [选项]... [文件]...
  -b, --before                  在行前而非行尾添加分隔标志
  -r, --regex                   将分隔标志视作正则表达式来解析
  -s, --separator=字符串        使用指定字符串代替换行作为分隔标志
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出
  • tail
1
2
3
4
5
[root@Cc touch]# tail -n 3 ../cp/default.conf
          index  index.html index.htm;
    }
}
// 最后三行

find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@Cc ~]# find --help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

默认路径为当前目录;默认表达式为 -print
表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:

操作符 (优先级递减;未做任何指定时默认使用 -and):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

比较测试 (N 可以是 +N 或 -N 或 N): -amin N -anewer FILE -atime N -cmin N
      -cnewer 文件 -ctime N -empty -false -fstype 类型 -gid N -group 名称
      -ilname 匹配模式 -iname 匹配模式 -inum N -ipath 匹配模式 -iregex 匹配模式
      -links N -lname 匹配模式 -mmin N -mtime N -name 匹配模式 -newer 文件
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context 文本


操作: -delete -print0 -printf 格式 -fprintf 文件 格式 -print
      -fprint0 文件 -fprint 文件 -ls -fls 文件 -prune -quit
      -exec 命令 ; -exec 命令 {} + -ok 命令 ;
      -execdir 命令 ; -execdir 命令 {} + -okdir 命令 ;

文本文本操作

  • grep 过滤
  • diff 比较文件差异
  • ln 文件目录之间创建链接
  • file [文件或者文件夹] 显示类型
  • split 分割
  • join 合并
  • umask 文件权限
把权限拿出来细讲下

unmask 范围[000~777]

  • r - 4 - 读
  • w - 2 - 写
  • x - 1 - 执行
计算如下
  • 最大权限 777 读、写、执行权限
  • 当前环境 umask 值 ,系统为 002
  • 对于当前目录,根据互补原则目录权限 755。而文件默认没有执行权限,最大 666,对应的权限 644

    文件默认权限 666-022=644,目录默认权限 777-022=755

1
2
3
4
5
6
7
8
[root@Cc nmask]# umask
0022
[root@Cc nmask]# touch file;mkdir dir
[root@Cc nmask]# ls -l
总用量 4
drwxr-xr-x 2 root root 4096 3月  24 23:28 dir
-rw-r--r-- 1 root root    0 3月  24 23:28 file
[root@Cc nmask]#

目录管理

This post is licensed under CC BY 4.0 by the author.