Linux Shell ls查看目录汇总

声明与简介

本文的数据来自网络,部分代码也有所参照,这里做了注释和延伸,旨在技术交流,如有冒犯之处请联系博主及时处理。本文主要介绍Linux的命令ls的使用。

ls命令用于查看当前操作系统下的目录、文件信息。

LS常见命令集锦

查看指定目录的信息

#查看指定目录,默认是当前目录
ls /data/mysql/
ls
ls .

以区分目录、文件形式查看

#以区分目录、文件方式
ls –F /data/mysql

: 1 这里文件以后缀 “/”显示,可执行文件以”*”

2 这里的F约等价于字母Format

以长信息展示目录

#以长信息,详细信息方式展示
ls -l

主要展示以下信息:

  1. 文件类型,D代表目录(Directory)、-代表文件、c代表字符块(character device)、b代表块设备(block device)。
  2. 文件类型,比如截图里的anaconda-ks.cfg的文件权限是rw-即可读可写不可执行。文件的权限有读、写、执行三种,文件权限对应面向三类用户(自己、所在组、其它组)。权限有时也以数字形式表现,其中R对应4,W对应2,X对照1,针对某类用户其权限是三类的总和,所以anaconda-ks.cfg的权限是600
  3. 文件硬链接数
  4. 文件所属用户名
  5. 文件所属组名
  6. 文件最新的修改时间
  7. 文件或目录名

缩略方式查看所有文件(含隐藏文件)

隐藏文件多数为配置文件,以.开头,比如这里的.bash_profile、.bashrc环境变量配置文件;.kettle是KETTLE的配置文件。

# 显示所有文件(含隐藏)
ls –a /root

递归式遍历文件

#递归式查看文件和目录
ls –F –R
#也可以写成 ls –FR

这里的R参数是以递归式查看指定文件信息,即如果有的文件夹有子文件(夹)则会遍历完全。

这里的参数R等价于recurcive

模糊匹配

#模糊匹配方式查看文件、目录
ls -l sh.t?t
ls -l sh*

这里?是一个字符模糊匹配,*则是可以匹配多个字符。

中括号匹配

情景1:中括号多固定字符匹配,比如如下命令会匹配到txt和tgt。

#多字符组合匹配
ls -l sh.t[gx]t

情景2:中括号多固定字符匹配,比如如下命令会匹配到txt和tgt。

#以正则表达式方式匹配
ls -l sh.t[a-zA-Z]t

情景3:反向匹配,比如不含字符“g”的文件,则可以

#反向匹配
ls -l sh.t[!g]t

合并目录、文件名

#这里合并时以逗号作为分隔符。
ls -m .
#shelldir, sh.Tgt, sh.txt

引号包裹文件名

#以引号包裹方式展示
ls -Q .
#"shelldir"  "sh.Tgt"  "sh.txt"

查看目录时仅显示用户及组ID

ls -n .
#总用量 8
#drwxr-xr-x 2 0 0 23 5月  14 16:19 shelldir
#-rw-r--r-- 1 0 0  7 5月  14 16:26 sh.Tgt
#-rw-r--r-- 1 0 0 12 5月  14 16:16 sh.txt

注:这里的0即是root的用户和组ID。可通过如下命令验证:

#查看root的用户信息
cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

指定时间格式显示文件信息

#指定以年月日时分秒方式展示
ls -l --time-style='+%Y/%m/%d %H:%M:%S' .#总用量 8
#drwxr-xr-x 2 root root 23 2021/05/14 16:19:31 shelldir
#-rw-r--r-- 1 root root  7 2021/05/14 16:26:32 sh.Tgt
#-rw-r--r-- 1 root root 12 2021/05/14 16:16:55 sh.txt

其它几种内置时间格式参数为full-iso、long-iso、iso、locale,以下以full-iso为例。

其中full-iso格式为年-月-日 时-分-秒.毫秒 时区。比如2021-05-14 16:19:31.574107937 +0800

Long-iso格式为年-月-日 时-分,比如2021-05-14 16:19

Iso格式为月-日 时-分,比如05-14 16:19

locale格式为依赖于系统的语言环境,当前为中文(zh_CN.utf8),所以显示效果为:5月  14 16:19。

[root@host_128 shellscrips]# ls -l --time-style=full-iso .

总用量 8

drwxr-xr-x 2 root root 23 2021-05-14 16:19:31.574107937 +0800 shelldir

-rw-r--r-- 1 root root  7 2021-05-14 16:26:32.840134427 +0800 sh.Tgt

-rw-r--r-- 1 root root 12 2021-05-14 16:16:55.545098126 +0800 sh.txt

这里显示的是时间格式里含了时区信息,当前CentOS系统是东八区(+0800)。

以文件大小显示

#以文件大小排序,这里以文件大的排在前。
ls -S -l .

通过时间类型查看

这里有三个参数mtime:modification time,即文件内容修改时。

ctime: status change time 即文件的状态(权限和属性)变化时

atime : access time 文件被访问时,比如cat、more、ls等时

注:针对atime的说明:

这里特别注意,不是每次访问时该时间都会更新,而是文件状态或内容修改后最近的一次时间。

案例说明:

1 先修改文件sh.txt,查看通过修改时间查看

2 再修改文件的读写权限,然后通过状态时间查看

3 通过cat访问文件,然后通过访问时间查看

4 再次cat访问文件,然后通过访问时间查看

详细见下文:

ll

总用量 8

drwxr-xr-x 2 root root 23 5月  14 16:19 shelldir

-rw-r--r-- 1 root root 60 5月  14 21:34 sh.Tgt

-rw-r--r-- 1 root root 24 5月  14 21:48 sh.txt

[root@host_128 shellscrips]# echo "new content" >> sh.txt

[root@host_128 shellscrips]# ls -l sh.txt

-rw-r--r-- 1 root root 36 5月  14 22:19 sh.txt

[root@host_128 shellscrips]# chmod 744 sh.txt

[root@host_128 shellscrips]# ls -l --time=ctime sh.txt

-rwxr--r-- 1 root root 36 5月  14 22:20 sh.txt

[root@host_128 shellscrips]# cat sh.txt

say nothing

modify now!

new content

[root@host_128 shellscrips]# ls -l --time=atime sh.txt

-rwxr--r-- 1 root root 36 5月  14 22:20 sh.txt

[root@host_128 shellscrips]# date

2021年 05月 14日 星期五 22:21:05 CST

[root@host_128 shellscrips]# cat sh.txt

say nothing

modify now!

new content

[root@host_128 shellscrips]# ls -l --time=atime sh.txt

-rwxr--r-- 1 root root 36 5月  14 22:20 sh.txt

以列的形式展示目录信息

#这里的C等价于column,ls默认即以列形式展示。
ls –C .

以单独行展示

#每个结果1行,注意这里的参数是1
ls -1 .

shelldir
sh.Tgt
sh.txt

以访问时间展示

#以访问时间格式展示
ls -u -lt .

总用量 8

-rwxr--r-- 1 root root 36 5月  14 22:20 sh.txt

-rw-r--r-- 1 root root 60 5月  14 21:35 sh.Tgt

drwxr-xr-x 2 root root 23 5月  14 16:22 shelldir

[root@host_128 shellscrips]# ls -lt .

总用量 8

-rwxr--r-- 1 root root 36 5月  14 22:19 sh.txt

-rw-r--r-- 1 root root 60 5月  14 21:34 sh.Tgt

drwxr-xr-x 2 root root 23 5月  14 16:19 shelldir

[root@host_128 shellscrips]# stat sh.txt

文件:sh.txt

大小:36          块:8          IO 块:4096   普通文件

设备:fd00h/64768d  Inode:68385119    硬链接:1

权限:(0744/-rwxr--r--)  Uid:(    0/    root)   Gid:(    0/    root)

最近访问:2021-05-14 22:20:41.599276005 +0800

最近更改:2021-05-14 22:19:53.233272963 +0800

最近改动:2021-05-14 22:20:24.664274940 +0800

创建时间:-

以可方便读方式展示

#以可方便读方式展示
ls -h -s .

总用量 8.0K

0 shelldir  4.0K sh.Tgt  4.0K sh.txt

以k为单位显示

#以k为存储单位方式展示文件信息
ls -sk .

总用量 8

0 shelldir  4 sh.Tgt  4 sh.txt

按照指定类型排序

[root@host_128 shellscrips]# ls --sort=version .

sh.Tgt  sh.txt  shelldir

[root@host_128 shellscrips]# ls --sort=time .

sh.txt  sh.Tgt  shelldir

[root@host_128 shellscrips]# ls --sort=extension .

shelldir  sh.Tgt  sh.txt

[root@host_128 shellscrips]# ls --sort=size .

sh.Tgt  sh.txt  shelldir

类型可以是version(针对文件是版本号的如1.2.4,等同于-v)、time(等同于-t 修改时间)、extension、size(等同于-S)。

Linux ls查看目录文件命令集锦相关推荐

  1. 使用ls命令查看Linux的目录结构,linux ls命令查看目录文件详解

    首页 > Linux教程 > 常用命令 > ls 查看目录文件 linux ls命令查看目录文件详解 linux中ls命令用来查看目录中的所有文件和子目录,可选的参数比较多,本文筛选 ...

  2. Linux之查看目录命令

    Linux之查看目录命令 [虚拟机中不能使用右边的数字键盘][终端命令中把字体放大,Ctrl+shift+'+',这个'+'是和等号在一块的那个] 1. 查看目录命令的使用 命令 说明 ls 查看当前 ...

  3. Linux 查看目录常用命令

    linux 查看目录常用命令 ls -F |grep "/$" 只显示当前目录下的文件夹 ls -al |grep "^-" 只显示当前目录下的文件 ls -a ...

  4. windows用 tree命令查看目录文件夹结构

    windows用 tree命令查看目录文件夹结构 ## 查看帮助 tree --helptree --dirsfirst --filelimit 6 -h -t –dirsfirst 目录优先展示 – ...

  5. Linux中查看各文件夹大小命令du -h --max-depth=1

    du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>] [--max-de ...

  6. linux查看文件版本,Linux下查看版本号的命令

    Linux下查看版本号的命令 1,查看内核版本命令: cat   /proc/version uname   -a uname   -rcat   /etc/issue man   uname 2,查 ...

  7. linux查询当前目录剩余空间,如何在linux下查看目录的剩余空间大小

    df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息,命令格式: df -hl 显示格式为: 文件系统 容量 已用 可用 已用% 挂载点 Filesystem Siz ...

  8. linux cat 文本颜色,linux文本文件查看、展示命令 :cat head tail grep more less nl

    linux文本文件查看.显示命令 :cat head tail grep more less nl linux文本文件查看.显示命令 :cat head tail  grep  more less n ...

  9. linux 7进入目录的命令,centos7目录统计之du命令

    CentOS下du查看计算目录大小的命令 用法实例: [root@localhost local]# du -hs smgpdfd 3.3G    smgpdfd [root@localhost lo ...

最新文章

  1. CGAffineTransform与CATransform3D
  2. 一群机器狗亮相MIT,集体后空翻、踢足球,网友:赶紧去看黑镜压压惊
  3. tf.dynamic_stitch 和  tf.dynamic_partition
  4. linux数据被删了怎么办
  5. php下载的文件不是汉字,php实现支持中文的文件下载功能示例
  6. JAVA基础——toString()方法,java基础面试笔试题
  7. 图片抓取_小小爬虫批量抓取微信推文里的图片
  8. 深度学习---之bias
  9. 如何确定线程池核心数的最佳值?
  10. 使用Sigar采系统信息
  11. 计算机网络胡工程施工税率,弱电项目增值税6%、9%、13%税率怎样区分?项目经理必知内容...
  12. H5清理微信缓存的方案
  13. 咖啡技能培训 | 成为咖啡师需要注意哪些方面?
  14. 中考体育项目满分标准(深圳、安徽、湖北)
  15. 前端关于自己模拟接口做测试
  16. C语言: 定义一个函数int isprime(int n),用来判别一个正整数n是否为素数。在主函数中输入两个正整数m和n(m>=1,n>m),统计并输出m和n之间的素数的个数以及这些素数的和。
  17. 2021年中国民航及其重点企业对比分析(中航集团VS东航集团VS南航集团VS海航集团)[图]
  18. 1063 计算谱半径 (20 分)
  19. 施工企业物资管理软件
  20. 微信小程序之实现常用日期格式-日历格式(二)

热门文章

  1. boost::dynamic_property_map相关的测试程序
  2. boost::multiprecision模块tommath相关的测试程序
  3. boost::mpl模块实现transform相关的测试程序
  4. boost::mp11::mp_remove_if相关用法的测试程序
  5. hana::detail::variadic::split_at用法的测试程序
  6. boost::remove_vertex用法的测试程序
  7. boost::geometry::num_interior_rings用法的测试程序
  8. boost::fusion::detail::and_用法的测试
  9. Boost:boost :: bind相等运算符的测试程序
  10. DCMTK:将显示曲线导出到文本文件