linux怎么查看本机内存大小
233
2022-11-07
文件管理
目录的架构
/boot (靴子) 引导文件存放目录, 内核文件vmlinuz 引导加载器 bootloader grub都存放该目录,/boot一般独立分区 /bin 所有用户使用的基本命令都放在此处,一般不独立分区 /sbin 管理类的基本命令,一般不独立分区 /lib 基本共享库文件和内核模块文件 /lib64 专用x86_64 系统上的辅助共享库文件存放位置 /etc 配置文件的目录,纯文本 /home/username 普通用户的家目录 /root 管理员的家目录 /media 便携式设备挂载点 /mnt 临时文件系统挂载点 /dev 设备文件和特殊文件的存储位置 /opt 第三方程序的安装目录 /tmp 临时文件的存放目录 /srv 系统数运行的服务用到的数据 /usr 全局共享只读数据,简称usr bin 保证系统有完整的功能提供的应用程序 sbin lib64 include C程序的头文件 share local 第三方应用程序安装目录 bin sbin lib64 lib etc share /var 通常被拿来独立分区,存放经常发生变化的程序 cache 缓存数据目录 lib 不是放库的文件。是应用程序状态信息数据 lock 锁文件 log 日志文件 run 用来存储进程的pid文件 spool 应用程序数据池 tmp 系统两次启动产生的临时文件系统 mail 邮件文件存放目录 /proc 伪文件系统,输出内核与进程之间的信息 /sys 输出当前硬件的相关信息的伪文件 /selinux 加强型防火墙所在目录 二进制文件: /bin /sbin /use/bin /use/sbin /use/local/bin /use/local/sbin 库文件 /lib /lib64 /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 配置文件 /etc /use/local/etc 帮助文件 /usr/share/man /usr/share/doc /usr/local/share/man /uer/local/share/doc [root12:08 PMcentos7 ~]#ll /etc/motd 开机会显示的内容 -rw-r--r--. 1 root root 0 Jun 7 2013 /etc/motd 文件系统的类型 - 普通 d 目录 c 字符设备 b 块设备 s 套接字 p 管道 l 符号链接 /boot 内核文件放里面 [root12:17 PMcentos7 ~]#ll /boot -h |tail -1 -rwxr-xr-x. 1 root root 5.0M Nov 19 2015 vmlinuz-3.10.0-327.el7.x86_64 /run 内存里面运行的文件 /usr 和操作系统一样的一些文件 /media 移动设备的放置 /proc 内存里面的信息 FSH :文件系统的分层标准
[root@c7-77]#rpm -q man-pages man-pages-3.53-5.el7.noarch
神奇的文件夹
[root09:44 AMcentos7 ~]#yum install autofs -y
[root09:38 AMcentos7 ~]#systemctl start autofs
[root09:50 AMcentos7 ~]#systemctl enable --now autofs
文件后缀决定颜色
ls
[root@centos6 maildrop]# ll /etc/fstab -ih 2228231 -rw-r--r--. 1 root root 860 Apr 16 04:50 /etc/fstab iNode数 文件的类型和权限 硬链接数 文件或目录的所属组 文件或目录大小 文件或目录的修改时间 #修改时间可用ll --time=ctime 或者 --time=atime -a 查看目录中所有数据信息,包含隐藏文件 -d 查看目录属性信息 -i inode节点信息 -c change 属性发生改变的时间 -a access 文件被访问的时间 -t modify 文件修改过后的时间 -rt 时间倒序 -l 长格式 -d 目录 [root10:48 AMcentos7 ~]#ll -d /etc drwxr-xr-x. 75 root root 4096 Jun 2 09:59 /etc -h 人类可以读懂的 -p 目录结尾加斜线,区分目录和文件 -F 不同文件结尾加不同的标识,目录结尾加/,其他的也表现出来 --time-style=long-iso 显示详细时间 [root12:41 PMcentos7 ~]#ll --time-style=long-iso total 12 -rw-------. 1 root root 1248 2021-04-15 08:45 anaconda-ks.cfg -rw-r--r--. 1 root root 8 2021-04-19 12:02 a.txt -rw-r--r--. 1 root root 54 2021-04-19 10:31 case.sh --full-time 以完整的时间格式显示 [root12:11 PMcentos7 ~]#ll /run/ -F 显示目录后面带 \ -A 显示隐藏文件 -a 显示隐藏文件 -F 特定的后缀显示 这样的话可以过滤目录: ls -F |grep -v / 例如备份数据:ls -F |egrep / |awk -F "/" '{print $1} >' [root@centos6 maildrop]# ls -F /bin/date /bin/date* 不同的文件带不同的符号 [root@centos6 maildrop]# ls -F /etc/init.d /etc/init.d@ 不同的文件带不同的符号 链接文件 [root@centos6 maildrop]# find / -type s -exec ls -lF {} \; srw-rw-rw-. 1 root root 0 Apr 20 14:30 /dev/log= 套接字文件带=号 -l 长格式的详细信息 -S 从大到小的排序 -t 按时间顺序排序 -rS 倒序的大小显示 -d 只显示目录文件 */ 只显示当前目录里面的文件夹 [root01:26 AMcentos8 ~]#ls -d /etc/*/ #只显示目录 /etc/cron.weekly/ /etc/gss/ /etc/modprobe.d/ /etc/prelink.conf.d/ /etc/rsyslog.d/ /etc/terminfo/ /etc/crypto-policies/ /etc/init.d/ /etc/modules-load.d/ /etc/profile.d/ /etc/rwtab.d/ /etc/tmpfiles.d/ [root01:26 AMcentos8 ~]#l. #只显示隐藏文件 . .. .bash_logout .bash_profile .bashrc .cshrc .ssh .tcshrc .viminfo meta data 元数据,文件的属性信息,大小、所有者、 mtime 修改时间,内容的时间 atime 访问时间 ctime 属性时间 mtime改变,那么ctime就会改变 ctime改变,那么mtimeye不会改变 [root12:22 PMcentos7 ~]#ll --time=atime anaconda-ks.cfg -rw-------. 1 root root 1248 Apr 15 08:45 anaconda-ks.cfg [root12:35 PMcentos7 ~]#ll --time=ctime anaconda-ks.cfg -rw-------. 1 root root 1248 Apr 15 08:45 anaconda-ks.cfg [root12:35 PMcentos7 ~]#stat anaconda-ks.cfg #一次看三个时间 File: ‘anaconda-ks.cfg’ Size: 1248 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 269457919 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2021-04-15 08:45:22.685199987 -0400 Modify: 2021-04-15 08:45:22.685199987 -0400 Change: 2021-04-15 08:45:22.685199987 -0400 Birth: -
file -f
读取文件内容
[root11:21 AMcentos7 ~]#file -f /data/test /dev/sda1: block special /dev/sda1: block special /etc/hosts: ASCII text /proc: directory [root11:21 AMcentos7 ~]#cat /data/test /dev/sda1 /dev/sda1 /etc/hosts /proc
Windows文件和Linux格式的区别
[root@centos8 ~]# cat win.txt 1 2 3 [root@centos8 ~]# cat linux.txt a b c [root@centos8 ~]# hexdump -C win.txt 00000000 31 0d 0a 32 0d 0a 33 0d 0a |1..2..3..| 00000009 [root@centos8 ~]# hexdump -C linux.txt 00000000 61 0a 62 0a 63 0a |a.b.c.| 00000006 0d 表示换行 0a 回车,windows将回车和换行变成了一个步骤 linux文件里 0a表示换行且回车 Windows文件里 0d 0a 合起来才表示换行和回车 #光标从行尾放在行首叫回车 [root@centos8 ~]# echo '' >test.txt [root@centos8 ~]# hexdump -C test.txt 00000000 0a |.| 00000001 [root@centos8 ~]# echo ' ' >test.txt [root@centos8 ~]# hexdump -C test.txt 00000000 20 0a | .| 00000002 # 0a 表示换行
linux与windows文件格式装换
[root@centos8 ~]# yum install dos2unix -y dos2unix win.txt 将Windows格式装换为linux unix2dos win.txt 将linux格式装换为windows 方法二 [root@localhost ~]# tr -d '\r' < win.txt >win2.txt #去掉回车键,Windows的换行包括回车 将windows格式的win.txt文件 装换为linux格式的win2.txt [root12:42 AMcentos8 ~]#hexdump -C win.txt 00000000 31 0d 0a 32 0d 0a 33 0d 0a |1..2..3..| 00000009 #0d换行 0a回车 [root12:42 AMcentos8 ~]#dos2unix win.txt dos2unix: converting file win.txt to Unix format... [root12:42 AMcentos8 ~]#hexdump -C win.txt 00000000 31 0a 32 0a 33 0a |1.2.3.| 00000006 [root12:42 AMcentos8 ~]#unix2dos win.txt unix2dos: converting file win.txt to DOS format... [root12:43 AMcentos8 ~]#hexdump -C win.txt 00000000 31 0d 0a 32 0d 0a 33 0d 0a |1..2..3..| 00000009 [root12:43 AMcentos8 ~]#
iconv
[root11:46 AMcentos7 ~]#iconv -l 查看一共有哪些编码
将Windows默认的GB232编码 文件装换为Linux 的utf-8 [root12:32 AMcentos7 ~]#iconv -f gb232 windows.txt -o Linux.txt 将Linux的utf-8转化为gb232 [root12:33 AMcentos7 ~]#iconv -f utf8 -t gb232 linux.txt -o win10.txt
总结: Linux的换行符和编码和Linux不一样
通配符
[0-9] 匹配数值范围 [wang]匹配列表中任意字符 [^wang]匹配列表中所有字符以外的字符 * 任意长度的任意字符(0个也可以),不匹配以 . 开头的文件,即隐藏文件 a*b ---> assb ab ? 任意单个字符,有且只有一个 a?b ----> aab a1b adb [root12:08 PMcentos7 /data]#ls abc a.gz test [root12:09 PMcentos7 /data]#ls ??? abc . 在通配符里面没有含义 []匹配范围内任意单个字符 专用字符集合 [[:digit:]] 任意数字,相当于[0-9] [[:lower:]] 任意小写 [[:upper:]] 任意大写 [[:alpha:]] 任意大小写 [[:alnum:]] 任意数字或字母 [[:space:]] 空格 [[:punct:]] 标点符号 [wang] 匹配中任意一个字符 [^wang]匹配中所有字符以外的字符
*glob实现文件名的通配符**,正则表达式针对的是文本的行内容
[root12:51 AMcentos8 ~]#touch f{1..10}.txt 花括号的特殊功能 [root12:52 AMcentos8 ~]#ls f* f10.txt f1.txt f2.txt f3.txt f4.txt f5.txt f6.txt f7.txt f8.txt f9.txt [3-6]和{3..6}[356]区别 [root12:52 AMcentos8 ~]#ls f[3..6].txt f3.txt f6.txt [root12:52 AMcentos8 ~]#ls f{3..6}.txt f3.txt f4.txt f5.txt f6.txt [root12:52 AMcentos8 ~]#ls f[3-6].txt f3.txt f4.txt f5.txt f6.txt [root12:53 AMcentos8 ~]#rm -rf f4.txt [root12:54 AMcentos8 ~]#ls f[3-6].txt #或者的关系 f3.txt f5.txt f6.txt [root12:54 AMcentos8 ~]#ls f{3..6}.txt ls: cannot access 'f4.txt': No such file or directory [root12:55 AMcentos8 ~]#ls f[356].txt 或3 或6 或9 f3.txt f5.txt f6.txt [x-z] [root01:03 AMcentos8 /data]#ls x X y Y z Z [root01:03 AMcentos8 /data]#ls [x-z] x X y Y z =============================================================== [root01:09 AMcentos8 /data]#ls a A b B c C d D [root01:09 AMcentos8 /data]#ls [A-D] A b B c C d D ============================================================= [root01:09 AMcentos8 /data]#ls a A b B c C d D [root01:13 AMcentos8 /data]#ls [^a-d] D 小写 大写 小写 大写,ascii [[:upper:]] [[:lower:]] [[:digit:]]
ls -a 显示所有的文件,包括隐藏文件
作业
案例: 1显示/var目录下,以l开头,中间出现至少一位数值的,以一个小写字母结尾的文件或目录 ls -d /var/l*[[:0-9:]]*[[:lower:]] 2 在/etc目录下非字母开头后面跟一个字母,任意字符结尾的文件或目录 ls /etc/[^[:alpha:]][[:alpha:]]* 3 复制 /etc目录下所有以m开头以非数值结尾的文件或目录到/tmp/wanghua中 mkdir /tmp/wanghua cp -a /etc/m*[^[0-9]] /tmp/wanghua 4 查找/etc 目录下的所有以rc 开头的,后面是0到6的数字,其他为任意字符的文件或,目录 方法一 [root12:47 PMcentos7 /data]#cd /etc [root12:47 PMcentos7 /etc]#ls -d rc[0-6]* rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d 方法二: [root12:52 PMcentos7 ~]#ls -d /etc/rc[0-6]* /etc/rc0.d /etc/rc2.d /etc/rc4.d /etc/rc6.d /etc/rc1.d /etc/rc3.d /etc/rc5.d 5 找出当前目录下所有的隐藏文件或目录 [root12:57 PMcentos7 ~]#ls -d .* . .bash_history .bash_profile .cshrc .tcshrc .. .bash_logout .bashrc .lesshst .viminfo 别名: [root01:01 PMcentos7 ~]#alias l. alias l.='ls -d .* --color=auto' [root01:01 PMcentos7 ~]#l. . .bash_history .bash_profile .cshrc .tcshrc .. .bash_logout .bashrc .lesshst .viminfo 6 只显示/etc目录下的非隐藏目录: [root01:04 PMcentos7 ~]#ls -d /etc/*/ 不加-d 他就跑到子目录也查找 加/就排除了文件,只保留目录
touch
touch修改时间戳 选项说明: -a 仅改变 atime和ctime -m 仅改变 mtime和ctime -t [[CC]YY]MMDDhhmm[.ss] 指定atime和mtime的时间戳 #创建一个一年以后的文件 [root01:29 PMcentos7 ~]#touch `date -d "1 year" +%F_%T`.log [root01:29 PMcentos7 ~]#ls 2022-06-02_13:29:57.log anaconda-ks.cfg
cp
(root账户用的别名,cp -i 但是其他用户的是真正的cp 命令)
-r 递归复制 -i 交互复制,默认就处于这种格式 -p 保留文件的所有者 权限 时间属性 -f 还可以在复制胡过程中不提示,不交互 -a 相当于 dpr -d:只拷贝符号链接,不复制符号链接背后文件, 备份:cp test{,.org} -r: 递归,复制目录。 -u :数据更新的时候才提醒是否覆盖 -t 反着复制 -v 显示过程 [root01:35 PMcentos7 ~]#cp /etc/issue ./issue.bak [root01:35 PMcentos7 ~]#ll !* ll /etc/issue ./issue.bak -rw-r--r--. 1 root root 23 Oct 23 2020 /etc/issue #复制到当前的目录,但是时间属性发生变化,属性发生变化了 -rw-r--r--. 1 root root 23 Jun 2 13:35 ./issue.bak -t [root12:54 PMlocalhost /data]#ls 1.txt 2.txt 3.txt 4.txt 5.txt [root12:54 PMlocalhost /data]#mkdir /wang [root12:55 PMlocalhost /data]#cp -t /wang/ 1.txt 2.txt 3.txt [root12:55 PMlocalhost /data]#ls /wang/ 1.txt 2.txt 3.txt -u [root01:43 AMcentos8 ~]#cp a.txt /data/ [root01:44 AMcentos8 ~]#cp a.txt /data/ cp: overwrite '/data/a.txt'? y [root01:44 AMcentos8 ~]#cp -u a.txt /data/ [root01:44 AMcentos8 ~]#echo 1 >>a.txt [root01:44 AMcentos8 ~]#cp -u a.txt /data/ cp: overwrite '/data/a.txt'? no [root01:45 AMcentos8 ~]#cat /data/a.txt 1 2 3 4 5 --backup=numbered 保留多个版本 [root01:54 AMcentos8 ~]#rm -rf /data/* [root01:54 AMcentos8 ~]#cp --backup=numbered a.txt /data/ [root01:54 AMcentos8 ~]#cp --backup=numbered a.txt /data/ cp: overwrite '/data/a.txt'? y [root01:54 AMcentos8 ~]#cp --backup=numbered a.txt /data/ cp: overwrite '/data/a.txt'? y [root01:54 AMcentos8 ~]#cp --backup=numbered a.txt /data/ cp: overwrite '/data/a.txt'? y [root01:54 AMcentos8 ~]#ls /data/ a.txt a.txt.~1~ a.txt.~2~ a.txt.~3~ -b 覆盖之前还保留一份原来的数据 [root01:45 AMcentos8 ~]#cp -b a.txt /data/ cp: overwrite '/data/a.txt'? y [root01:47 AMcentos8 ~]#ls /data a.txt a.txt~ **root 用户用的是cp 的别名,其他用户用的是真正的cp命令** #备份目录: [root01:58 AMcentos8 ~]#cp -a /etc/ /data/etc-`date +%F_%H_%M_%S` [root02:00 AMcentos8 ~]#ls /data/ etc-2021-04-19_02_00_07 #非幂等性 一个命令第一次执行和后一次执行的效果不一样 [root02:04 AMcentos8 ~]#ls /data/ [root02:04 AMcentos8 ~]#cp -a /etc/ /data/etcbak #第一次是创建了目录 [root02:04 AMcentos8 ~]#cp -a /etc/ /data/etcbak [root02:04 AMcentos8 ~]#ls /data/ etcbak [root02:04 AMcentos8 ~]#cp -a /etc/ /data/etcbak cp: overwrite '/data/etcbak/etc/pki/ca-trust/README'? 第三次提醒我 #快速备份:cp /xx/xx{,.org}
-a 相当于 dpr
即使你是属于root,当时在我家目录,依然覆盖删除你
[root10:27 AMcentos7 ]#echo ggle|grep go* ggle [root10:27 AMcentos7 ]#echo ggle|grep go*le ggle =============================== [root@centos8 ~]# cat ta.txt google gooooooogle ggle gogle gooooOOOgle 至少一个o [root@centos8 ~]# grep 'goo*gle' ta.txt google gooooooogle gogle 至少2个o [root@centos8 ~]# grep 'goo\+gle' ta.txt google gooooooogle 至少2个o [root@centos8 ~]# grep 'gooo*gle' ta.txt google gooooooogle ============================== #后项引用: 变量的重复使用 :%#\(r..\)#\1er#g ================== a.mp5 a.mp5 a.mps 贪婪模式将.mp5结尾的替换 为mp6 :%s#\(.*\)\.mp5#\1.mp6#g =================== http://nginx.org/en/docs/http/ngx_http_rewrite_module.html server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; 含义: ^(/download/.*) 表示$1 即不发生变化 /media/表示替换为/mp3/ (.*)\. 表示$2 .* 表示 mp3 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403; ... } #vim 命令搜索修改: GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=8a6b11db-b142-4b69-afea-02111bf91469 rhgb quiet net.ifnames=0" GRUB_DISABLE_RECOVERY="true" GRUB_ENABLE_BLSCFG=true ~ :/^GRUB_CMDLINE_LINUX/s#\(.*\)"$#\1 net.ifnames=0" #g #找到GRUB_CMDLINE_LINUX开头的行 s###g替换 vim都用的基本的正则表达式 ==================================== 方法二: :/^GRUB_CMDLINE_LINUX=/s@"$@net.ifnames=0"@ [root@centos8 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg ====================================== #分区的利用率: 方法一 [root@centos8 ~]# df |grep '^/dev/sd'|grep -Eo '\<[0-9]{,3}\>%' |tr -d '%' |sort -nr |head -1 15 方法二:只要数字 [root@centos8 ~]# df |grep '^/dev/sd'|grep -Eo '\<[0-9]{,3}\>' |sort -nr |head -1 15 =================================================== #ip和当前主机连接最多前 [root@centos8 ~]# ss -tn |grep 'ESTAB' ESTAB 0 36 10.0.0.130:22 10.0.0.1:55968 ESTAB 0 0 10.0.0.130:22 10.0.0.1:55963 ESTAB 0 0 10.0.0.130:22 10.0.0.1:54305 [root@centos8 ~]# ss -tn |grep 'ESTAB' |tr -s ' ' ':' |cut -d':' -f6 10.0.0.1 10.0.0.1 10.0.0.1 ======================================== #显示连接的状态有哪些种类: [root@centos8 ~]# ss -nta |grep -v 'State' LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* ESTAB 0 36 10.0.0.130:22 10.0.0.1:55968 ESTAB 0 0 10.0.0.130:22 10.0.0.1:55963 ESTAB 0 0 10.0.0.130:22 10.0.0.1:54305 LISTEN 0 128 [::]:22 [::]:* LISTEN 0 5 [::1]:631 [::]:* [root@centos8 ~]# ss -nta |grep -v 'State' |cut -d' ' -f1 |sort -nr |uniq -c 4 LISTEN 3 ESTAB [root@centos8 ~]# ss -nta |tail -n +2 |cut -d' ' -f1 |sort -nr |uniq -c 4 LISTEN 或者用 tail -n +2 不要第一个 3 ESTAB =================================================== #求年龄的总和: [root@centos8 ~]# cat age.txt a=2 b=3 c=4 [root@centos8 ~]# grep -oE '[0-9]+' age.txt |paste -s 2 3 4 [root@centos8 ~]# grep -oE '[0-9]+' age.txt |paste -s -d+ 2+3+4 [root@centos8 ~]# grep -oE '[0-9]+' age.txt |paste -s -d+ |bc 9 方法二: [root@centos8 ~]# cat age.txt a=2 b=3 c=4 [root@centos8 ~]# cut -d'=' -f2 age.txt 2 3 4 [root@centos8 ~]# cut -d'=' -f2 age.txt |tr '\n' '+' 2+3+4++[root@centos8 ~]# cut -d'=' -f2 age.txt |tr '\n' '+'|grep -Eo '.*[0-9]' 2+3+4 [root@centos8 ~]# cut -d'=' -f2 age.txt |tr '\n' '+'|grep -Eo '.*[0-9]' |bc 9 #通配符的意思和{}的意思不一样 [root@localhost data]# touch f{1..6}.txt [root@localhost data]# ls f1.txt f2.txt f3.txt f4.txt f5.txt f6.txt [root@localhost data]# ls f[3..6].txt f3.txt f6.txt [root@localhost data]# ls f{3..6}.txt f3.txt f4.txt f5.txt f6.txt [root@localhost data]# ls f[3-6].txt f3.txt f4.txt f5.txt f6.txt [root@localhost data]# \rm f4.txt [root@localhost data]# ls f[3-6].txt 3到6之间的任意一个,可以断断续续 f3.txt f5.txt f6.txt [root@localhost data]# ls f[356].txt 3或者5或者6 f3.txt f5.txt f6.txt [root@localhost data]# ls f{3..6}.txt 3到6之间的所有内容,必须连续 ls: cannot access f4.txt: No such file or directory f3.txt f5.txt f6.txt [root@localhost data]# touch f{a..z}.log [root@localhost data]# touch f{A..Z}.log [root@localhost data]# ls f1.txt f5.txt fA.log fc.log fD.log ff.log fG.log fi.log fJ.log fl.log fM.log fo.log fP.log fr.log fS.log fu.log fV.log fx.log fY.log f2.txt f6.txt fb.log fC.log fe.log fF.log fh.log fI.log fk.log fL.log fn.log fO.log fq.log fR.log ft.log fU.log fw.log fX.log fz.log f3.txt fa.log fB.log fd.log fE.log fg.log fH.log fj.log fK.log fm.log fN.log fp.log fQ.log fs.log fT.log fv.log fW.log fy.log fZ.log [root@localhost data]# ls f[wang].log fa.log fg.log fn.log fw.log [root@localhost data]# ls f[x-z].log (表示xXyYz) fx.log fX.log fy.log fY.log fz.log [root@localhost data]# ls f[A-C].log fA.log fb.log fB.log fc.log fC.log [root@localhost data]# ls f[^wang].log (排除字表中以外的字符) fA.log fc.log fD.log ff.log fh.log fI.log fk.log fL.log fN.log fp.log fQ.log fs.log fT.log fv.log fx.log fY.log fb.log fC.log fe.log fF.log fH.log fj.log fK.log fm.log fo.log fP.log fr.log fS.log fu.log fV.log fX.log fz.log fB.log fd.log fE.log fG.log fi.log fJ.log fl.log fM.log fO.log fq.log fR.log ft.log fU.log fW.log fy.log fZ.log [root@localhost data]# ls f[[:lower:]].log 所有的小写字母 fa.log fc.log fe.log fg.log fi.log fk.log fm.log fo.log fq.log fs.log fu.log fw.log fy.log fb.log fd.log ff.log fh.log fj.log fl.log fn.log fp.log fr.log ft.log fv.log fx.log fz.log [root@localhost data]# ls f[[:upper:]].log fA.log fC.log fE.log fG.log fI.log fK.log fM.log fO.log fQ.log fS.log fU.log fW.log fY.log fB.log fD.log fF.log fH.log fJ.log fL.log fN.log fP.log fR.log fT.log fV.log fX.log fZ.log [root@localhost data]# ls f[0-9].txt f1.txt f2.txt f3.txt f5.txt f6.txt [root@localhost data]# ls f[[:digit:]].txt f1.txt f2.txt f3.txt f5.txt f6.txt [root@localhost data]# ls -d /etc/*/ 只显示文件夹 -d只显示文件夹不进入文件里面 /etc/alternatives/ /etc/default/ /etc/init.d/ /etc/openldap/ /etc/python/ /etc/rwtab.d/ /etc/tmpfiles.d/ /etc/audisp/ /etc/depmod.d/ /etc/iproute2/ /etc/opt/ /etc/rc0.d/ /etc/sasl2/ /etc/tuned/ /etc/audit/ /etc/dhcp/ /etc/kernel/ /etc/pam.d/ /etc/rc1.d/ /etc/security/ /etc/udev/ /etc/avahi/ /etc/dnsmasq.d/ /etc/krb5.conf.d/ /etc/pkcs11/ /etc/rc2.d/ /etc/selinux/ /etc/wpa_supplicant/ /etc/bash_completion.d/ /etc/dracut.conf.d/ /etc/ld.so.conf.d/ /etc/pki/ [root@localhost ~]# l. . .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .lesshst .ssh .tcshrc .viminfo
rename批量改名字
[root12:39 PMcentos8 /data]#ls f1.log f2.log f3.log f4.log f5.log f6.log [root12:39 PMcentos8 /data]#rename log log.bak * [root12:39 PMcentos8 /data]#ls f1.log.bak f2.log.bak f3.log.bak f4.log.bak f5.log.bak f6.log.bak [root12:39 PMcentos8 /data]#rename .bak '' * 后缀变成空 [root12:40 PMcentos8 /data]#ls f1.log f2.log f3.log f4.log f5.log f6.log [root12:40 PMcentos8 /data]#
删除文件
-f
删除以.开头的文件但是不删除父目录
[root12:48 PMcentos8 /data]#ls -a . .. .a .b .c f1.log f2.log f3.log f4.log f5.log f6.log [root12:48 PMcentos8 /data]#rm -rf .[^.]* [root12:48 PMcentos8 /data]#ls -a . .. f1.log f2.log f3.log f4.log f5.log f6.log
删除所有的文件包括隐藏文件
删除奇葩文件
自己建立rm 回收站
[root@localhost data]# alias rm='DIR=/data/backup-`date +%F-%T`;mkdir $DIR;mv -t $DIR' [root@localhost data]# unalias rm 取消别名,因为别名的优先级最高 [root@localhost data]# ls a.log backup-2020-11-17-01:18:15 b.log e.log.bak h.log.bak k.log.bak n.log.bak q.log.bak t.log.bak w.log.bak z.log.bak backup-2020-11-17-01:16:31 backup-2020-11-17-01:18:29 c.log f.log.bak i.log.bak l.log.bak o.log.bak r.log.bak u.log.bak x.log.bak backup-2020-11-17-01:16:43 backup-2020-11-17-01:18:35 d.log g.log.bak j.log.bak m.log.bak p.log.bak s.log.bak v.log.bak y.log.bak [root@localhost data]# rm -rf ./* [root@localhost data]# alias rm='DIR=/data/backup-`date +%F-%T`;mkdir $DIR;mv -t $DIR' [root@localhost data]# touch /1.txt /2.txt [root@localhost data]# ls / 1.txt 2.txt bak bin boot data dev etc home lib lib64 media mnt opt proc root run sbin script server srv sys tmp usr var wanghua [root@localhost data]# ls /data/ [root@localhost data]# rm /1.txt [root@localhost data]# ls /data/ backup-2020-11-17-01:22:51 [root@localhost data]# rm /2.txt [root@localhost data]# ls /data/ backup-2020-11-17-01:22:51 backup-2020-11-17-01:23:24 别名的优先级高一点 册除文件的时候自动在目录中备份
tree
-d 只显示目录 [root01:44 PMcentos8 /opt]#tree -d /etc/sysconfig/ /etc/sysconfig/ ├── console ├── modules ├── network-scripts └── rhn ├── allowed-actions │?? ├── configfiles │?? └── script └── clientCaps.d -L 定义层级
节点和数据
[root03:45 AMcentos8 ~]#stat 显示文件的原数据属性 权限 所有者 大小 时间戳 指针:数据真正存放的位置,直接指针和间接指针 [root03:46 AMcentos8 ~]#stat a.log File: a.log Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 134321284 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2021-04-19 02:22:58.483728666 +0800 Modify: 2021-04-19 02:22:58.483728666 +0800 Change: 2021-04-19 02:23:33.581891296 +0800 Birth: - Inode: 134321284 每次创建一个文件就有一个节点编号,每个文件的节点编号就是各自的分区所管理的,就像中国和印度两个国家,即使身份证一样也不是一个人。不同的分区,即使节点编号一样也不是同一个文件 [root03:46 AMcentos8 ~]#df -i Filesystem Inodes IUsed IFree IUse% Mounted on devtmpfs 96813 425 96388 1% /dev tmpfs 101311 1 101310 1% /dev/shm tmpfs 101311 632 100679 1% /run tmpfs 101311 17 101294 1% /sys/fs/cgroup /dev/mapper/cl-root 52428800 41404 52387396 1% / /dev/sda1 131072 309 130763 1% /boot tmpfs 101311 5 101306 1% /run/user/0 /dev/sda1 131072 309 130763 1% /boot比如说它的节点空间用完了就没有了 [root04:00 AMcentos8 ~]#dd if=/dev/zero of=/boot/image dd: writing to '/boot/image': No space left on device 3667049+0 records in 3667048+0 records out 1877528576 bytes (1.9 GB, 1.7 GiB) copied, 19.9795 s, 94.0 MB/s [root04:01 AMcentos8 ~]#df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 379M 0 379M 0% /dev tmpfs 396M 0 396M 0% /dev/shm tmpfs 396M 11M 386M 3% /run tmpfs 396M 0 396M 0% /sys/fs/cgroup /dev/mapper/cl-root 100G 5.3G 95G 6% / /dev/sda1 2.0G 1.9G 0 100% /boot 空间被用满 tmpfs 80M 0 80M 0% /run/user/0
节点编号是有限的资源,同一分区移动,节点编号不变化,不同分区移动节点编号发生变化
硬链接和软链接
同一分区的不同的文件的节点编号是不一样的,就像一个国家的身份证号不一样 不同的分区的文件的节点编号的可以一样的,就像两个不同的国家两个人的名字相同。 硬链接的本质就是给同一个文件取多个名字。因此硬链接无法跨分区,相当于一个人,在两个不同的国家 文件夹不能创建硬链接 所有的硬链接都被删除了,那么文件就没了,因为写数据就将那块空闲的区域覆盖了 硬链接的用途:
#一个正在使用的文件,想删除它,但是他很大,如果我贸然删除他,就会有很大的磁盘IO,突然系统特别忙,为了减少磁盘IO压力,想删除它,但是不想太大的IO: 方法: 1. 给这个大文件创建一个硬链接,那么他就有了两个名字了,链接数就变成2了 2 删除这个正在使用的大文件,这样没有很大的磁盘IO, 3.由于这个文件没有真正被删除,所以半夜人少的时候删除这个大文件
或者这样理解
软链接实现软件升级、回滚
删除软链接别带 / (非常重要)
总结
硬链接: 概念说明: 超市的多个门 ??? 在一个分区中,inode号码相同的文件,互为硬链接,相当于访问文件的不同窗口 [root@node3 ~]# touch a.txt [root@node3 ~]# ln a.txt b.txt [root@node3 ~]# ll a.txt -rw-r--r-- 2 root root 0 Apr 2 22:42 a.txt [root@node3 ~]# ll b.txt -rw-r--r-- 2 root root 0 Apr 2 22:42 b.txt [root@node3 ~]# ll -i a.txt b.txt 68446180 -rw-r--r-- 2 root root 0 Apr 2 22:42 a.txt 68446180 -rw-r--r-- 2 root root 0 Apr 2 22:42 b.txt 硬链接磁盘只占用一份空间,相当于同一个人的不同名字 [root@node3 ~]# \rm -rf b.txt [root@node3 ~]# ll -i a.txt 68446180 -rw-r--r-- 1 root root 0 Apr 2 22:42 a.txt 删除一个大文件,先创建一个硬链接,然后删除源文件,留下硬链接的那个文件,快速清理大文件的方法。避免大的磁盘IO 硬链接不可以跨目录创建,硬链接的本质是同一个文件有的不同的名字,不能跨目录 [root@node3 ~]# ln a.txt /boot/b.txt ln: failed to create hard link ‘/boot/b.txt’ => ‘a.txt’: Invalid cross-device link 软链接(符号链接)的大小是路径的大小 drwxr-xr-x 3 root root 51 Feb 2 18:18 TOOS lrwxrwxrwx 1 root root 4 Apr 2 22:55 TOOS.link -> TOOS [root@node3 data]# ln -s /root/data/f2.txt /root/f2.txt.link (此处的源路径和目标路径建议都 用绝对路径,源文件的路径想用相对路径是相对于软链接的链接) [root@node3 data]# ll /root/f2.txt.link -i 67184608 lrwxrwxrwx 1 root root 17 Apr 2 23:38 /root/f2.txt.link -> /root/data/f2.txt [root@node3 data]# cat /root/f2.txt.link (软链接的大小就是路径的大小) doty [root@node3 data]# rm -rf /root/data/f2.txt [root@node3 data]# ll /root/f2.txt.link -i 67184608 lrwxrwxrwx 1 root root 17 Apr 2 23:38 /root/f2.txt.link -> /root/data/f2.txt [root@node3 data]# cp /etc/issue /root/data/f2.txt 更改原始文件里面的内容,软链接的内容也发生变化 [root@node3 data]# cat /root/data/f2.txt \S Kernel \r on an \m [root@node3 data]# ln -s d1 d1.link [root@node3 data]# ll total 4 drwxr-xr-x 3 root root 15 Apr 2 23:26 d1 lrwxrwxrwx 1 root root 2 Apr 2 23:45 d1.link -> d1 -rw-r--r-- 1 root root 0 Apr 2 23:23 f11.txt -rw-r--r-- 1 root root 0 Apr 2 23:23 f22.txt -rw-r--r-- 1 root root 23 Apr 2 23:42 f2.txt lrwxrwxrwx 1 root root 6 Apr 2 23:30 f2.txt.link -> f2.txt [root@node3 data]# touch d1/a.txt [root@node3 data]# ll d1.link/ total 0 -rw-r--r-- 1 root root 0 Apr 2 23:46 a.txt drwxr-xr-x 2 root root 6 Apr 2 23:26 d2 [root@node3 data]# rm -rf d1.link/ (删除软链接带/ 表示删除软链接文件夹里面的内容,但是文件夹 不删除) [root@node3 data]# ll total 4 drwxr-xr-x 2 root root 6 Apr 2 23:47 d1 lrwxrwxrwx 1 root root 2 Apr 2 23:45 d1.link -> d1 -rw-r--r-- 1 root root 0 Apr 2 23:23 f11.txt -rw-r--r-- 1 root root 0 Apr 2 23:23 f22.txt -rw-r--r-- 1 root root 23 Apr 2 23:42 f2.txt lrwxrwxrwx 1 root root 6 Apr 2 23:30 f2.txt.link -> f2.txt [root@node3 data]# ll d1.link/ total 0 生产案例一: df 显示空间多。但是 NO space ... 节点编号(index node)不够了 生产案例二 使用rm 删除大文件以后仍然显示空间不足 寻找大文件 lsof ,半夜关闭一个进程, #软硬链接的区别: 硬链接是一个文件,软链接不是一个文件 硬链接不支持跨设备,软链接支持跨设备 硬链接的iNode相同,软链接的iNode不同 硬链接创建链接,链接数会增加,删除硬链接,链接数减少 软链接删除或创建,链接数不发生变化 硬链接不支持文件夹的创建,软链接支持 相对路径: 硬链接:原始文件的相对路径是当前工作目录 软链接;原始文件的相对路径是相对于链接文件的相对路径 删除源文件: 硬链接: 只是链接数减少,但是链接文件的访问不影响 软链接: 链接文件将无法访问 文件类型: 硬链接:和源文件相同 软链接:链接文件和源文件无关 01. 链接后的文件大小不一致 02. 链接后的文件类型和权限不一致(颜色不一致) 03. 链接后的文件和源文件 inode信息有出入 硬链接inode号码一致 软链接inode号码不一样 04. 源文件被删除之后 硬链接文件依然有效 软链接文件失效 05. 软硬链接创建过程不一致 06.硬链接目录是无法创建硬链接 07 硬链接指向的是同一个文件,软链接指向的是同一个文件的不同路径 硬链接生成的是普通文件,-符号, 软链接生成的l类型 硬链接是通过索引节点进行链接,所有的文件都有一个独立的iNode编号,允许一个文件拥有有效的路径名
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~