Linux基础及命令(二)

90 阅读13分钟

Linux 7大文件属性

[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 2014 3  28 2024 anaconda-ks.cfg
-rw-r--r--. 1 root root 2062 3  28 2024 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 3  28 2024 公共
drwxr-xr-x. 2 root root    6 3  28 2024 模板
drwxr-xr-x. 2 root root    6 3  28 2024 视频
drwxr-xr-x. 2 root root    6 3  28 2024 图片
drwxr-xr-x. 2 root root    6 3  28 2024 文档
drwxr-xr-x. 2 root root    6 3  28 2024 下载
drwxr-xr-x. 2 root root    6 3  28 2024 音乐
drwxr-xr-x. 2 root root    6 3  28 2024 桌面

表示字符含义解释
-普通文件
d文件夹
c字符设备键盘
b块设备硬盘
l软链接类似于windows的快捷方式
p管道文件用于程序之间的数据传递,特点是单向的
s套接字传递数据,可以跨主机

第一个字符,后面的9个字符代表文件的权限 属主 属组 其他 .代表核心防护 1 文件的连接个数,硬连接 第一个root 文件的拥有者 第二个root 代表文件的拥有组 0或17 代表文件大小 文件的最后一次使用时间 123/opt 代表文件的名字

list(ls)的用法

ls [选项] ...[参数]...

-a:显示包括.和..隐藏文件

[root@localhost /]# ls -a
.   bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
..  boot  etc  lib   media  opt  root  sbin  sys  usr
​

-A:显示除.及..隐藏文件

[root@localhost /]# ls -A
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
​

-l:使用长格式显示文件 =ll

[root@localhost /]# ls -l
总用量 28
lrwxrwxrwx.   1 root root    7 3  26 19:30 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 3  26 19:35 boot
drwxr-xr-x.  20 root root 3360 3  27 08:28 dev
drwxr-xr-x. 138 root root 8192 3  27 08:28 etc
drwxr-xr-x.   3 root root   20 3  26 19:35 home
lrwxrwxrwx.   1 root root    7 3  26 19:30 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 3  26 19:30 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   3 root root   16 3  26 19:33 opt
dr-xr-xr-x. 174 root root    0 3  27 08:28 proc
dr-xr-x---.  14 root root 4096 3  27 08:32 root
drwxr-xr-x.  40 root root 1180 3  27 08:28 run
lrwxrwxrwx.   1 root root    8 3  26 19:30 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x.  13 root root    0 3  27 08:28 sys
drwxrwxrwt.  23 root root 4096 3  27 09:12 tmp
drwxr-xr-x.  13 root root  155 3  26 19:30 usr
drwxr-xr-x.  21 root root 4096 3  26 19:37 var

-R:递归显示子目录

创建文件夹和子文件夹:
[root@localhost /]# mkdir test
[root@localhost /]# cd test
[root@localhost test]# mkdir 1
[root@localhost test]# cd /test/1
[root@localhost 1]# mkdir 2
[root@localhost 1]# cd 2
[root@localhost 2]# mkdir 3
[root@localhost 2]# cd 3
[root@localhost 3]# mkdir 4
[root@localhost 3]# cd /
​
使用“-R”递归显示子目录:
[root@localhost /]# ls -R test
test:
1
​
test/1:
2
​
test/1/2:
3
​
test/1/2/3:
4
​
test/1/2/3/4:
​

-d:显示当前文件夹的信息,但不显示内容

[root@localhost /]# ll -d
dr-xr-xr-x. 18 root root 236 3月  28 09:05 .

-r:倒序排序

[root@localhost /]# ls -r
var  tmp   sys  sbin  root  opt  media  lib   etc  boot
usr  test  srv  run   proc  mnt  lib64  home  dev  bin
[root@localhost /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test  usr
boot  etc  lib   media  opt  root  sbin  sys  tmp   var
​

-t:按时间倒序排序

[root@localhost /]# ls -tl
总用量 28
drwxr-xr-x.  21 root root 4096 3  28 2024 var
dr-xr-xr-x.   5 root root 4096 3  28 2024 boot
drwxr-xr-x.   3 root root   19 3  28 2024 home
drwxr-xr-x.   3 root root   16 3  28 2024 opt
drwxr-xr-x.  13 root root  155 3  28 2024 usr
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
drwxr-xr-x.   3 root root   15 3  28 09:05 test
drwxr-xr-x. 138 root root 8192 3  28 09:03 etc
dr-xr-x---.  14 root root 4096 3  28 08:42 root
drwxrwxrwt.  23 root root 4096 3  28 08:40 tmp
drwxr-xr-x.  40 root root 1180 3  28 08:40 run
drwxr-xr-x.  20 root root 3320 3  28 08:40 dev
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys
dr-xr-xr-x. 161 root root    0 3  28 08:40 proc
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   2 root root    6 11  5 2016 srv
[root@localhost /]# ll
总用量 28
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 3  28 2024 boot
drwxr-xr-x.  20 root root 3320 3  28 08:40 dev
drwxr-xr-x. 138 root root 8192 3  28 09:03 etc
drwxr-xr-x.   3 root root   19 3  28 2024 home
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   3 root root   16 3  28 2024 opt
dr-xr-xr-x. 161 root root    0 3  28 08:40 proc
dr-xr-x---.  14 root root 4096 3  28 08:42 root
drwxr-xr-x.  40 root root 1180 3  28 08:40 run
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys
drwxr-xr-x.   3 root root   15 3  28 09:05 test
drwxrwxrwt.  23 root root 4096 3  28 08:40 tmp
drwxr-xr-x.  13 root root  155 3  28 2024 usr
drwxr-xr-x.  21 root root 4096 3  28 2024 var

-S:按文件大小排序

[root@localhost /]# ll -h
总用量 28K
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
dr-xr-xr-x.   5 root root 4.0K 3  28 2024 boot
drwxr-xr-x.  20 root root 3.3K 3  28 08:40 dev
drwxr-xr-x. 138 root root 8.0K 3  28 09:03 etc
drwxr-xr-x.   3 root root   19 3  28 2024 home
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   3 root root   16 3  28 2024 opt
dr-xr-xr-x. 160 root root    0 3  28 08:40 proc
dr-xr-x---.  14 root root 4.0K 3  28 08:42 root
drwxr-xr-x.  40 root root 1.2K 3  28 08:40 run
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys
drwxr-xr-x.   3 root root   15 3  28 09:05 test
drwxrwxrwt.  23 root root 4.0K 3  28 08:40 tmp
drwxr-xr-x.  13 root root  155 3  28 2024 usr
drwxr-xr-x.  21 root root 4.0K 3  28 2024 var
[root@localhost /]# ll -S
总用量 28
drwxr-xr-x. 138 root root 8192 3  28 09:03 etc
dr-xr-xr-x.   5 root root 4096 3  28 2024 boot
dr-xr-x---.  14 root root 4096 3  28 08:42 root
drwxrwxrwt.  23 root root 4096 3  28 08:40 tmp
drwxr-xr-x.  21 root root 4096 3  28 2024 var
drwxr-xr-x.  20 root root 3320 3  28 08:40 dev
drwxr-xr-x.  40 root root 1180 3  28 08:40 run
drwxr-xr-x.  13 root root  155 3  28 2024 usr
drwxr-xr-x.   3 root root   19 3  28 2024 home
drwxr-xr-x.   3 root root   16 3  28 2024 opt
drwxr-xr-x.   3 root root   15 3  28 09:05 test
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x. 160 root root    0 3  28 08:40 proc
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys

-h:人性化显示(加单位)

[root@localhost /]# ll -h
总用量 28K
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
dr-xr-xr-x.   5 root root 4.0K 3  28 2024 boot
drwxr-xr-x.  20 root root 3.3K 3  28 08:40 dev
drwxr-xr-x. 138 root root 8.0K 3  28 09:03 etc
drwxr-xr-x.   3 root root   19 3  28 2024 home
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   3 root root   16 3  28 2024 opt
dr-xr-xr-x. 160 root root    0 3  28 08:40 proc
dr-xr-x---.  14 root root 4.0K 3  28 08:42 root
drwxr-xr-x.  40 root root 1.2K 3  28 08:40 run
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys
drwxr-xr-x.   3 root root   15 3  28 09:05 test
drwxrwxrwt.  23 root root 4.0K 3  28 08:40 tmp
drwxr-xr-x.  13 root root  155 3  28 2024 usr
drwxr-xr-x.  21 root root 4.0K 3  28 2024 var
[root@localhost /]# ll
总用量 28
lrwxrwxrwx.   1 root root    7 3  28 2024 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 3  28 2024 boot
drwxr-xr-x.  20 root root 3320 3  28 08:40 dev
drwxr-xr-x. 138 root root 8192 3  28 09:03 etc
drwxr-xr-x.   3 root root   19 3  28 2024 home
lrwxrwxrwx.   1 root root    7 3  28 2024 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 3  28 2024 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 11  5 2016 media
drwxr-xr-x.   2 root root    6 11  5 2016 mnt
drwxr-xr-x.   3 root root   16 3  28 2024 opt
dr-xr-xr-x. 161 root root    0 3  28 08:40 proc
dr-xr-x---.  14 root root 4096 3  28 08:42 root
drwxr-xr-x.  40 root root 1180 3  28 08:40 run
lrwxrwxrwx.   1 root root    8 3  28 2024 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 11  5 2016 srv
dr-xr-xr-x.  13 root root    0 3  28 08:40 sys
drwxr-xr-x.   3 root root   15 3  28 09:05 test
drwxrwxrwt.  23 root root 4096 3  28 08:40 tmp
drwxr-xr-x.  13 root root  155 3  28 2024 usr
drwxr-xr-x.  21 root root 4096 3  28 2024 var

-I :显示文件的身份号

[root@localhost /]# ls -i
    34434 bin   100664480 home    33555589 mnt        9105 run    34317385 test
       64 boot         80 lib     67262336 opt       34438 sbin   33554504 tmp
     1025 dev          82 lib64          1 proc  100664481 srv    67258375 usr
 33554497 etc          83 media   67149889 root          1 sys   100663361 var
​

通配符

  • 匹配文件的名字
  • 正则表达式:匹配文件中的内容
通配符含义
*任意长度的字符
代表单个字符
/转义符

别名

有些命令比较长,不方便我们使用,我们可以使用别名来简化操作

[root@localhost /]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

alias

alias 自定义命令=‘原始命令’

自定义命令:尽量不要使用原有命令

unalias 取消自定义名利

别名>内部>缓存>外部

du:disk use

磁盘使用量

在文件夹下使用du可以统计文件占磁盘的大小

-a:显示所有

-s:显示总和

-h:人性化显示

-d 数字:显示层级

注意a和s不可以同时使用

磁盘是一个块设备

du:看到的不是文件的真实大小,而是该文件占用了磁盘的具体空间

ll:看到的是文件的真实大小

硬盘满了怎么处理

使用du命令查找,占用量较大的文件夹或文件,然后确认是垃圾或无用文件可以将其删除

mkdir

mkdir:建立文件夹

mkdir /xx/xx/xx -p:递归建立文件夹

v 显示过程

touch

touch 选项 文件名

文件名已存在 刷新文件时间

文件名不存在 新建文件

例:touch 123 新建空文件123

linux中分两种

硬链接:

多了一种找到文件的方法,无法对文件夹做链接

软链接:

ln:快捷方式(绝对路径),指明了源文件在什么位置

ln -s:源文件或目录(绝对路径)) 链接文件或目标位置

cp

cp 选项 源文件 存放目的位置(可以改名或不改名)

选项:

-i:提醒覆盖

-f:不提醒覆盖,强制覆盖同一文件夹下同名的文件

-p:保留权限的复制

-r:复制文件夹一定要加

-a:更强大的保留权限复制,可以替代-p