xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux file system All In One

Linux file system All In One

图解 Linux 文件系统

image

image

image

每一行总共会有10个 -符号:

第一个符号表示文件类型,如该文件是文件(-表示),目录(d表示), 链接文件(l表示);
后面 9个符号按照 3 个符号一组分成 3 组,如:

$ ls test.sh
-rwxrwx--- 1 eric eric    0 10月  8 17:21 test.sh

-rwxrwx--- 770 权限
表示此文件拥有者同组用户具有对文件进行读``写执行的权限,其他用户组没有任何权限

rwx 第一组表示所有者权限;
rwx第二组表示同组用户权限
---最后一组表示其他用户权限

以上的其他用户,不包括root这个 super user

file type

- file / 普通文件
d directory / 目录
b block / 设备
l symbol link / 链接文件 (soft symbol link / hard symbol link)
c character / 字符设备

file permissions

3 个符号一组,共三组;

  1. user permission 用户权限
  2. group permission 组权限
  3. other permission 其他权限

u === user
g === group
o === other
a === all (u + g + o)

+ === add
- === remove
= === set

每组符号表示的含义(权重):

  • w === 4
  • r === 2
  • x === 1

可能的组合,枚举值

  • wrx === 7
  • wr- === 6
  • w-x === 5
  • w-- === 4
  • -rx === 3
  • -r- === 2
  • --x === 1
  • --- === 0

chmod 777
chmod 755
chmod +x

wrxw-xw-x === 755

$ touch ./test.sh

eric@rpi4b:~/Desktop/linux-file-system $ ls -al
总用量 8
drwxr-xr-x 2 eric eric 4096 10月  8 17:21 .
drwxr-xr-x 6 eric eric 4096 10月  8 17:21 ..
-rw-r--r-- 1 eric eric    0 10月  8 17:21 test.sh
# 等价于
$ chmod 755 ./test.sh
# 755, 默认当前登录用户、默认组、默认其他
$ chmod +x ./test.sh

# 等价于
$ chmod a+x ./test.sh

# 等价于
$ chmod u+x ./test.sh
$ chmod g+x ./test.sh
$ chmod o+x ./test.sh
# the a stands for "all", the + for "add", and the r for "read".
$ chmod a+r file.sh

chown

chown - change file owner and group

$ chown name filename
$ chown name foldername

# chown
$ man chown > man-chown.md

# recursively 递归
$ chown -R

To change permissions recursively in all subdirectories below the specified directory, add the -R option;
递归更改指定目录下所有子目录的权限,请添加 -R 选项;

$ mkdir folder_test
$ chown -R folder_test

shell script to change directory permissions

changing ownership in Linux

??? ch dir / change folder ???

#!/bin/bash

source /generic/utils/etc/environments/perm.conf

cd $ENVR
DIRS=`ls -l $ENVR | egrep '^d' | awk '{print $9}'`

for DIR in "${DIRS[@]}";
do
    echo "$DIR"
        echo "Which environment do you want?: "
        echo -n "> "
        read i
echo "Changing permissions now..."

sudo chown -R $OWN:$GRP "$i" && sudo chmod -R $MOD1 "$i"
#cd $ENVR/$i
#sudo chmod -R $MOD2 *

echo "Permissions are changed!"

done

chgrp

change groups of files and directories in Linux

$ chgrp groupname filename
$ chgrp groupname foldername

Note that the group must exit before you can assign groups to files and directories.

demos

system environment variables

# global variable
$ export NVM_USER=xgqfrms_pi4b
$ echo $NVM_USER
xgqfrms_pi4b
$ touch test.sh
$ vim test.sh
$ sudo chmod +x ./test.sh
#!/usr/bin/env bash

echo $NVM_USER
echo "string with a variable $NVM_USER"
echo string with a variable $NVM_USER
echo "string with a variable ${NVM_USER}"
# hard link
$ ln test.sh test-hard-link.sh
# soft link
$ ln -s test.sh test-soft-link.sh

$ ls -al
总用量 152
drwxr-xr-x 17 eric eric  4096 10月  8 16:45 .
drwxr-xr-x  4 root root  4096  9月 19 15:51 ..
-rw-------  1 eric eric 16213 10月  8 12:51 .bash_history
-rw-r--r--  1 eric eric   220  5月  3 10:53 .bash_logout
-rw-r--r--  1 eric eric  3912  9月 21 13:08 .bashrc
drwxr-xr-x  2 eric eric  4096  5月  3 11:02 Bookshelf
drwxr-xr-x  6 eric eric  4096  9月 20 22:55 .cache
drwx------  6 eric eric  4096  5月  3 11:24 .config
drwxr-xr-x  4 eric eric  4096 10月  6 23:16 Desktop
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Documents
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Downloads
drwxr-xr-x  3 eric eric  4096  5月  3 11:02 .local
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Music
-rw-------  1 eric eric   111 10月  7 15:37 .node_repl_history
drwxr-xr-x  5 eric eric  4096 10月  7 01:27 .npm
drwxr-xr-x  8 eric eric  4096  9月 21 11:01 .nvm
-rw-r--r--  1 eric eric   411  9月 21 11:37 nvm_echo_test.sh
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Pictures
-rw-r--r--  1 eric eric   807  5月  3 10:53 .profile
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Public
drwxrwxrwx  2 eric eric  4096  9月 16 04:21 Share
-rwxr-xr-x  1 eric eric   172  9月 21 18:03 shell-env-variable-test.sh
-rwxr-xr-x  1 eric eric   196  9月 21 16:58 shell-variable.sh
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Templates
-rwxr-xr-x  2 eric eric   156  9月 21 13:15 test-hard-link.sh
-rwxr-xr-x  2 eric eric   156  9月 21 13:15 test.sh
lrwxrwxrwx  1 eric eric     7 10月  8 16:44 test-soft-link.sh -> test.sh
drwxr-xr-x  2 eric eric  4096  5月  3 11:24 Videos
-rw-------  1 eric eric 19119 10月  7 20:47 .viminfo
-rw-------  1 eric eric    50  9月 30 11:48 .Xauthority
-rw-------  1 eric eric  2604  9月 30 11:48 .xsession-errors
-rw-------  1 eric eric  2604  9月 25 22:17 .xsession-errors.old
eric@rpi4b:~ $ 


image

$ ls -alh
总用量 152K
drwxr-xr-x 17 eric eric 4.0K 10月  8 16:45 .
drwxr-xr-x  4 root root 4.0K  9月 19 15:51 ..
-rw-------  1 eric eric  16K 10月  8 12:51 .bash_history
-rw-r--r--  1 eric eric  220  5月  3 10:53 .bash_logout
-rw-r--r--  1 eric eric 3.9K  9月 21 13:08 .bashrc
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:02 Bookshelf
drwxr-xr-x  6 eric eric 4.0K  9月 20 22:55 .cache
drwx------  6 eric eric 4.0K  5月  3 11:24 .config
drwxr-xr-x  4 eric eric 4.0K 10月  6 23:16 Desktop
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Documents
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Downloads
drwxr-xr-x  3 eric eric 4.0K  5月  3 11:02 .local
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Music
-rw-------  1 eric eric  111 10月  7 15:37 .node_repl_history
drwxr-xr-x  5 eric eric 4.0K 10月  7 01:27 .npm
drwxr-xr-x  8 eric eric 4.0K  9月 21 11:01 .nvm
-rw-r--r--  1 eric eric  411  9月 21 11:37 nvm_echo_test.sh
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Pictures
-rw-r--r--  1 eric eric  807  5月  3 10:53 .profile
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Public
drwxrwxrwx  2 eric eric 4.0K  9月 16 04:21 Share
-rwxr-xr-x  1 eric eric  172  9月 21 18:03 shell-env-variable-test.sh
-rwxr-xr-x  1 eric eric  196  9月 21 16:58 shell-variable.sh
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Templates
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test-hard-link.sh
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test.sh
lrwxrwxrwx  1 eric eric    7 10月  8 16:44 test-soft-link.sh -> test.sh
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Videos
-rw-------  1 eric eric  19K 10月  7 20:47 .viminfo
-rw-------  1 eric eric   50  9月 30 11:48 .Xauthority
-rw-------  1 eric eric 2.6K  9月 30 11:48 .xsession-errors
-rw-------  1 eric eric 2.6K  9月 25 22:17 .xsession-errors.old

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

soft link vs hard link / 软链接 vs 硬链接

软链接,是创建一个新的链接文件(内容为空), 链接到原文件(指向原文件),文件变小 7Bytes,创建时间更新;
硬链接,是创建一个全新的原文件副本(内容完全复制),文件大小不变 156Bytes,创建时间不变;

lrwxrwxrwx  1 eric eric    7 10月  8 16:44 test-soft-link.sh -> test.sh
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test-hard-link.sh
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test.sh

# 按时间排序 (逆序)
$ ls -alht
总用量 152K
drwxr-xr-x 17 eric eric 4.0K 10月  8 16:45 .
lrwxrwxrwx  1 eric eric    7 10月  8 16:44 test-soft-link.sh -> test.sh
-rw-------  1 eric eric  16K 10月  8 12:51 .bash_history
-rw-------  1 eric eric  19K 10月  7 20:47 .viminfo
-rw-------  1 eric eric  111 10月  7 15:37 .node_repl_history
drwxr-xr-x  5 eric eric 4.0K 10月  7 01:27 .npm
drwxr-xr-x  4 eric eric 4.0K 10月  6 23:16 Desktop
-rw-------  1 eric eric 2.6K  9月 30 11:48 .xsession-errors
-rw-------  1 eric eric   50  9月 30 11:48 .Xauthority
-rw-------  1 eric eric 2.6K  9月 25 22:17 .xsession-errors.old
-rwxr-xr-x  1 eric eric  172  9月 21 18:03 shell-env-variable-test.sh
-rwxr-xr-x  1 eric eric  196  9月 21 16:58 shell-variable.sh
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test-hard-link.sh
-rwxr-xr-x  2 eric eric  156  9月 21 13:15 test.sh
-rw-r--r--  1 eric eric 3.9K  9月 21 13:08 .bashrc
-rw-r--r--  1 eric eric  411  9月 21 11:37 nvm_echo_test.sh
drwxr-xr-x  8 eric eric 4.0K  9月 21 11:01 .nvm
drwxr-xr-x  6 eric eric 4.0K  9月 20 22:55 .cache
drwxr-xr-x  4 root root 4.0K  9月 19 15:51 ..
drwxrwxrwx  2 eric eric 4.0K  9月 16 04:21 Share
drwx------  6 eric eric 4.0K  5月  3 11:24 .config
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Documents
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Downloads
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Music
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Pictures
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Public
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Templates
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:24 Videos
drwxr-xr-x  2 eric eric 4.0K  5月  3 11:02 Bookshelf
drwxr-xr-x  3 eric eric 4.0K  5月  3 11:02 .local
-rw-r--r--  1 eric eric  220  5月  3 10:53 .bash_logout
-rw-r--r--  1 eric eric  807  5月  3 10:53 .profile

demo

$ ls -alith
$ ls -i
$ ls -il
$ ls -ia
$ ls -ila
# i => inode ✅
eric@rpi4b:~ $ ls -ali | grep test
 24553 -rw-r--r--  1 eric eric   411  9月 21 11:37 nvm_echo_test.sh
 30221 -rwxr-xr-x  1 eric eric   172  9月 21 18:03 shell-env-variable-test.sh
 24592 -rwxr-xr-x  2 eric eric   156  9月 21 13:15 test-hard-link.sh
 24592 -rwxr-xr-x  2 eric eric   156  9月 21 13:15 test.sh
 23662 lrwxrwxrwx  1 eric eric     7 10月  8 16:44 test-soft-link.sh -> test.sh
eric@rpi4b:~ $ 

https://www.imooc.com/video/7948

# -ef => --equal --file
# 文件相等比较,检测文件是否是一个软链接 ✅
[ ./student.sh -ef ./soft ] && [ -L ./soft ] && echo yes || echo no

# 文件相等比较,检测文件是否是一个硬链接 ✅
[ ./student.sh -ef ./hard ] && [ -L ./hard ] && echo yes || echo no

https://github.com/xgqfrms/linux-shell-script-programming/assets/7291672/94ce2289-e74a-4f69-a33a-54dee716ae04

https://www.imooc.com/qadetail/337388

refs

https://www.pluralsight.com/blog/it-ops/linux-file-permissions
https://kb.iu.edu/d/abdb

https://web-full-stack.xgqfrms.xyz/
https://www.cnblogs.com/xgqfrms/p/9546961.html
http://www.imooc.com/article/4777



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-10-08 17:12  xgqfrms  阅读(3)  评论(2编辑  收藏  举报