Linux查看查找目录和文件的命令

Linux查看目录和文件的命令

一、pwd命令:查看当前工作目录

pwd命令用来查看系统当前所在的目录

  • pwd命令使用格式。

     [root@hello ~]# pwd
     /root                    //  root目录是管理员的root的家目录
    

二、cd命令:切换目录

cd命令是切换当前目录的目录。

  • cd命令使用格式。
    格式:cd [目标文件夹位置]

     [root@hello ~]# cd /home/         //切换到 home 目录
     [root@hello home]# pwd         
     /home                      // home 目录是存放普通用户家目录的
    
  • cd命令的其它用法

     [root@hello ~]# cd     /切换到当前用户的家目录
     [root@hello ~]# cd ..           //切换到当前目录的上一级目录     
     [root@hello ~]# cd ../..              //切换到当前目录的上两级目录,以此类推,不过一般没人用
     [root@hello ~]# cd ~ 用户名   //切换到用户的家目录   
    

三、ls命令:列出目录内容

ls命令用于列出目录中的文件和子目录,还可以查看目录的属性。

  • ls命令使用格式
    格式:ls 选项 [目标目录]

     [root@hello ~]#  ls -l /root
     总用量 8
     -rw-------. 1 root root 1729 1月  29 2020 anaconda-ks.cfg
     -rw-r--r--. 1 root root 1760 1月  29 2020 initial-setup-ks.cfg
     drwxr-xr-x. 2 root root    6 2月   2 2020 公共
     drwxr-xr-x. 2 root root    6 2月   2 2020 模板
     drwxr-xr-x. 2 root root    6 2月   2 2020 视频
     drwxr-xr-x. 2 root root    6 2月   2 2020 图片
     drwxr-xr-x. 2 root root    6 2月   2 2020 文档
     drwxr-xr-x. 2 root root    6 2月   2 2020 下载
     drwxr-xr-x. 2 root root    6 2月   2 2020 音乐
     drwxr-xr-x. 2 root root   45 2月   5 2020 桌面
    
  • ls命令常用的一些选项
    选项(-l 以长格式显示)
    选项(-h 一般与-l一起方便查看文件大小)
    选项(-d 一般与-l配合使用,可显示文件路径)
    选项(-A 可以显示目录下所有文件,包括以.开头的隐藏文件)
    选项(-R 可以递归显示目录)

  • 与ls命令类似的两个命令dir和vdir
    dir和vdir与ls的功能类似,但是没有ls的功能多。所以一般不怎么用,Windows里面dir这个命令到是比较常用的。
    dir与ls区别(截图方式做对比)
    在这里插入图片描述
    区别不是很大,但是ls命令可以将不用类型的文件和目录通过颜色做区分。

    vdir与ls区别
    在这里插入图片描述
    vdir命令类似于 在ls命令 后加上了选项 -l 都是以更加详细内容列出文件和子目录。

四、cat命令和more命令:显示文件内容

cat命令用来查看文件内容,并将文件的内容显示在命令行中。通常是内容比较少的文件。

  • cat命令使用格式
    cat [文件的路径]
    查看/root目录下得1.txt文件内容(提前准备好的)

     [root@hello ~]# cat /root/1.txt
     大家好
    
  • cat命令常用的选项

    为了方便阅读一些行数较多的文件。
    选项 (-n)在每一行前面显示行号

     [root@hello ~]# cat /root/1.txt 
     大家好
     [root@hello ~]# cat -n /root/1.txt 
     1	大家好
    

more命令也是用来查看文件内容的,比较cat命令有一个显而易见的好处是,在阅读一下内容较多的文件时,more命令可以将内容分成一页一页的。
例如

	[root@hello ~]# more /etc/wgetrc 

在这里插入图片描述
可以看到在页面的最下方显示有more命令的一个百分比,表示已显示的内容占整个文件的比例。
使用空格键向下翻一页,按Enter建向下翻一行。按Q键退出。

五、head和tail命令:分别用来显示文件的开头和结尾

  • head命令:用来显示文件的开头,默认显示10行

  • tail命令:用来显示文件的结尾,默认显示10行

     [root@hello ~]# head  /etc/fstab 
     [root@hello ~]# tail /etc/fstab 
    
  • 常用选项 -n number 指定显示的行数

      [root@hello ~]# tail -n 2 /etc/fstab 
      [root@hello ~]# head -n 2 /etc/fstab
    

六、 less:文本阅读工具

less和more比较相似,而且功能更强大。

  • less的基本功能
    1、可以使用光标键在文本文件中前后左右的滚屏。
    2、用行号或百分比作为书签浏览文件
    3、可以实现关键字检索,高亮显示等。
    4、more命令阅读到文件底部后会退出,但less不会。
  • 以/etc/wgetrc 这个文件举例。
    在这里插入图片描述
    可以看到,当前页面最下方显示的是文件路径。但是通过方向键移动光标后变化为下图所示的:号
    在这里插入图片描述
    这个冒号后面可以输入命令,比如说 /you 然后Enter键输出;可以查询带有you的字段。
    在这里插入图片描述

七、grep命令:查找文件内容

grep命令可以不用查看全部内容,可以通过查找关键字的方式查看文件中包含这个关键字的行。

  • grep 命令使用格式
    grep 关键字 目标文件路径

  • 例如 查找/etc/passwd 文件中包含 root的行

      [root@hello ~]# grep root /etc/passwd
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin
    
  • 例如 查找/etc/passwd 文件中以root开头的行

      [root@hello ~]# grep ^root /etc/passwd            
      root:x:0:0:root:/root:/bin/bash
    

^root 表示以root开头

  • 例如 查找/etc/passwd文件中 以bash结尾的行

      [root@hello ~]# grep bash$ /etc/passwd
      root:x:0:0:root:/root:/bin/bash
      student:x:1000:1000:student:/home/student:/bin/bash
    

bash$表示以bash结尾

八、find命令: 查找文件的命令

find命令用来在指定范围内查找文件,根据预设的条件递归查找对应的文件。

  • find命令使用格式
    find 指定查找范围 条件1 参数 条件2 参数

  • 常用条件表示:
    -type 类型(f文本文件、d目录、l快捷方式)

      [root@server0 ~]# find /boot/ -type l
      [root@server0 ~]# find /boot/ -type d
      [root@server0 ~]# find /boot/ -type f
    

    -name ‘文档名称’

      [root@server0 ~]# find /etc/  -name 'passwd'
      [root@server0 ~]# find /etc/  -name '*tab'
      [root@server0 ~]# find /etc/  -name '*tab*'
    

查找名字是passwd 的文本文件

	[root@hello ~]# find /etc/ -name  'passwd' -type f

  • 常用条件表示:
    -size + 或 -文件大小(k、M、G)
    -user 用户名 #按照所有者

      [root@server0 ~]# find /boot/ -size +10M
      [root@server0 ~]# find /boot/ -size -10M
      [root@server0 ~]# find /boot/ -size -1024M
      [root@server0 ~]# find /boot/ -size +1M
      [root@server0 ~]# find /home/  -user lisi    //前提是有这个用户
      [root@server0 ~]# find /  -user lisi
    
  • 常用条件表示:
    -mtime 根据文件修改时间
    -mtime +10 #10天之前的数据
    -mtime -10 #最近10天之内的数据

      [root@server0 ~]# find /root/ -mtime +1000
      [root@server0 ~]# find /root/ -mtime -2
    

    三个月之前的数据

      [root@server0 ~]# find /root/ -mtime +90
    

处理find的查询结果
• 使用find命令的 -exec 操作

  • find … … -exec 处理命令 {} ;
  • 优势:以 {} 代替每一个结果,逐个处理,遇 ; 结束

]# find /etc/ -name ‘*tab’
]# find /etc/ -name ‘*tab’ -exec cp {} /opt ;
]# ls /opt/

]# find /boot/ -size +10M
]# find /boot/ -size +10M -exec cp {} /opt ;
]# ls /opt/

发布了7 篇原创文章 · 获赞 2 · 访问量 189

猜你喜欢

转载自blog.csdn.net/weixin_40136446/article/details/104233937