Linux文件管理相关知识

Linux中的文件类型与扩展名

Linux文件类型

普通文件(regular file):第一个属性为“-”,按照文件内容,又大略可以分为:
  纯文本文件(ascll):文件内容可以直接读到数据,例如:字母、数字等。可以用cat命令读出文件内容
  二进制文件(binary):linux当中可执行文件就是属于这种格式。例如cat这个执行文件就是一个二进制文件,查看二进制文件的内容使用hexdump命令,eg:hexdump -C /usr/bin/cat
  数据格式文件(data):有些程序在运行的过程中会读取某些特定格式的文件,那些特定格式的文件可以被称为数据文件。例如:linux在用户登录是,都会将登录的数据记录在/var/log/wtmp那个文件内,该文件是一个数据文件。通过last命令读出来。cat命令会读出乱码。因为他属于一种特殊格式的文件
目录(directory):第一个属性为“d”
连接文件(link):第一个属性为“l”,类似windows系统下的快捷方式。
设备与设备文件(device):与系统外设及存储等相关的一些文件,通常都集中在/dev目录下,又分为两种,如下:
  块(block)设备文件:就是一些存储数据,以提供系统随机访问的接口设备。例如:硬盘、软盘等。你可以自行查一下/dev/sda看看,第一个属性为“b”
  字符(character)设备文件:一些串行端口的接口设备,例如:键盘、鼠标等。这些设备的特征就是“一次性读取”的,不能够截断输出。举例:你不可以让鼠标“跳到”另一个界面,而是“滑动”到另一个地方的。第一个属性为“c”
套接字(sockets):既然被称为数据接口文件,这种类型的文件通常被用在网络上的数据连接。我们可以启动一个程序来监听客户端的要求, 而客户端就可以通过这个socket来进行数据的通信了。第一个属性为“s”, 最常在/var/run这个目录中看到这种文件类型了
管道(FIFO,pipe):FIFO也是一种特殊的文件类型,他主要的目的在解决多个程序同时访问一个文件所造成的错误问题。 FIFO是first-in-first-out的缩写。第一个属性为“p”

Linux文件的扩展名

Linux中没有所谓的“扩展名”,一个文件能不能被执行,与它的第一列的10个属性有关,与文件名根本一点关系也没有。只要你的权限当中具有X的话,例如:【-rwxr-xr-x】即代表这个文件可以被执行。这个x代表这个文件具有可执行的能力,但是能不能执行成功,当然就得看该文件的内容

常用的扩展名

*.sh:表示shell脚本或批处理文件(scripts)。通过shell语言开发的程序
*.py:表示python语言文件,通过python语言开发的程序
*.pl:表示perl语言文件,通过perl语言开发的程序
*Z、*.tar、*.tar.gz、*.zip、*.tgz:表示压缩文件,创建命令一般为tar、gzip、unzip
*.html、*.php、*.jsp、*.do:表示网页相关文件。分别代表HTML语法与PHP语法的网页文件。.html文件可以直接用浏览器打开。而php文件是通过客户端的浏览器来服务端浏览,以得到运算后的网页结果
*.conf:表示系统配置文件
*.rpm:表示rpm安装包文件

Linux文件系统目录结构

Linux-file-tree

文件和目录被组织成一个单根倒置的树结构
文件系统从根目录开始,用/表示
根文件系统(rootfs):root filesystem
标准Linux文件系统(eg:ext4),文件名称大小写敏感,eg:MAIL、Mail、mail、maIL都是代表不同的目录
.开头的文件为隐藏文件
路径分隔/
文件名最长不能超过255字节;包括路径在内的文件名称最长不能超过4095字节
不同颜色的文件或目录代表着不同的文件类型:蓝色代表目录;绿色代表可执行文件;红色代表压缩文件;浅蓝色代表链接文件;灰色代表其他文件;决定这些颜色的配置文件:/etc/DIR_COLORS
除了/空格,所有字符都有效;但不推荐使用特殊字符命名目录或者文件,特殊字符需要转移符来引用
每个文件都有两类相关数据:
   元数据:metadata,即属性,eg:文件的大小、创建时间、属主等
   数据:data,即文件内容
Linux目录配置标准:FHS Filesystem Hierarchy Standard

常见的文件系统目录的用途

tree -L 2 /
/
├── bin -> usr/bin  # 所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
├── boot    # 引导文件存放目录,内核文件(vmlinuz),引导加载器(bootloader,grub)都存放在此目录
├── data    # 自定义目录
├── dev # 设备文件及特殊文件目录;eg:硬盘;
├──     # b:block device,随机访问,c:character device,线性访问
├── etc # 系统配置文件目录
├── home    # 普通用户的家目录
├── lib -> usr/lib  # 启动程序时依赖的基本共享库文件及内核模块文件(`/lib/modules`)
├── lib64 -> usr/lib64  # 专用于X86_64系统上的辅助共享库文件目录
├── media   # 便携式移动设备挂在点;eg:光驱、U盘等
├── mnt # 临时文件系统挂在点
├── opt # 第三方应用程序的安装目录 
├── proc    # 用于输出内核与进程信息相关的虚拟文件系统
├── root    # 管理员的家目录
├── run # 运行中的进程相关数据,通常用于存储进程pid文件
├── sbin -> usr/sbin    # 管理类的基本命令,不能关联至独立分区,OS启动即会用到的程序
├── srv  # 系统上运行的服务用到的数据
├── sys # 用于输出当前系统上硬件设备相关信息虚拟文件系统
├── tmp # 临时文件目录
├── usr # universal shared,read-only data  全局共享的只读数据路径
│   ├── bin # 保证系统拥有完整功能而提供的应用程序
│   ├── etc # 配置文件
│   ├── games
│   ├── include # C程序的头文件(header files)
│   ├── lib  # 32位使用
│   ├── lib64   # 专用于X86_64系统
│   ├── libexec
│   ├── local   # 第三方应用程序的安装目录
│   ├── sbin
│   ├── share   # 结构化独立的数据,eg:doc,man等
│   ├── src
│   └── tmp -> ../var/tmp
└── var # variable data files
    ├── adm
    ├── cache   # 应用程序缓存数据目录
    ├── crash
    ├── db
    ├── empty
    ├── games
    ├── gopher
    ├── kerberos
    ├── lib # 应用程序状态信息数据
    ├── local   # 专用于为/usr/local下的应用程序存储可变数据
    ├── lock -> ../run/lock # 锁文件
    ├── log # 日志文件及目录
    ├── mail -> spool/mail
    ├── nis
    ├── opt # 专用于为/opt下的应用程序存储可变数据
    ├── preserve
    ├── run -> ../run   # 运行中的进程相关数据,通常用于存储进程pid文件
    ├── spool   # 应用程序数据池
    ├── tmp # 保存系统两次重启之间产生的临时数据
    └── yp

绝对和相对路径

绝对路径:
   以/开始,完整的文件路径;可用于任何想指定的一个文件名的时候
相对路径:
   不以/开始,一般情况下,指定相对于当前工作的目录或某目录的位置,可以作为一个简短的形式指定一个文件名;
   特殊场景下,是相对于某个指定的目录的位置;eg:环境变量,$JAVA_HOME

基名:basename 只取文件名而不要路径
目录名:dirname 只去路径,不要文件名

实例:

ll /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r-- 1 root root 323 Feb 24 15:01 /etc/sysconfig/network-scripts/ifcfg-eth0
# /etc/sysconfig/network-scripts/ 表示dirname;即所在的目录
# ifcfg-eth0 表示basename;即文件本身

dirname /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0

文件的元数据

元数据信息内容及含义

文件的完整名称:file
文件数据大小:size
占用磁盘的块数:blocks
IO块的大小:IO block
所在设备:device
文件的类型:regular
文件的节点:indoe
文件的连接数:links
文件的访问权限:access
文件的属主:uid
文件的属组:gid
文件的访问时间:access time atime 读取文件内容的时间
文件的修改时间:modify time mtime 修改文件内容的时间
文件的改变时间:change time ctime 元数据发生改变的时间;修改文件的权限、属主、属组、添加软连接等,都会修改ctime的时间;注意⚠️:修改文件内容,除了mtime会改变,同时ctime也会改变的

查看文件元数据

查看一个文件或目录的元数据,我们可以使用stat这个命令

ll /data/sh/pyenv-installer
-rwxr-xr-x 1 root root 2.4K Feb 25 14:50 /data/sh/pyenv-installer

stat /data/sh/pyenv-installer
  File: ‘/data/sh/pyenv-installer’
  Size: 2454      	Blocks: 8          IO Block: 4096   regular file
Device: fd03h/64771d	Inode: 1567        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-25 14:50:38.559596243 +0800
Modify: 2021-02-25 14:50:23.105408493 +0800
Change: 2021-02-25 14:50:29.777489547 +0800
 Birth: -
 
 stat /data/sh
  File: ‘/data/sh’
  Size: 29        	Blocks: 0          IO Block: 4096   directory
Device: fd03h/64771d	Inode: 67          Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-16 10:00:16.279965428 +0800
Modify: 2021-03-08 10:04:43.938121224 +0800
Change: 2021-03-08 10:04:43.938121224 +0800
 Birth: -
 
 stat /dev/vda
  File: ‘/dev/vda’
  Size: 0         	Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d	Inode: 8871        Links: 1     Device type: fc,0
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2021-02-24 16:11:56.986000000 +0800
Modify: 2021-02-24 16:11:56.967000000 +0800
Change: 2021-02-24 16:11:56.967000000 +0800
 Birth: -

想要修改一个文件的访问、修改、改变时间的话,可以使用touch 这个命令

touch 这个命令不仅可以创建文件,如果文件存在的话,会刷新文件的时间
touch <existfile> 刷新文件的所有时间
touch -a <existfile> 仅刷新文件的atime及ctime
touch -m <existfile> 仅刷新文件的mtime及ctime
touch -t [[CC]YY]MMDDhhmm[.ss] <existfile> 指定刷新文件的atime及mtime的时间戳
touch -c 如果文件不存在,则不予创建

cp -rp /var/log/cron-20210228 /tmp/

# 查看cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-05-25 20:29:14.902541963 +0800
Modify: 2021-02-28 03:43:01.559588497 +0800
Change: 2021-03-17 16:33:30.474142243 +0800
 Birth: -
 
# 刷新文件的时间
touch cron-20210228
# 再次查看 cron-20210228元数据中的时间
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:34:54.407097490 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:34:54.407097490 +0800
 Birth: -

# 仅刷新atime及ctime时间 
touch -a cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:34:54.407097490 +0800
Change: 2021-03-17 16:42:56.627585679 +0800
 Birth: -
 
 # 仅刷新mtime及ctime时间
touch -m cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-03-17 16:42:56.627585679 +0800
Modify: 2021-03-17 16:44:13.308458381 +0800
Change: 2021-03-17 16:44:13.308458381 +0800
 Birth: -
 
 # 刷新atime及mtime为02282008的时间戳
touch -t 02282008 cron-20210228
stat cron-20210228
  File: ‘cron-20210228’
  Size: 66251     	Blocks: 136        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 101576211   Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-28 20:08:00.000000000 +0800
Modify: 2021-02-28 20:08:00.000000000 +0800
Change: 2021-03-17 16:45:49.738555861 +0800
 Birth: -

查看文件类型

查看一个文件的类型,我们可以使用file这个命令

格式:

file [options] <filename>...

常用选项:

-b:列出文件辨识结果时,不显示文件名称
-f filelist: 列出文件filelist中文件名的文件类型
-F:使用指定分隔符号替换输出文件名后默认的:分隔符
-L:查看对应软连接对应的文件类型
--help:显示命令的在线帮助

实例:

file /usr/bin/cal
/usr/bin/cal: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ee47c2bd90a4f3981d89e8fbb5e241365871eb7f, stripped

file /data/sh/pyenv-installer
/data/sh/pyenv-installer: a /usr/bin/env bash script, ASCII text executable

file /dev/vda
/dev/vda: block special

file /etc
/etc: directory

文件通配符模式 wildcard pattern

文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件
通配符采用特定的符号,表示特定的含义,此符号称为元 meta字符

常用的通配符:

*: 匹配 0 或多个字符?匹配任意一个字符;*无法匹配.开头的文件,即隐藏文件
?: 匹配任意一个字符
[list]: 匹配 list 中的任意单一字符
   eg: ls -l h[ab]oper.txt 列出haoper.txt或者hboper.txt文件
[!list]或者[^list]:匹配 除list中的任意单一字符以外的字
   eg: ls-l h[!a]oper.txt 列出h[b-z]oper.txt的文件
[c1-c2]:匹配 c1-c2 中的任意单一字符;如:[0-9] [a-z] [A-Z]
   eg: ls-l h[a-c]per.txt 列出h[a-c]per.txt
{string1,string2,…}:匹配 string1 或 string2 (或更多)其一字符串
{c1..c2}:匹配 c1-c2 中全部字符;如:{1…10}
~:当前用户家目录
~+:当前工作目录
~-:前一个工作目录

专用字符集:

[:alnum:]] 任意数字或者字母
[[:alpha:]] 任意字母
[[:space:]] 空格
[[:lower:]] 任意小写字母
[[:digit:]] 任意数字
[[:upper:]] 任意大写字母
[[:cntrl:]] 控制符
[[:graph:]] 图形
[[:print:]] 可打印字符
[[:punct:]] 标点符号
[[:xdigit:]] 16进制数
[[:blank:]] 空格字符(未验证)

实例:

# 实例准备,创建file(a-z、A-Z、0-9)这些文件
touch file{A..Z}.log file{a..z}.log file{0..9}.log
ls
file0.log  file7.log  filec.log  fileF.log  filej.log  fileM.log  fileq.log  fileT.log  filex.log
file1.log  file8.log  fileC.log  fileg.log  fileJ.log  filen.log  fileQ.log  fileu.log  fileX.log
file2.log  file9.log  filed.log  fileG.log  filek.log  fileN.log  filer.log  fileU.log  filey.log
file3.log  filea.log  fileD.log  fileh.log  fileK.log  fileo.log  fileR.log  filev.log  fileY.log
file4.log  fileA.log  filee.log  fileH.log  filel.log  fileO.log  files.log  fileV.log  filez.log
file5.log  fileb.log  fileE.log  filei.log  fileL.log  filep.log  fileS.log  filew.log  fileZ.log
file6.log  fileB.log  filef.log  fileI.log  filem.log  fileP.log  filet.log  fileW.log

# 显示不包含“hoper”字符的文件,等同于ls file[^wang].log
ls file[!hooper].log
file0.log  file6.log  fileb.log  fileE.log  filei.log  filel.log  fileO.log  fileS.log  fileV.log  fileY.log
file1.log  file7.log  fileB.log  filef.log  fileI.log  fileL.log  fileP.log  filet.log  filew.log  filez.log
file2.log  file8.log  filec.log  fileF.log  filej.log  filem.log  fileq.log  fileT.log  fileW.log  fileZ.log
file3.log  file9.log  fileC.log  fileg.log  fileJ.log  fileM.log  fileQ.log  fileu.log  filex.log
file4.log  filea.log  filed.log  fileG.log  filek.log  filen.log  fileR.log  fileU.log  fileX.log
file5.log  fileA.log  fileD.log  fileH.log  fileK.log  fileN.log  files.log  filev.log  filey.log

# 显示只包含“hoper”字符的文件
ls file[hooper].log
filee.log  fileh.log  fileo.log  filep.log  filer.log

# 显示包含A跟D字符的文件
ls file{A,D}.log
fileA.log  fileD.log

# 显示包含0-9数字的文件
ls file[0-9].log
file0.log  file1.log  file2.log  file3.log  file4.log  file5.log  file6.log  file7.log  file8.log  file9.log

# 显示包含“a|A|b|B|c|C|d”的文件
ls file[a-d].log
filea.log  fileA.log  fileb.log  fileB.log  filec.log  fileC.log  filed.log

# 显示包含“A|b|B|c|C|d|D”的文件
ls file[A-D].log
fileA.log  fileb.log  fileB.log  filec.log  fileC.log  filed.log  fileD.log

# 显示包含“:lower”字符的文件
ls file[:lower:].log
filee.log  filel.log  fileo.log  filer.log  filew.log

# 显示所有小写字符的文件
ls file[[:lower:]].log
filea.log  filed.log  fileg.log  filej.log  filem.log  filep.log  files.log  filev.log  filey.log
fileb.log  filee.log  fileh.log  filek.log  filen.log  fileq.log  filet.log  filew.log  filez.log
filec.log  filef.log  filei.log  filel.log  fileo.log  filer.log  fileu.log  filex.log

# 当前工作目录及前一个工作目录的应用
cd /etc/sysconfig/network-scripts/
cd /data/hooper
echo $OLDPWD
/etc/sysconfig/network-scripts
 pwd
/data/hooper
# 显示前一个工作目录
ls ~-/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth0
# 显示当前工作目录
ls ~+/file0.log
/data/hooper/file0.log

# 比较有无*功能的区别
ls -a
. ..  .txt file0.log  file1.log file2.log file9.log

ls -a *.txt
ls: cannot access *.txt: No such file or directory

练习:

# 显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表
ls -ld /etc/l*[[:digit:]]*[[:lower:]]

# 显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
ls -dl /etc/[0-9]*[!0-9]
ls: cannot access /etc/[0-9]*[!0-9]: No such file or directory
ls -dl /etc/[[:digit:]]*[^[:digit:]]
ls: cannot access /etc/[[:digit:]]*[^[:digit:]]: No such file or directory

# 显示/etc目录以非字母开头,后面跟了一个字母及其他任意长度任意字符的文件或目录列表
ls -ld /etc/[!a-Z][a-Z]*
ls: cannot access /etc/[!a-Z][a-Z]*: No such file or directory
ls -dl /etc/[^[:alpha:]][[:alpha:]]*
ls: cannot access /etc/[^[:alpha:]][[:alpha:]]*: No such file or directory

# 显示/etc目录下以rc开头,并后面是0-6之间数字,其他为任意字符的文件或目录列表
ls -ld /etc/rc[0-6]*
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx 1 root root 10 Feb 24 15:08 /etc/rc6.d -> rc.d/rc6.d

# 显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录
ls -ld /etc/[mnrp]*.conf
-rw-r--r--. 1 root root 5171 Oct 31  2018 /etc/man_db.conf
-rw-r--r--. 1 root root  936 Apr  1  2020 /etc/mke2fs.conf
-rw-r--r--. 1 root root 1949 May 25  2020 /etc/nsswitch.conf
-rw-r--r--  1 root root   51 Feb 24 16:11 /etc/resolv.conf
-rw-r--r--. 1 root root  458 Apr  1  2020 /etc/rsyncd.conf
-rw-r--r--. 1 root root 3232 Nov 28  2019 /etc/rsyslog.conf
echo $?
0

# 下面的也能实现,但执行后的结果并不为真
ls -ld /etc/{m,n,r,p}*.conf
ls: cannot access /etc/p*.conf: No such file or directory
-rw-r--r--. 1 root root 5171 Oct 31  2018 /etc/man_db.conf
-rw-r--r--. 1 root root  936 Apr  1  2020 /etc/mke2fs.conf
-rw-r--r--. 1 root root 1949 May 25  2020 /etc/nsswitch.conf
-rw-r--r--  1 root root   51 Feb 24 16:11 /etc/resolv.conf
-rw-r--r--. 1 root root  458 Apr  1  2020 /etc/rsyncd.conf
-rw-r--r--. 1 root root 3232 Nov 28  2019 /etc/rsyslog.conf
echo $?
2

# 只显示/root下的隐藏文件和目录列表
ls -ld /root/.*
dr-xr-x---.  8 root root   4096 Mar  8 15:40 /root/.
dr-xr-xr-x. 18 root root    256 May 25  2020 /root/..
-rw-------.  1 root root  32475 Mar 17 11:25 /root/.bash_history
-rw-r--r--.  1 root root     18 Dec 29  2013 /root/.bash_logout
-rw-r--r--.  1 root root    176 Dec 29  2013 /root/.bash_profile
-rw-r--r--   1 root root    455 Feb 25 15:18 /root/.bashrc
-rw-r--r--.  1 root root    176 May 25  2020 /root/.bashrc.2020-05-25_20-46-30
drwx------   3 root root     17 Feb 25 14:35 /root/.cache
drwx------   3 root root     18 Mar  2 11:49 /root/.config
-rw-r--r--.  1 root root    100 Dec 29  2013 /root/.cshrc
-rw-------   1 root root     35 Mar  8 15:40 /root/.lesshst
-rw-r--r--   1 root root  33939 Mar 15 20:00 /root/.mycli-history
-rw-r--r--   1 root root 189601 Mar 15 19:02 /root/.mycli.log
-rw-r--r--   1 root root   5222 Feb 25 15:32 /root/.myclirc
-rw-r--r--   1 root root   5165 Feb 25 14:58 /root/.myclirc.2021-02-25_15-18-23
-rw-------   1 root root   2388 Mar  7 07:38 /root/.mysql_history
drwxr-xr-x   2 root root     22 Feb 25 14:57 /root/.pip
drwxr-----.  3 root root     19 May 25  2020 /root/.pki
drwxr-xr-x  13 root root   4096 Feb 25 14:56 /root/.pyenv
drwx------   2 root root     25 Mar  3 19:27 /root/.ssh
-rw-r--r--.  1 root root    129 Dec 29  2013 /root/.tcshrc
-rw-------   1 root root   6861 Mar  5 17:36 /root/.viminfo

# 只显示/etc下的非隐藏目录列表
ls -d /etc/[^.]*/
ls -d /etc/*/
/etc/alternatives/       /etc/fonts/           /etc/NetworkManager/  /etc/rc1.d/       /etc/sudoers.d/
/etc/audisp/             /etc/gcrypt/          /etc/ntp/             /etc/rc2.d/       /etc/sysconfig/
/etc/audit/              /etc/gnupg/           /etc/openldap/        /etc/rc3.d/       /etc/sysctl.d/
/etc/bash_completion.d/  /etc/groff/           /etc/opt/             /etc/rc4.d/       /etc/systemd/
/etc/binfmt.d/           /etc/grub.d/          /etc/pam.d/           /etc/rc5.d/       /etc/terminfo/
/etc/chkconfig.d/        /etc/gss/             /etc/pkcs11/          /etc/rc6.d/       /etc/tmpfiles.d/
/etc/cron.d/             /etc/init.d/          /etc/pki/             /etc/rc.d/        /etc/tuned/
/etc/cron.daily/         /etc/iproute2/        /etc/plymouth/        /etc/rpm/         /etc/udev/
/etc/cron.hourly/        /etc/kernel/          /etc/pm/              /etc/rsyslog.d/   /etc/wpa_supplicant/
/etc/cron.monthly/       /etc/krb5.conf.d/     /etc/polkit-1/        /etc/rwtab.d/     /etc/X11/
/etc/cron.weekly/        /etc/ld.so.conf.d/    /etc/popt.d/          /etc/sasl2/       /etc/xdg/
/etc/dbus-1/             /etc/libnl/           /etc/ppp/             /etc/security/    /etc/xinetd.d/
/etc/default/            /etc/logrotate.d/     /etc/prelink.conf.d/  /etc/selinux/     /etc/yum/
/etc/depmod.d/           /etc/lvm/             /etc/profile.d/       /etc/skel/        /etc/yum.repos.d/
/etc/dhcp/               /etc/modprobe.d/      /etc/python/          /etc/ssh/
/etc/dracut.conf.d/      /etc/modules-load.d/  /etc/qemu-ga/         /etc/ssl/
/etc/firewalld/          /etc/my.cnf.d/        /etc/rc0.d/           /etc/statetab.d/

文件的元数据及节点结构

inode表结构

每个文件都有自己的属性信息;eg:文件的大小、时间、类型等,称为文件的元数据(meta data);这些元数据是存放在inode(index node)表中的,node表中有很多记录组成,第一个记录对应存放了一个文件的元数据信息
第一个node表记录对应保存的信息如下:
   inode number节点号
   文件类型
   权限
   UID
   GID
   链接数(指向这个文件名路径名称的个数)
   该文件的大小和不同的时间戳
   指向磁盘上文件的数据块指针
   有关文件的其他数据

inode01
inode02

目录

目录是一个特殊的文件,目录文件的内容保存了此目录中文件的列表及inode number对应关系

文件名是放在目录中的数据块中的,不放在目录的inode中

实例:

# 在同个分区使用mv命令,inode编号没有变
[hooper@magedu-demo ~]$ touch inode_file
[hooper@magedu-demo ~]$ ll -i inode_file
76 -rw-rw-r-- 1 hooper hooper 0 3月  18 11:37 inode_file
[hooper@magedu-demo ~]$ mkdir inode_dir
[hooper@magedu-demo ~]$ mv inode_file ./inode_dir/
[hooper@magedu-demo ~]$ ll -i inode_dir/inode_file
76 -rw-rw-r-- 1 hooper hooper 0 3月  18 11:37 inode_dir/inode_file

# 在不同分区,使用mv命令,inode编号发生的变化
# /home跟/tmp属于不同的两个分区中
[hooper@magedu-demo ~]$ df -h
文件系统                              容量  已用  可用 已用% 挂载点
devtmpfs                              1.9G     0  1.9G    0% /dev
tmpfs                                 1.9G     0  1.9G    0% /dev/shm
tmpfs                                 1.9G   17M  1.9G    1% /run
tmpfs                                 1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mapper/centos_magedu--demo-root   50G  1.9G   49G    4% /
/dev/vda1                            1014M  189M  826M   19% /boot
/dev/mapper/centos_magedu--demo-home   41G   33M   41G    1% /home
/dev/mapper/centos_magedu--demo-data  200G  2.7G  198G    2% /data
tmpfs                                 379M     0  379M    0% /run/user/1000
[hooper@magedu-demo ~]$ ll -i inode_dir/inode_file
76 -rw-rw-r-- 1 hooper hooper 0 3月  18 11:37 inode_dir/inode_file
# 将inode_file文件移动到另一个分区对应的目录后,再次查看inode,发生了变化
[hooper@magedu-demo ~]$ mv inode_dir/inode_file /tmp/
[hooper@magedu-demo ~]$ ll -i /tmp/inode_file
100663369 -rw-rw-r-- 1 hooper hooper 0 3月  18 11:37 /tmp/inode_file

链接

硬(hard)链接

硬链接本质上就给一个文件起了一个新的名称,实质是同一个文件

硬链接特性:

创建硬链接会在对应的目录中增加额外的记录项以引用文件
对应于同一文件系统上一个物理文件
每个目录引用相同的inode号
创建时链接数递增
删除文件时:rm命令递减计数的链接,文件要存在,至少一个链接数,当链接数为0时,该文件被删除
不能跨越驱动器或分区
不支持对目录创建硬链接

格式:

ln filename [linkname]

软(soft)连接

软连接也叫做符号(symbolic)链接,一个符号链接指向另一个文件,就像windows中快捷方式,软连接文件和原文件本质上不是同一个文件

软链接特性:

一个符号链接的内容是它引用文件的名称
可以跨分区链接
可以对目录创建软链接
软连接的大小为指向路径字符串的长度
指向的是另一个文件的路径;不增加或减少目标文件inode的引用计数
软连接如果使用相对路径,是相对于软连接文件的路径,而非相对于当前路径

格式:

ln -s filename [linkname]

实例:

# 软连接的大小为22,正是/data/jdk/jdk1.8.0_101的字符串长度
[kfx@SpringAPP161 ~]$ ll -i /data/jdk
总用量 0
134217795 drwxr-xr-x 8 10 143 255 6月  22 2016 jdk1.8.0_101
[kfx@SpringAPP161 ~]$ ll -i /usr/local/java
949465 lrwxrwxrwx 1 root root 22 10月 13 17:01 /usr/local/java -> /data/jdk/jdk1.8.0_101

# 在创建软连接的时候使用的是相对路径的话,需要注意的是相对路径是针对于软连接文件路径的,而非当前路径
[hooper@magedu-demo link_dir]$ ll
总用量 4
-rw-r--r-- 1 hooper hooper 158 3月  18 15:31 hooper.log
drwxrwxr-x 2 hooper hooper   6 3月  18 15:31 hooper_soft_link
[hooper@magedu-demo link_dir]$ ln -s hooper.log hooper_soft_link/hooper_soft_link.log
[hooper@magedu-demo link_dir]$ ll -i !*
ll -i -s hooper.log hooper_soft_link/hooper_soft_link.log
      74 4 -rw-r--r-- 1 hooper hooper 158 3月  18 15:31 hooper.log
67150913 0 lrwxrwxrwx 1 hooper hooper  10 3月  18 15:32 hooper_soft_link/hooper_soft_link.log -> hooper.log
[hooper@magedu-demo link_dir]$ ln -s ../hooper.log hooper_soft_link/hooper_soft_link_dir.log
[hooper@magedu-demo link_dir]$ ll -i !*
ll -i -s ../hooper.log hooper_soft_link/hooper_soft_link_dir.log
ls: 无法访问../hooper.log: 没有那个文件或目录
67150914 0 lrwxrwxrwx 1 hooper hooper 13 3月  18 15:33 hooper_soft_link/hooper_soft_link_dir.log -> ../hooper.log

连接文件实例

扩展

软连接与硬链接的区别

linux系统中,链接分两种:一种被称为硬链接(Hard Link),另一种被称为符号链接或软链接(Symbolic Link)
使用ln命令来创建链接,ln默认创建的是硬链接;ln -s创建的是软链接
硬链接文件与源文件的inode节点号相同,而软链接文件的inode节点号与源文件不同
删除软链接文件,对源文件及硬链接文件无任何影响
删除文件的硬链接文件,对源文件及软链接文件无任何影响
删除链接文件的原文件,对硬链接文件无影响,会导致其软链接失效(红底白字闪烁状)
同时删除原文件及其硬链接文件,整个文件才会被真正的删除
软连接可以跨文件系统,硬链接不可以跨文件系统
软链接可以对目录进行连接,硬连接不可以
很多硬件设备中的快照功能,使用的就类似硬链接的原理
软链接可以对一个不存在的文件名进行链接,硬链接不行

连接文件

实例:

[hooper@magedu-demo ~]$ mkdir link_dir
[hooper@magedu-demo ~]$ cd link_dir/
[hooper@magedu-demo link_dir]$ ll
总用量 0
[hooper@magedu-demo link_dir]$ touch hooper
# 查看文件的连接数及inode号,连接数为1,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 1 hooper hooper 0 3月  17 18:18 hooper
# 创建硬链接hooper_hard_link
[hooper@magedu-demo link_dir]$ ln hooper hooper_hard_link
# 查看文件的连接数及inode号,连接数为2,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 2 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 2 hooper hooper 0 3月  17 18:18 hooper_hard_link
# 再次创建一个硬链接hooper_hard_link_1
[hooper@magedu-demo link_dir]$ ln hooper hooper_hard_link_1
# 再次查看文件的连接数及inode号,连接数为3,inode为74
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link_1
# 创建一个软连接hooper_soft_link
[hooper@magedu-demo link_dir]$ ln -s hooper hooper_soft_link
# 再次查看文件的连接数及inode号,硬链接连接数为3,软连接文件的inode为75
[hooper@magedu-demo link_dir]$ ls -li
总用量 0
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link
74 -rw-rw-r-- 3 hooper hooper 0 3月  17 18:18 hooper_hard_link_1
75 lrwxrwxrwx 1 hooper hooper 6 3月  17 18:19 hooper_soft_link -> hooper
# 写入数据到原文件中
[hooper@magedu-demo link_dir]$ cat > hooper << EOF
> I am hooper
> EOF
# 分别查看原文件、硬、软链接文件
[hooper@magedu-demo link_dir]$ cat hooper
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_hard_link_1
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
I am hooper
# 删除硬链接文件
[hooper@magedu-demo link_dir]$ rm -rf hooper_hard_link_1
# 查看文件的连接数及inode号,硬链接连接数为2,其他不变
[hooper@magedu-demo link_dir]$ ls -li
总用量 8
74 -rw-rw-r-- 2 hooper hooper 12 3月  17 18:20 hooper
74 -rw-rw-r-- 2 hooper hooper 12 3月  17 18:20 hooper_hard_link
75 lrwxrwxrwx 1 hooper hooper  6 3月  17 18:19 hooper_soft_link -> hooper
# 分别查看文件内容
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper
I am hooper
# 删除原文件
[hooper@magedu-demo link_dir]$ rm -rf hooper
# 查看文件的连接数及inode号,连接数都变成1,inode分别为74、75;但软连接文件显示红色
[hooper@magedu-demo link_dir]$ ls -li
总用量 4
74 -rw-rw-r-- 1 hooper hooper 12 3月  17 18:20 hooper_hard_link
75 lrwxrwxrwx 1 hooper hooper  6 3月  17 18:19 hooper_soft_link -> hooper
# 分别查看文件内容
[hooper@magedu-demo link_dir]$ cat hooper_hard_link
I am hooper
[hooper@magedu-demo link_dir]$ cat hooper_soft_link
cat: hooper_soft_link: 没有那个文件或目录

连接文件实例01

IO重定向和管道

标准输入和输出

一个应用程序在内存中包含指令和数据

打开的文件都是一个fdfile descriptor):文件描述符

Linux给程序提供了三种I\O设备

标准输入(STDIN) - 0 默认接受来自终端窗口的输入
标准输出(STDOUT) - 1 默认输出到终端窗口
标准错误(STDERR) - 2 默认输出到终端窗口

每个应用程序在运行的时候,都会有它的自己文件描述符,其中至少包含:标准输入、标准输出、标准错误

实例

# 以hooper 登录的时候,默认会运行shell中,虚拟终端我们可以使用who查看
[hooper@magedu-demo ~]$ who
hooper   pts/0        2021-03-27 20:56 (192.168.168.238)
[hooper@magedu-demo ~]$ ps -ef|grep bash
hooper    5749  5748  0 20:56 pts/0    00:00:00 -bash
hooper    5778  5749  0 20:56 pts/0    00:00:00 grep --color=auto bash

# 查看当前登录shell的PID
[hooper@magedu-demo ~]$ echo $$
5749
# 扩展:$_为显示上一次命令最后参数
[hooper@magedu-demo ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[hooper@magedu-demo ~]$ ll $_
lrwxrwxrwx. 1 root root 14 3月   9 10:19 /etc/redhat-release -> centos-release

# 查看当前登录shell的PID,对应的文件描述符,其中0、1、2分别代表标准输入、标准输出、标准错误,而/dev/pts/0就是对应的当前登陆的终端
[hooper@magedu-demo ~]$ ll /proc/5749/fd/
总用量 0
lrwx------ 1 hooper hooper 64 3月  27 20:56 0 -> /dev/pts/0
lrwx------ 1 hooper hooper 64 3月  27 20:56 1 -> /dev/pts/0
lrwx------ 1 hooper hooper 64 3月  27 20:56 2 -> /dev/pts/0
lrwx------ 1 hooper hooper 64 3月  27 20:56 255 -> /dev/pts/0
[hooper@magedu-demo ~]$ tty
/dev/pts/0

# 扩展:self是查看自己的
[hooper@magedu-demo ~]$ ll /proc/self/fd/
总用量 0
lrwx------ 1 hooper hooper 64 3月  27 20:59 0 -> /dev/pts/0
lrwx------ 1 hooper hooper 64 3月  27 20:59 1 -> /dev/pts/0
lrwx------ 1 hooper hooper 64 3月  27 20:59 2 -> /dev/pts/0
lr-x------ 1 hooper hooper 64 3月  27 20:59 3 -> /proc/5832/fd

[hooper@magedu-demo ~]$ ll /dev/st*
lrwxrwxrwx 1 root root 15 3月  26 15:51 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 3月  26 15:51 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 3月  26 15:51 /dev/stdout -> /proc/self/fd/1

I/O重定向 redirect

I/O重定向:将默认的输入,输出或者错误对应的设备改变,指向新的目标

标准输出和错误重新定向
STDOUTSTDERR可以被重定向到指定文件,而非默认的当前终端

格式:

命令 操作符号 文件名

支持的操作符包括

1> 或>STDOUT重定向到文件
2>STDERR重定向到文件
&> 把标准输出和错误都重定向

以上如果文件已存在,文件内容被覆盖

set -C 禁止将内容覆盖已有文件,但可追加,利用>| 仍可强制覆盖
set +C 允许覆盖,默认

追加
>> 可以在原有内容基础上,追加的内容

把输出和错误重新定向追加到文件

>> 追加标准输出重定向到文件
2>> 追加标准错误重定向到文件

标准输出和错误输出各自定向至不同位置

COMMAND > /path/to/path/stdout.log 2> /path/to/path/stderr.log

实例

[hooper@magedu-demo ~]$ ls /data /err > /tmp/stdout.log 2> /tmp/stderr.log
[hooper@magedu-demo ~]$ cat /tmp/stdout.log
/data:
jdk
logs
nacos
rabbitmq
sh
soft
[hooper@magedu-demo ~]$ cat /tmp/stderr.log
ls: 无法访问/err: 没有那个文件或目录

合并标准输出和错误输出为同一个数据流进行重定向

&> 覆盖重定向
&>> 追加重定向
COMMAND > /path/to/path/all.log 2>&1 注意顺序很重要
COMMAND &> /path/to/path/all.log 等同上面
COMMAND >> /path/to/path/all.log 2>&1

多个程序合并重定向到同一个文件中

(CMD1;CMD2......) 或者{ CMD1;CMD2......; } 合并多个程序的STDOUT
其中{}的写法,注意在CMD1之前后个空格,然后在最后一个命令结束后有个分号;

实例

# 多个命令合并重定向到指定的文件中
# 将2020年及2021年的日历合并重定向到/tmp/all.log
[hooper@magedu-demo ~]$ (cal 2020;cal 2021) >/tmp/all.log
[hooper@magedu-demo ~]$ cat /tmp/all.log
                               2020

        一月                   二月                   三月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                      1    1  2  3  4  5  6  7
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31

        四月                   五月                   六月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                   1  2       1  2  3  4  5  6
 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                       31
        七月                   八月                   九月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                      1          1  2  3  4  5
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                       30 31
        十月                  十一月                 十二月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
             1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
25 26 27 28 29 30 31   29 30                  27 28 29 30 31


                               2021

        一月                   二月                   三月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2       1  2  3  4  5  6       1  2  3  4  5  6
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    7  8  9 10 11 12 13
10 11 12 13 14 15 16   14 15 16 17 18 19 20   14 15 16 17 18 19 20
17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27
24 25 26 27 28 29 30   28                     28 29 30 31
31
        四月                   五月                   六月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
             1  2  3                      1          1  2  3  4  5
 4  5  6  7  8  9 10    2  3  4  5  6  7  8    6  7  8  9 10 11 12
11 12 13 14 15 16 17    9 10 11 12 13 14 15   13 14 15 16 17 18 19
18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26
25 26 27 28 29 30      23 24 25 26 27 28 29   27 28 29 30
                       30 31
        七月                   八月                   九月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
             1  2  3    1  2  3  4  5  6  7             1  2  3  4
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    5  6  7  8  9 10 11
11 12 13 14 15 16 17   15 16 17 18 19 20 21   12 13 14 15 16 17 18
18 19 20 21 22 23 24   22 23 24 25 26 27 28   19 20 21 22 23 24 25
25 26 27 28 29 30 31   29 30 31               26 27 28 29 30

        十月                  十一月                 十二月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2       1  2  3  4  5  6             1  2  3  4
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11
10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18
17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25
24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31
31

# 将ls /tmp/及cat /etc/redhat-release输出的结果重定向到/tmp/all.txt中
[hooper@magedu-demo ~]$ { ls /tmp/;cat /etc/redhat-release; } > /tmp/all.txt
[hooper@magedu-demo ~]$ cat /tmp/all.txt
all.log
all.txt
stderr.log
stdout.log
systemd-private-370fe7fe3b9445f18bcccda19a597486-chronyd.service-NRlzaJ
user1
user2
user3
CentOS Linux release 7.6.1810 (Core)

# 清除大文件
[hooper@magedu-demo ~]$ cat /dev/null > /tmp/all.txt
[hooper@magedu-demo ~]$ cat /tmp/all.txt

# 扩展知识:在一些交互式命令中,提示的信息,算是标准错误重定向也就是需要将这些信息使用2>或者2>>重定向到指定的文件中
[hooper@magedu-demo ~]$ rm /tmp/all.txt
rm: remove regular empty file ‘/tmp/all.txt’? y
[hooper@magedu-demo ~]$ rm /tmp/std
stderr.log  stdout.log
[hooper@magedu-demo ~]$ rm /tmp/stdout.log 2> /tmp/stderr.log
y
[hooper@magedu-demo ~]$ cat /tmp/stderr.log
rm: remove regular file ‘/tmp/stdout.log’?

# 下列将标准输出及错误输出都重定向到同一个文件中,错误的写法是:
A: ls /data /xxx > /data/all.log 2>&1
B: ls /data /xxx 2> /data/all.log 1>&2
C: ls /data /xxx &> /data/all.log
D: ls /data /xxx 2>&1 > /data/all.log
# 答案为D,

[hooper@magedu-demo tmp]$ curl http://www.wangxiaochun.com/testdir/hello.sh
#!/bin/bash
# ------------------------------------------
# Filename: 	hello.sh
# Version:    	1.0
# Date: 		2017/06/01
# Author:		wang
# Email:		29308620@qq.com
# Website:		www.wangxiaochun.com
# Description:	This is the first script
# Copyright: 	2017 wang
# License:		GPL
# ------------------------------------------
#经典写法
echo "hello, world"
#流行写法
echo 'Hello, world!'
[hooper@magedu-demo tmp]$ echo $?
0
[hooper@magedu-demo tmp]$ curl http://www.wangxiaochun.com/testdir/hello.sh > f1.log 2> f2.log
[hooper@magedu-demo tmp]$ ll
总用量 12
-rw-rw-r-- 1 hooper hooper 388 3月  28 07:02 f1.log
-rw-rw-r-- 1 hooper hooper 317 3月  28 07:02 f2.log
-rw-rw-r-- 1 hooper hooper  47 3月  28 06:53 stderr.log
drwx------ 3 root   root    17 3月  26 15:51 systemd-private-370fe7fe3b9445f18bcccda19a597486-chronyd.service-NRlzaJ
drwxrwxr-x 2 hooper hooper   6 3月  23 14:23 user1
drwxrwxr-x 2 hooper hooper   6 3月  23 14:23 user2
drwxrwxr-x 2 hooper hooper   6 3月  23 14:23 user3
# 在f2.log中重定向的内容为输出的curl信息的内容
[hooper@magedu-demo tmp]$ cat f2.log
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   388  100   388    0     0  14490      0 --:--:-- --:--:-- --:--:-- 14923
[hooper@magedu-demo tmp]$ cat f1.log
#!/bin/bash
# ------------------------------------------
# Filename: 	hello.sh
# Version:    	1.0
# Date: 		2017/06/01
# Author:		wang
# Email:		29308620@qq.com
# Website:		www.wangxiaochun.com
# Description:	This is the first script
# Copyright: 	2017 wang
# License:		GPL
# ------------------------------------------
#经典写法
echo "hello, world"
#流行写法
echo 'Hello, world!'

# 给用户添加密码时候,取消在终端的显示
passwd hooper &> /dev/null

标准输入重定向
从文件中导入STDIN,代替当前终端的输入设备,使用<来重定向标准输入某个命令能够接受从文件中导入的STDIN

实例

# 将sql语句导入到数据库中
[hooper@magedu-demo tmp]$ mysql < test.sql

tr命令

tr 从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出

语法:

tr [OPTION] ... SET1 [SET2]

选项:

-c, -C, --complement 取字符集的补集
-d, --delete 删除匹配SET1的内容,并不作替换
-s, --squeeze-repeats 把连续重复的字符以单独一个字符表示,即去重
-t, --truncate-set1 将SET1对应的字符转化为SET2对应的字符,多余的字符不匹配

SET 是一组字符串,一般都可按照字面含义理解。解析序列如下:
\NNN 八进制值为NNN 的字符(1 至3 个数位)
\\ 反斜杠
\a 终端鸣响
\b 退格
\f 换页
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符
字符1-字符2 从字符1 到字符2 的升序递增过程中经历的所有字符
[字符*] 在SET2 中适用,指定字符会被连续复制直到吻合设置1 的长度
[字符*次数] 对字符执行指定次数的复制,若次数以 0 开头则被视为八进制数
[:alnum:] 所有的字母和数字
[:alpha:] 所有的字母
[:blank:] 所有呈水平排列的空白字符
[:cntrl:] 所有的控制字符
[:digit:] 所有的数字
[:graph:] 所有的可打印字符,不包括空格
[:lower:] 所有的小写字母
[:print:] 所有的可打印字符,包括空格
[:punct:] 所有的标点字符
[:space:] 所有呈水平或垂直排列的空白字符
[:upper:] 所有的大写字母
[:xdigit:] 所有的十六进制数
[=字符=] 所有和指定字符相等的字符

实例

把/etc/issue中的小写字符都转换成大写字符
[hooper@magedu-demo tmp]$ cp -rp /etc/issue /tmp/issue
[hooper@magedu-demo tmp]$ cat /tmp/issue
\S
Kernel \r on an \m

[hooper@magedu-demo tmp]$ tr '[:lower:]' '[:upper:]' /tmp/issue
tr: 额外的操作数 "/tmp/issue"
Try 'tr --help' for more information.
[hooper@magedu-demo tmp]$ tr '[:lower:]' '[:upper:]' < /tmp/issue
\S
KERNEL \R ON AN \M

# 删除文件中所有的abc中任意字符
[hooper@magedu-demo tmp]$ echo 'abcdfgabacadafagcecico' > /tmp/delectchar.log
[hooper@magedu-demo tmp]$ tr -d 'abc' < /tmp/delectchar.log
dfgdfgeio

# 将abcd替换为123,其中d没有替换的对象,默认将SET2中的最后一个字符选为d的替换对象
[hooper@magedu-demo tmp]$ echo 'a1b2c3d4f5g6h9c3' |tr 'abcd' '123'
11223334f5g6h933

# 将abcd替换为123,并做精确匹配,d没有替换的对象,那就不做替换
[hooper@magedu-demo tmp]$ echo 'a1b2c3d4f5g6h9c3' |tr -t 'abcd' '123'
112233d4f5g6h933

# 删除连续的空格
[hooper@magedu-demo tmp]$ df
文件系统                                 1K-块    已用      可用 已用% 挂载点
devtmpfs                               1928412       0   1928412    0% /dev
tmpfs                                  1940144       0   1940144    0% /dev/shm
tmpfs                                  1940144    8700   1931444    1% /run
tmpfs                                  1940144       0   1940144    0% /sys/fs/cgroup
/dev/mapper/centos_magedu--demo-root  52403200 2072436  50330764    4% /
/dev/vda1                              1038336  192860    845476   19% /boot
/dev/mapper/centos_magedu--demo-home  42966532   33208  42933324    1% /home
/dev/mapper/centos_magedu--demo-data 209612800 2771248 206841552    2% /data
tmpfs                                   388032       0    388032    0% /run/user/1000
[hooper@magedu-demo tmp]$ df |tr -s ' '
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 1928412 0 1928412 0% /dev
tmpfs 1940144 0 1940144 0% /dev/shm
tmpfs 1940144 8700 1931444 1% /run
tmpfs 1940144 0 1940144 0% /sys/fs/cgroup
/dev/mapper/centos_magedu--demo-root 52403200 2072304 50330896 4% /
/dev/vda1 1038336 192860 845476 19% /boot
/dev/mapper/centos_magedu--demo-home 42966532 33208 42933324 1% /home
/dev/mapper/centos_magedu--demo-data 209612800 2771248 206841552 2% /data
tmpfs 388032 0 388032 0% /run/user/1000

多行重定向

使用<<终止词 命令从键盘把多行重定向给STDIN,直到终止词位置之前的所有文本都发送给STDIN,有时候被称为就地文本(here document)
其中终止词可以是任何一个或者多个字符,eg:!,@,$,EOF(End Of File),hooper等,其中EOF比较常用

实例

[hooper@magedu-demo tmp]$ cat > /tmp/hooper.log << EOF
> hello hooper
> welcome Beijing
> Thx.
> EOF
[hooper@magedu-demo tmp]$ cat /tmp/hooper.log
hello hooper
welcome Beijing
Thx.

管道

管道

管道(使用符号|表示)用来连接多个命令

格式

CMD1 | CMD2 | CMD3 |......

功能说明:

CMD1命令的STDOUT发送命令CMD2STDIN,将CMD2命令的STDOUT发送命令CMD3STDIN
所有命令会在当前shell进程的子shell进程中执行
组合多种工具的功能
注意⚠️:STDERR默认不能通过管道转发,可以利用2>&1或者|&实现,格式如下:
CMD1 2>&1 | CMD2或者CMD1 |& CMD2

实例

#  错误输出没有被转换
[hooper@magedu-demo tmp]$ ls /data /xxx |tr 'a-z' 'A-Z'
ls: 无法访问/xxx: 没有那个文件或目录
/DATA:
JDK
LOGS
NACOS
RABBITMQ
SH
SOFT
[hooper@magedu-demo tmp]$ ls /data /xxx 2>&1 | tr 'a-z' 'A-Z'
LS: 无法访问/XXX: 没有那个文件或目录
/DATA:
JDK
LOGS
NACOS
RABBITMQ
SH
SOFT
[hooper@magedu-demo tmp]$ ls /data /xxx |& tr 'a-z' 'A-Z'
LS: 无法访问/XXX: 没有那个文件或目录
/DATA:
JDK
LOGS
NACOS
RABBITMQ
SH
SOFT

# 1到100相加的结果
[hooper@magedu-demo tmp]$ echo {1..100}|tr ' ' +|bc
5050
[hooper@magedu-demo tmp]$ echo {1..100}|tr ' ' +|bc
5050
#1到100,单数的相加结果
[hooper@magedu-demo tmp]$ echo {1..100..2} | tr ' ' + | bc
2500
[hooper@magedu-demo tmp]$ seq -s+ 1 2 100 | bc
2500

tee命令

利用tee命令可以重定向到多个目标,经常配合管道符一起使用

格式

CMD1 | tee [-a] /path/to/path/file |CMD2
# 以上可以把CMD1的STDOUT保存到文件中,作为CMD2的输入

选项

-a 追加

功能

保存不同阶段的输出
复杂管道的故障排除
同时查看和记录输出

实例

[hooper@magedu-demo tmp]$ cat << EOF |tee /tmp/motd
> Welcome to CentOS
> I am Hooper
> EOF
Welcome to CentOS
I am Hooper
[hooper@magedu-demo tmp]$ cat /tmp/motd
Welcome to CentOS
I am Hooper

重定向中的-符号

重定向有时会使用-符号

范例

# 将/home里的文件打包,但打包的数据不是记录到文件,而是转送到stdout,经过管道后,将tar -cvf - /home 传送给后面的 tar -xvf - , 后面的这个 - 则是取前一个命令的 stdout, 因此,就不需要使用临时文件了
tar -cvf - /home | tar -xvf -

参考文献

Inode与block详解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值