Linux文件的基本信息和常用命令

1、linux发行版的系统目录名称命名规则以及用途

目录名称 相应用途
/boot 存放用于系统引导时的各种文件
/bin 存放二进制可执行文件,所有用户使用的基本命令
/sbin· 存放二进制可执行文件,管理类的基本命令
/dev 设备文件及特殊文件存储位置
/etc 配置文件
/home 用户家目录
/lib 存放启动时程序依赖的基本共享库文件,以及内核模块文件
/lib64 存放专用于X86_64系统上的辅助共享库文件
/media 用于挂载设备文件的目录
/mnt 临时文件系统挂载点
/opt 第三方应用程序的安装位置
/proc 虚拟文件系统,例如系统内核、进程等
/root 系统管理员的家目录
/run 系统上运行的服务用到的数据
/sys 用于输出当前系统上硬件设备的相关信息的虚拟文件系统
/tmp 临时文件存储位置
/usr 通用共享文件
/var 用于存放运行时需要改变数据的文件,如日志
ls -al /
total 20
dr-xr-xr-x.  18 root root  236 Oct  9 11:01 .
dr-xr-xr-x.  18 root root  236 Oct  9 11:01 ..
lrwxrwxrwx.   1 root root    7 May 11  2019 bin -> usr/bin
dr-xr-xr-x.   6 root root 4096 Sep 22 09:35 boot
drwxr-xr-x.   3 root root  130 Oct 10 16:28 data
drwxr-xr-x.  19 root root 3200 Oct 10 20:18 dev
drwxr-xr-x.  80 root root 8192 Oct 10 20:18 etc
drwxr-xr-x.  10 root root  110 Oct 10 14:14 home
lrwxrwxrwx.   1 root root    8 Oct  9 11:01 lib -> /usr/lib
lrwxrwxrwx.   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 May 11  2019 media
drwxr-xr-x.   3 root root   18 Sep 22 09:34 mnt
drwxr-xr-x.   2 root root    6 May 11  2019 opt
dr-xr-xr-x. 166 root root    0 Oct 10 20:17 proc
dr-xr-x---.   3 root root  196 Oct 10 17:28 root
drwxr-xr-x.  23 root root  640 Oct 10 20:18 run
lrwxrwxrwx.   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 May 11  2019 srv
dr-xr-xr-x.  13 root root    0 Oct 10 20:17 sys
drwxrwxrwt.  15 root root 4096 Oct 10 20:28 tmp
drwxr-xr-x.  12 root root  144 Sep 22 09:32 usr

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

每个文件的属性信息,比如:文件的大小、时间、类型等,称为文件的元数据

ll -i /run/
total 20
30755 -rw-------.  1 root root    0 Oct 10 20:18 agetty.reload
25317 -rw-r--r--.  1 root root    4 Oct 10 20:17 auditd.pid
25292 drwxr-xr-x.  2 root root   40 Oct 10 20:17 console
24542 ----------.  1 root root    0 Oct 10 20:18 cron.reboot
25288 drwx------.  2 root root   40 Oct 10 20:17 cryptsetup
23960 drwxr-xr-x.  2 root root   60 Oct 10 20:17 dbus
25293 drwxr-xr-x.  2 root root   40 Oct 10 20:17 faillock
25531 drwxr-x---.  2 root root   40 Oct 10 20:18 firewalld
14606 drwxr-xr-x.  2 root root   60 Oct 10 20:17 fsck
19367 prw-------.  1 root root    0 Oct 10 20:17 initctl
  214 drwxr-xr-x.  4 root root  100 Oct 10 20:17 initramfs
14350 drwxr-xr-x.  3 root root   80 Oct 10 20:18 lock
14242 drwxr-xr-x.  3 root root   60 Oct 10 20:17 log
12937 drwxr-xr-x.  2 root root   40 Oct 10 20:17 mount
25538 drwxr-xr-x.  3 root root  100 Oct 10 20:18 NetworkManager
15631 drwxr-xr-x.  2 root root   40 Oct 10 20:18 plymouth
30747 -rw-------.  1 root root    4 Oct 10 20:18 rsyslogd.pid
25294 drwxr-xr-x.  2 root root   40 Oct 10 20:17 sepermit
25291 drwxr-xr-x.  2 root root   40 Oct 10 20:17 setrans
27874 -rw-r--r--.  1 root root    4 Oct 10 20:18 sshd.pid
26202 -rw-------.  1 root root    4 Oct 10 20:17 sssd.pid
25295 drwx--x--x.  3 root root   60 Oct 10 20:17 sudo
12381 drwxr-xr-x. 16 root root  400 Oct 10 20:18 systemd
14392 drwxr-xr-x.  2 root root   60 Oct 10 20:17 tmpfiles.d
25298 drwxr-xr-x.  2 root root   60 Oct 10 20:18 tuned
  249 drwxr-xr-x.  7 root root  160 Oct 10 20:18 udev
18868 drwxr-xr-x.  3 root root   60 Oct 10 20:18 user
18869 -rw-rw-r--.  1 root utmp 1536 Oct 10 20:18 utmp
25523 drwxr-xr-x.  2 root root   60 Oct 10 20:17 vmware

举例:

25294 drwxr-xr-x.  2 root root   40 Oct 10 20:17 sepermit
25294:inode号
d:文件类型
rwxr-xr-x:所有者、所有组、其他用户的读写执行权限
.:配置ACL后变+号
2:链接数,有两个不同的文件名引用相同的inode号
root root :所有者、所有组
40:文件大小
Oct 10 20:17:mtime,改变文件内容的时间
sepermit:文件名

inode:每个分区有inodes的总量
df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
devtmpfs         117659   401   117258    1% /dev
tmpfs            121863     1   121862    1% /dev/shm
tmpfs            121863   567   121296    1% /run
tmpfs            121863    17   121846    1% /sys/fs/cgroup
/dev/sda2      52428800 38761 52390039    1% /
/dev/sda1         65536   308    65228    1% /boot
/dev/sda3      26214400    16 26214384    1% /data
tmpfs            121863     5   121858    1% /run/user/0



文件类型:
-:普通文件
d:目录文件
b:块文件
c:字符文件
l:符号链接文件
p:管道文件
s:套接字文件

ACL:
未配置ACL:
----------. 2 root root 3055 Oct 10 21:30 abc
配置ACL:
setfacl -m u:test:wx /data/abc 
ll -a /data/abc 
-----wx---+ 2 root root 3055 Oct 10 21:30 /data/abc
getfacl /data/abc 
getfacl: Removing leading '/' from absolute path names
# file: data/abc
# owner: root
# group: root
user::---
user:test:-wx
group::---
mask::-wx
other::---
移除所有ACL:
setfacl -b /data/abc 
ll -a /data/abc 
----------. 2 root root 3055 Oct 10 21:30 /data/abc


修改文件的时间戳

stat查看文件状态
stat /data/abc 
  File: /data/abc
  Size: 3055      	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 140         Links: 2
Access: (0000/----------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-10-10 17:08:27.978545400 +0800
Modify: 2020-10-10 21:30:56.886005764 +0800
Change: 2020-10-10 22:35:56.475082409 +0800
 Birth: -
touch可用来修改文件时间
-a:改变atime和ctime
-m:改变mtime和ctime

touch -t 10102247.35 /data/abc 
stat /data/abc

  File: /data/abc
  Size: 3055      	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 140         Links: 2
Access: (0000/----------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-10-10 22:47:35.000000000 +0800
Modify: 2020-10-10 22:47:35.000000000 +0800
Change: 2020-10-10 22:48:19.468097012 +0800
 Birth: -

touch -d "2020-10-10 22:48:35" /data/abc 
stat /data/abc

  File: /data/abc
  Size: 3055      	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 140         Links: 2
Access: (0000/----------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-10-10 22:48:35.000000000 +0800
Modify: 2020-10-10 22:48:35.000000000 +0800
Change: 2020-10-10 22:49:42.728098649 +0800
 Birth: -

3、软链接和硬链接的区别

硬链接 软链接
本质是同一个文件 本质不是同一个文件
不支持跨设备 支持跨设备
相同inode 不同inode
链接数随链接数量变更 连接数不变
文件夹不支持 文件夹支持
原始文件相对路径是相对于当前工作目录 原始文件相对路径是相对于链接文件的相对路径
删除源文件,链接文件不影响 删除源文件,链接文件将无法访问
文件类型和源文件相同 文件类型为链接文件,和源文件无关

举例:

创建硬链接
创建前
ll -i /data/testdir/test.txt 
100663682 -rw-r--r--. 1 root root 10 Oct 11 08:16 /data/testdir/test.txt
创建后
ll -i !*
ll -i test.txt ../testdir2/f1.text
100663682 -rw-r--r--. 2 root root 10 Oct 11 08:16 ../testdir2/f1.text
100663682 -rw-r--r--. 2 root root 10 Oct 11 08:16 test.txt
删除源文件
ll -i ../testdir2/f1.text
100663682 -rw-r--r--. 1 root root 10 Oct 11 08:16 ../testdir2/f1.text
不支持跨设备
ln /data/testdir/f2.txt  /home/f2.txt
ln: failed to create hard link '/home/f2.txt' => '/data/testdir/f2.txt': Invalid cross-device link
文件夹不支持
ln /data/testdir /data/dir2
ln: /data/testdir: hard link not allowed for directory
软硬链接相对路径
硬链接
ln ../testdir2/f1.text /data/testdir/f2.txt
报错:ln: failed to access '../testdir2/f1.text': No such file or directory
软链接
ln -s ../testdir2/f1.text /data/testdir/f3.txt
ll -i -s /data/testdir2/f1.text /data/testdir/f3.txt
100663682 4 -rw-r--r--. 1 root root 10 Oct 11 08:16 /data/testdir2/f1.text
100663684 0 lrwxrwxrwx. 1 root root 19 Oct 11 08:45 /data/testdir/f3.txt -> ../testdir2/f1.tex

创建软链接
创建前
ll -i /data/testdir/f1.txt 
100663685 -rw-r--r--. 1 root root 0 Oct 11 08:51 /data/testdir/f1.txt
创建后
ln -s ../data/testdir/f1.txt /home/f5.txt
l -i -s /data/testdir/f1.txt /home/f5.txt
100663685 0 -rw-r--r--. 1 root root  0 Oct 11 08:51 /data/testdir/f1.txt
   401170 0 lrwxrwxrwx. 1 root root 22 Oct 11 08:53 /home/f5.txt -> ../data/testdir/f1.t
   删除源文件后软连接失效
cat /home/f5.txt 
cat: /home/f5.txt: No such file or directory

4、Linux上常用的文件管理类命令

pwd:显示当前位置
pwd
/etc/sysconfig/network-scripts

基名:basename /etc/sysconfig/network-scripts/
network-scripts
目录名:
dirname /etc/sysconfig/network-scripts/
/etc/sysconfig

cd
cd    目录位置
cd    切换至当前用户的家目录(cd ~)
cd .. 切换至父目录
cd -  切换至上一个目录位置

ls
s -al /run/ 显示/run目录下的隐藏文件和额外信息
total 20
drwxr-xr-x. 23 root root  640 Oct 11 07:56 .
dr-xr-xr-x. 18 root root  236 Oct  9 11:01 ..
-rw-------.  1 root root    0 Oct 11 07:56 agetty.reload
-rw-r--r--.  1 root root    4 Oct 11 07:55 auditd.pid
drwxr-xr-x.  2 root root   40 Oct 11 07:55 console
----------.  1 root root    0 Oct 11 07:56 cron.reboot
drwx------.  2 root root   40 Oct 11 07:55 cryptsetup
drwxr-xr-x.  2 root root   60 Oct 11 07:55 dbus
drwxr-xr-x.  2 root root   40 Oct 11 07:55 faillock
drwxr-x---.  2 root root   40 Oct 11 07:56 firewalld
drwxr-xr-x.  2 root root   60 Oct 11 07:55 fsck
srw-rw-rw-.  1 root root    0 Oct 11 07:55 .heim_org.h5l.kcm-socket
prw-------.  1 root root    0 Oct 11 07:55 initctl
drwxr-xr-x.  4 root root  100 Oct 11 07:55 initramfs
drwxr-xr-x.  3 root root   80 Oct 11 07:56 lock
drwxr-xr-x.  3 root root   60 Oct 11 07:55 log
drwxr-xr-x.  2 root root   40 Oct 11 07:55 mount
drwxr-xr-x.  3 root root  100 Oct 11 07:56 NetworkManager
drwxr-xr-x.  2 root root   40 Oct 11 07:56 plymouth
-rw-------.  1 root root    4 Oct 11 07:56 rsyslogd.pid
drwxr-xr-x.  2 root root   40 Oct 11 07:55 sepermit
drwxr-xr-x.  2 root root   40 Oct 11 07:55 setrans
-rw-r--r--.  1 root root    4 Oct 11 07:56 sshd.pid
-rw-------.  1 root root    4 Oct 11 07:56 sssd.pid
drwx--x--x.  3 root root   60 Oct 11 07:55 sudo
drwxr-xr-x. 16 root root  400 Oct 11 07:57 systemd
drwxr-xr-x.  2 root root   60 Oct 11 07:55 tmpfiles.d
drwxr-xr-x.  2 root root   60 Oct 11 07:56 tuned
drwxr-xr-x.  7 root root  160 Oct 11 07:56 udev
drwxr-xr-x.  3 root root   60 Oct 11 07:57 user
-rw-rw-r--.  1 root utmp 1536 Oct 11 07:57 utmp
drwxr-xr-x.  2 root root   60 Oct 11 07:56 vmware

s -dl /run/  显示/run目录文件信息,不进去
drwxr-xr-x. 23 root root 640 Oct 11 07:56 /run/

ls -R /run 显示/run目录下的所有文件信息和内部目录下的所有文件信息

ls -Sl /run 按照文件大小排序
total 20
-rw-rw-r--.  1 root utmp 1536 Oct 11 07:57 utmp
drwxr-xr-x. 16 root root  400 Oct 11 07:57 systemd
drwxr-xr-x.  7 root root  160 Oct 11 07:56 udev
drwxr-xr-x.  4 root root  100 Oct 11 07:55 initramfs
drwxr-xr-x.  3 root root  100 Oct 11 07:56 NetworkManager
drwxr-xr-x.  3 root root   80 Oct 11 07:56 lock
drwxr-xr-x.  2 root root   60 Oct 11 07:55 dbus
drwxr-xr-x.  2 root root   60 Oct 11 07:55 fsck
drwxr-xr-x.  3 root root   60 Oct 11 07:55 log
drwx--x--x.  3 root root   60 Oct 11 07:55 sudo
drwxr-xr-x.  2 root root   60 Oct 11 07:55 tmpfiles.d
drwxr-xr-x.  2 root root   60 Oct 11 07:56 tuned
drwxr-xr-x.  3 root root   60 Oct 11 07:57 user
drwxr-xr-x.  2 root root   60 Oct 11 07:56 vmware
drwxr-xr-x.  2 root root   40 Oct 11 07:55 console
drwx------.  2 root root   40 Oct 11 07:55 cryptsetup
drwxr-xr-x.  2 root root   40 Oct 11 07:55 faillock
drwxr-x---.  2 root root   40 Oct 11 07:56 firewalld
drwxr-xr-x.  2 root root   40 Oct 11 07:55 mount
drwxr-xr-x.  2 root root   40 Oct 11 07:56 plymouth
drwxr-xr-x.  2 root root   40 Oct 11 07:55 sepermit
drwxr-xr-x.  2 root root   40 Oct 11 07:55 setrans
-rw-r--r--.  1 root root    4 Oct 11 07:55 auditd.pid
-rw-------.  1 root root    4 Oct 11 07:56 rsyslogd.pid
-rw-r--r--.  1 root root    4 Oct 11 07:56 sshd.pid
-rw-------.  1 root root    4 Oct 11 07:56 sssd.pid
-rw-------.  1 root root    0 Oct 11 07:56 agetty.reload
----------.  1 root root    0 Oct 11 07:56 cron.reboot
prw-------.  1 root root    0 Oct 11 07:55 initctl

stat 查看文件状态
stat /etc/passwd
  File: /etc/passwd
  Size: 1555      	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 134882152   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2020-10-10 14:14:56.611865173 +0800
Modify: 2020-10-10 14:14:56.591865172 +0800
Change: 2020-10-10 14:14:56.593865172 +0800
 Birth: -

file 查看文件类型
file /etc/passwd
/etc/passwd: ASCII text

touch 创建文件和刷新时间

cp 复制文件和目录
-r 递归

mv 移动文件和目录
-r 递归
-f 强制

rm 删除文件和目录
-r 递归
-f 强制

tree 树路径
tree /data
/data
├── abc
├── all.log
├── all.text
├── a.php
├── linux.txt
├── num.txt
├── shadow
├── testdir
│   ├── dir3
│   ├── dir4
│   ├── dir5
│   │   ├── dir6
│   │   └── dir7
│   ├── f2.txt
│   └── f3.txt -> ../testdir2/f1.text
├── testdir2
│   └── f1.text
└── windows.txt


mkdir 创建目录

rmkdir 删除目录

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

##cp /etc/profile /tmp/
##cat /tmp/profile | tr -d  ' '
cp /etc/profile /tmp/
vim /etc/profile /tmp/
:%s/^[[:blank:]]\+//g

6、在vim中设置tab缩减为4个字符

:set ts=4    

猜你喜欢

转载自blog.csdn.net/weixin_50904580/article/details/109003861