Linux基础(day10)

2.23/2.24/2.25 find命令

which命令介绍

  • 在Linux下搜索文件,which命令
  • which命令是在PATH环境变量中去查找那些命令在哪里
  • 可查找某个命令的绝对路径。

whereis命令介绍

  • whereis命令经过预先生成的一个文件列表库查找与给出的文件名相关文件。
  • whereis 【-bms】 【文件名】
    • -b:只查找二进制文件
    • -m:只查找帮助文件(在man目录下的文件)
    • -s:只查找源代码文件

locate 命令介绍

  • locate命令,须要yum安装——>yum install -y mlocate
    • 在安装后使用locate命令,会提示:locate: 没法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录——>是由于尚未产生db这个文件,就是这个文件所产生的一个数据库
    • 数据库天天凌晨四点会自动生成,若想要手动生成,只须要执行updatedb命令便可
  • 属于模糊搜索
[root@hf-01 ~]# yum install -y mlocate

[root@hf-01 ~]# locate ls
locate: 没法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录
[root@hf-01 ~]# updatedb
[root@hf-01 ~]#
  • 相似whereis命令,也是经过预先生成的文件列表库来告诉用户要查找的文件在哪,后面直接跟文件名
  • locate搜索到的文件列表,无论是目录名仍是文件名,只要包含关键词,都会列出来。

ctrl相关快捷键

  • ctrl+l 清屏
  • ctrl+d 退出一个终端,至关于执行了exit,logout命令
  • ctrl+c 至关于取消这条命令
  • ctrl+u 在当前命令行,删除字光标前面全部的字符
    • 在命令行输入一行命令,而后移动光标到第一个字符,按ctrl+d日后删,是一个一个删
  • ctrl+k 在当前命令行,删除光标后面全部的字符
  • ctrl+e 将光标移动到字符的最后面
  • ctrl+a 将光标移动到字符的第一个位置

find命令

which命令

  • which 搜索命令的绝对路径(搜索的目录,默认为 echo $ PATH 该变量下的目录),前提必须得在默认变量下的目录下,同时还得有执行权限,才能搜索获得。

whereis、locate命令

  • whereis命令是搜索文件的
    • whichis命令,经过预先生成的一个文件列表库查找与给出的文件名相关文件。 (有局限性)
  • whereis 【-bms】 【文件名】
    • -b:只查找二进制文件
    • -m:只查找帮助文件(在man目录下的文件)
    • -s:只查找源代码文件
  • locate命令——>安装包 yum install -y mlocate (模糊搜索)
    • 经过预先生成的文件列表库来告诉用户要查找的文件在哪,后面直接跟文件名 locate搜索到的文件列表,无论是目录名仍是文件名,只要包含关键词,都会列出来。( locate命令,不会搜索 tmp 下的文件)
      • 使用须要先产生库文件,能够手动升级 updatedb 更新数据库

linux基础快捷键

  • Linux快捷键使用:
    • ctrl+l:清屏
    • ctrl+d: 退出终端;或者输入命令瑞出,exit 或者 logout(在行内有字符的状况下是向后删)
    • ctrl+c:直接取消、暂停当前正在运行的进程、取消当前输入
    • ctrl+u:清除当前光标位置至最前内容
    • ctrl+k:清除当前光标位置至最后内容
    • ctrl+e:移动光标是末尾
    • ctrl+a:移动光标是开头
    • ctrl+z:暂停进程运行 fg 能够会以前暂停的进程
    • ctrl+s:暂停动态运行的进程的屏幕 ctrl+q 恢复屏幕动态

find命令

  • find命令语法:
    • find [路径][参数]
  • find 搜索用 用法:
  1. 根据名字查找
  • find 路径 -name “文件名字 ” 去搜索
[root@hf-01 ~]# find /etc/ -name "sshd_config"
/etc/ssh/sshd_config
[root@hf-01 ~]#
  • find只知道名字的模糊搜索
[root@hf-01 ~]# find /etc/ -name "sshd*"
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
/etc/systemd/system/multi-user.target.wants/sshd.service
[root@hf-01 ~]#
  1. 根据文件类型查找
  • find 路径 -type 文件类型
    • d(目录)
    • f(普通的文件)——>二进制的、或文本文档均可以列出来
    • l(软连接文件)
    • s、c(字符串设备文件)
    • b(块设备文件)-name
指定只要目录去搜索
[root@hf-01 ~]# find /etc/ -type d -name "sshd*"

指定类型去搜索
[root@hf-01 ~]# find /etc/ -type f -name "sshd*"
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
[root@hf-01 ~]#

stat命令

  • stat命令能够列出文件的具体信息,包括atime、ctime、mtime
  • 格式:stat 文件/目录
  • stat 2.txt
    • 三个时间属性:
    • Access time 为最近访问时间
    • Modify time 为最近建立或更改时间 (更改内容会改变时间)
    • Change time 为更改文件,更改权限,更改属组,属主时间(更改文件大小,也会改变)
  1. 根据时间查找
  • 最近访问为atime、 最近更改(更改的是文件的内容,包括建立)为mtime、 最近改动(更改全部者、权限、大小,随着inode更改而更改等)为ctime。
    • ①文件的access time(atime)是在读取文件或执行文件时更改的。
    • ②文件的modified time(mtime)是在写入文件时随文件的内容更改而更改的。
    • ③文件的change time (ctime)是在写入文件,更改全部者、权限或连接设置时随着inode内容更改而更改。 inode(索引节点)用来存放档案及目录的基本信息,包含时间信息、文档名、全部者以及所属组。
  • 更改文件的内容即会更改mtime和ctime,但文件的ctime更改了,mtime未必会会更改,如:更改了文件权限,可是文件内容没有变化。
  • 三个time的属性值都放在了inode中,若mtime,atime被修改,那么inode必定会更改,既然inode更改了,那么ctime也就会跟着更改。
  • -o :或者的意思 or 查找atime更改小于一天的文件
  • 查找,etc目录下, f 类型,一天之内mtime有变更的文件(+1:当天之外,-1:当天)
[root@hf-01 ~]# find /etc/ -type f -mtime -1
/etc/tuned/active_profile
/etc/resolv.conf
[root@hf-01 ~]#
  • 查找,etc目录下, f 类型,一天之内mtime有变更的,且文件名以conf结尾的文件
[root@hf-01 ~]# find /etc/ -type f -mtime -1 -name "*.conf"
/etc/resolv.conf
[root@hf-01 ~]#
  • 查找,etc目录下, f 类型,或者一天之内mtime有变更的,或者文件名以conf结尾的文件
[root@hf-01 ~]# find /etc/ -type f -o -mtime -1 -o  -name "*.conf"
  • 查找,etc目录下, f 类型,小于60分钟内改动过,且文件名以conf结尾的文件
[root@hf-01 ~]# find /etc/ -type f -mmin -60 -name "*.conf"
  1. find查找文件的硬连接
  • find 【路径】-inum inode号
[root@hf-01 ~]# ls -l 1_heard.txt 
-rw-r--r-- 2 root root 0 2月  13 22:31 1_heard.txt
[root@hf-01 ~]# ls -i 1_heard.txt 
76239367 1_heard.txt
[root@hf-01 ~]# find / -inum 76239367
/root/1_heard.txt
/tmp/1.txt.bak
[root@hf-01 ~]#
  • 使用find查找inode号,来找到这个硬连接文件
  1. find查找到文件后,直接显示出结果
  • find 【路径】【参数】 -exec 【命令】 { } (这是空格);
    • 注意:结尾必定要有 ;
[root@hf-01 ~]# find /etc/ -type f -mtime -1 -name "*.conf" -exec ls -l {} \;
-rw-r--r-- 1 root root 54 2月  13 17:04 /etc/resolv.conf
[root@hf-01 ~]#
  • 查找,根目录下, f 类型,小于60分钟内改动过,把查找到的结果显示出来
find / -type f -mmin -60 -exec ls -l {} \;
  • 查找,根目录下, f 类型,小于60分钟内改动过,把查找的结果更改成以bak结尾的文件
find / -type f -mmin -60 -exec mv {} {}.bak \;
  1. find根据文件大小查找
  • -size 能够查看 k(文件大小为k),M(文件大小为兆)——>小写m会报错
  • 查找root目录下大于10k的文件
[root@hf-01 ~]# find /root/ -size +10k
/root/.bash_history
/root/zabbix-release-3.2-1.el7.noarch.rpm
/root/shell/.fun3.sh.swp
[root@hf-01 ~]#
  • 查找,root目录下, f 类型,大于10k的文件,把查找到的结果显示出来
[root@hf-01 ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rw-------. 1 root root 28K 2月  10 00:20 /root/.bash_history
-rw-r--r--. 1 root root 14K 9月  14 2016 /root/zabbix-release-3.2-1.el7.noarch.rpm
-rw------- 1 root root 12K 2月   8 00:15 /root/shell/.fun3.sh.swp
[root@hf-01 ~]#

2.26 文件名后缀

  • 在Linux系统中,文件都是有后缀名的,但在Linux中文件的后缀名,并不表明这个文件是什么类型node

    • 为了便于区分,习惯相同的文件定义相同的后缀名
    • 1.sh 表明的是脚本
    • 2.tar.gz表明是压缩文件
    • my.cnf是一个配置文件
    • test.zip是压缩文件
  • echo $LANG 输出当前系统语言linux

这里[root@hf-01 ~]# echo LANG
LANG
[root@hf-01 ~]# echo $LANG
zh_CN.UTF-8
[root@hf-01 ~]# stat !$
stat 2.txt.bak
  文件:"2.txt.bak"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:803h/2051d	Inode:67189291    硬连接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2017-12-31 00:14:59.034915638 +0800
最近更改:2017-12-31 00:14:59.034915638 +0800
最近改动:2017-12-31 00:14:59.034915638 +0800
建立时间:-
[root@hf-01 ~]# 
``
相关文章
相关标签/搜索