day11-文件类型和查找命令

在这里插入图片描述

一.文件属性内容

1.文件类型:
例如这些扩展名:.avi .txt .log .sh .docjava

Windows: 系统根据不一样的扩展名 区分不一样类型的文件python

Linux: 扩展名是给咱们看的。方便人类区分不一样类型的文件linux

常见扩展名
.txt  文本文件
.log  日志文件
.conf .cfg  配置文件
.sh .bash  脚本文件(命令大礼包)

2.常见的文件类型web

-  (file )     普通文件
d  (diectory)  目录
l  (softlink)  软连接文件 快捷方式
b  (block)     设备(块)文件 光盘 硬盘
c  (character) 字符设备 (不断向外发出或接收字符)

二.如何区分文件类型(如何查看)

-rwxrw-r--.  每一个字符的含义
    -   文件类型
	rwx 读 写 执行 (属主)
	rw- 属组的其余用户权限 可读可写不能执行
	r-- 其余用户
	.   seLinux

1. file (-)shell

[root@oldboy59 ~]# file /etc/init.d/network 
  /etc/init.d/network: Bourne-Again shell script, ASCII text executable
file下:
 1.二进制文件
 2.文本文件(.txt)
 3.数据文件(data) 压缩包

2. d 目录 (文件夹)windows

[root@oldboy59 tmp]# ll
total 10932
drwxr-xr-x 2 root root       46 Apr  9 15:40 data
drwxr-xr-x 3 root root       20 Apr  9 18:35 file

3. l 软连接文件
软连接/符号连接 快捷方式 存放源文件的位置centos

建立软连接  ln -s

[root@oldboy59 tmp]# ln -s file  file.soft
[root@oldboy59 tmp]# ll
total 10932
drwxr-xr-x 3 root root       20 Apr  9 18:35 file
lrwxrwxrwx 1 root root        4 Apr  9 19:50 file.soft -> file

4. b 设备(块)文件 光盘 硬盘数组

[root@oldboy59 tmp]# ls -l /dev/cdrom  /dev/sr0 /dev/sda
lrwxrwxrwx 1 root root      3 Apr  8 22:07 /dev/cdrom -> sr0
brw-rw---- 1 root disk   8, 0 Apr  8 22:07 /dev/sda
brw-rw---- 1 root cdrom 11, 0 Apr  8 22:07 /dev/sr0

5. c 字符设备 (不断向外发出或接收字符 )bash

/dev/urandom   随机字符生成器(不断向外发出或接收字符 生成随机密码)
/dev/null      黑洞 (不断接收信息)
/dev/zero      白洞 (不断发出信息 没法查看)

[root@oldboy59 tmp]# ll /dev/urandom 
crw-rw-rw- 1 root root 1, 9 Apr  8 22:07 /dev/urandom

三. tr 替换的参数

-d删除
-c取反
例:(输入重定向 < 不要搞混)dom

在oldboy.txt下建立{a..z}  {0..10} 的数组:
[root@oldboyedu59 ~]# echo {a..z} {0..10} > /oldboy/oldboy.txt
[root@oldboyedu59 ~]# cat /oldboy/oldboy.txt
a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 10
2019/4/9 15:49:40

让小写字母替换成大写字母:
[root@oldboyedu59 ~]# tr 'a-z' 'A-Z' </oldboy/oldboy.txt
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 10

删除掉‘a-z’的字母:
[root@oldboyedu59 ~]# tr -d 'a-z' </oldboy/oldboy.txt
                          0 1 2 3 4 5 6 7 8 9 10
删除其余的并保留下'a-z' :
[root@oldboyedu59 ~]# tr -cd 'a-z' </oldboy/oldboy.txt
abcdefghijklmnopqrstuvwxyz[root@oldboyedu59 ~]# 

四. 文件查找类相关命令:

1. which 显示命令全路径

[root@oldboy59 tmp]# which sed awk grep
  alias grep='grep --color=auto'
  	/usr/bin/grep
  /usr/bin/sed
  /usr/bin/awk

2. whereis 显示命令及其相关文件全路径

[root@oldboy59 tmp]# whereis sed
  sed: /usr/bin/sed /usr/share/man/man1/sed.1.gz

3. locate 快速定位文件路径(了解无需掌握)

[root@oldboy59 tmp]# locate grep awk

在安装locate命令时可能会出现yum故障:

[root@oldboy59 tmp]# yum install -y locate
....
.....  
No package locate available.  \\没有叫作locate的软件包
Error: Nothing to do

如何查询命令属于哪一个软件包呢

用命令  yum provides locate

[root@oldboy59 tmp]# yum provides locate
....
mlocate-0.26-8.el7.x86_64 : An utility for finding files by name  \\软件包叫mlocate 版本号  el7(centos7) 64位
Repo        : @base
Matched from:
Filename    : /usr/bin/locate
还有一种方法就是—光盘安装
使用命令  mount /dev/cdrom /mnt/

[root@oldboyedu59 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only

[root@oldboyedu59 ~]# ls /mnt/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

[root@oldboyedu59 ~]# rpm -ivh /mnt/Packages/mlocate-0.26-8.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
	package mlocate-0.26-8.el7.x86_64 is already installed
	
[root@oldboyedu59 ~]# rpm -qa mlocate 
mlocate-0.26-8.el7.x86_64

4. find 查找目录下的文件 ※

1#.find -type 查找某类型的文件
-f 文件
-d 目录

:查找文件
找到/tmp下的以.txt结尾的文件
[root@oldboy59 tmp]# find /tmp/ -type f -name "*.txt"
/tmp/all.txt
/tmp/oldboy.txt
/tmp/data/oldboy.txt

找到ip命令所在的位置
[root@oldboy59 tmp]# find / -type f -name 'ip'
/usr/sbin/ip
/usr/share/bash-completion/completions/ip
:查找目录
使用  find / -type d -maxdepth +行数
相似于tree命令

[root@oldboy59 ~]# find / -type d -maxdepth 1
find: warning: you have specified the -maxdepth option after a non-option argument 
-type, but options are not positional (-maxdepth affects tests specified before it as 
well as those specified after it).  Please specify options before other arguments.
\\warning 警告了

-maxdepth 1 这个参数要放在其余参数以前
[root@oldboyedu59 ~]# find / -maxdepth 1 -type d 
/
/boot
/dev
/proc
/run
/sys
/etc
/root
.......

2#. find -iname参数 查找的文件不区分大小写
区分大小写:

[root@oldboyedu59 ~]# find /oldboy/alex/ -type f -name "oldboy*.txt"
/oldboy/alex/lidao/oldboy.txt
/oldboy/alex/oldboy01.txt
/oldboy/alex/oldboy02.txt
/oldboy/alex/oldboy03.txt
/oldboy/alex/oldboy04.txt
/oldboy/alex/oldboy05.txt

不区分大小写:

[root@oldboyedu59 ~]# find /oldboy/alex/ -type f -iname "oldboy*.txt"
/oldboy/alex/lidao/oldboy.txt
/oldboy/alex/oldboy01.txt
/oldboy/alex/oldboy02.txt
/oldboy/alex/oldboy03.txt
/oldboy/alex/oldboy04.txt
/oldboy/alex/oldboy05.txt
/oldboy/alex/OLDboy01.txt
/oldboy/alex/OLDboy02.txt
/oldboy/alex/OLDboy03.txt
/oldboy/alex/OLDboy04.txt
/oldboy/alex/OLDboy05.txt
例:

找出/etc/目录下面第1层目录中以.conf结尾的文件(不区分大小写):

find /etc/ -maxdepth 1  -type f  -iname "*.conf"

3#. find /etc/ -size +/- 1k/M 按文件大小查找
-size +/- 1M或k

查看/etc/下大于1M的文件:
[root@oldboy59 tmp]# find /etc/ -size +1M
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/active/policy.linked
/etc/udev/hwdb.bin

5. xargs 分组

先建立好环境:
[root@oldboyedu59 ~]# echo {1..10}  >/oldboy/sf.txt
[root@oldboy59 tmp]# cat oldboy/oldboy.txt 
1 2 3 4 5 6 7 8 9 10
[root@oldboy59 tmp]# xargs -n 3 < oldboy/oldboy.txt 
1 2 3
4 5 6
7 8 9
10

6.-exec 命令 {} ;

找出/oldboy下面以.txt结尾的文件复制到/tmp下面
find oldboy/ -type f -name ‘*.txt’ -exec cp {} /tmp/ ;

7.-mtime 1

一天之内 -mtime -1
一天之前全部 -mtime +1
只看一天前 -mtime 1

8.-maxdepth 2 相似tree命令

find / -type f -maxdepth 3 -name ‘*.txt’ 查看以.txt结尾的文件,最大目录层数是3层

五. tar 打包压缩命令

压缩文本文件 工做中能够用来备份配置文件

咱们在windows中常见的压缩软件 : 压缩 (Winrar 好压)
在Liunx中 : 打包压缩

参数:
z   经过gzip工具进行压缩
c   create  建立包
v   verbose 显示过程
f   指定压缩包(最好放在最后)
t   list  查看压缩包内容
x   解压

image.png

1.如何建立压缩包
tar zcvf

例:
使 /etc 目录 压缩完成放在:
/tmp/etc/.tar.gz (经过tar打包 gzip进行压缩)

tar zcvf
[root@oldboy59 tmp]# tar zcvf /tmp/etc.tar.gz /etc/

2. 如何查看压缩包的内容
tar ztf

[root@oldboy59 tmp]# tar ztf /tmp/etc.tar.gz

刷屏.....

3 如何解压
zxvf

[root@oldboy59 tmp]# tar zxf etc.tar.gz 
[root@oldboy59 tmp]# ll etc
total 1092
drwxr-xr-x  3 root root    101 Mar 26 13:57 abrt
-rw-r--r--  1 root root     16 Mar 26 14:04 adjtime
-rw-r--r--  1 root root   1518 Jun  7  2013 aliases
-rw-r--r--  1 root root  12288 Mar 26 14:10 aliases.db
....

4. 怎么解压到指定目录
xf (默认解压到当前目录)
解压到的指定路径前要加 -C

[root@oldboy59 tmp]# tar xf etc.tar.gz -C /data/
[root@oldboy59 tmp]# ll /data/
total 01
drwxr-xr-x  81 root root 8192 Apr  9 15:12 etc

【巨坑】tar让咱们迷茫的提示

相关连接 :xianhttps://www.jianshu.com/p/631618020430
使用tar建立压缩包的时候 被压缩的文件或目录使用了绝对路径 会有这个提示

[root@oldboy59 tmp]# tar zcvf oldboy/oldboy.txt  /tmp/oldboy.txt.tar.gz
tar: Removing leading `/' from member names  \\从压缩包中删除每一个文件最开头的/
/tmp/oldboy.txt.tar.gz

把压缩包中的 绝对路径---->相对路径
因此tar命令解压到的时候默认会解压到当前目录

为什么tar命令这么作? 解压的时候 也会按照绝对路径解压 会把源文件覆盖。 系统替咱们预防故障,经过把绝对路径—>相对路径。