首页 技术 正文
技术 2022年11月17日
0 收藏 680 点赞 2,277 浏览 11624 个字

文件有文件名与数据,在linux上被分为两个部分:用户数据(user data)与元数据(metadata)

用户数据,即文件数据块(data block),数据块是记录文件真实内容的地方,我们将其称为Block

元数据,即文件的附加属性,如文件大小,创建时间,所有者等信息,我们称其为Inode

在linux中,inode是文件元数据的一分部但其并不包含文件名,inode号即索引节点号,文件名仅是为了方便人们的记忆,

系统或程序通过正确的文件数据块。

ls命令详解

-a, –all

# 列出所有的文件,包括以“.“开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录)
[root@centos ~]#ls -la /data/
total 15364
drwxr-xr-x. 3 root root 50 Mar 27 18:46 .
dr-xr-xr-x. 19 root root 4096 Mar 27 11:32 ..
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

-A, –almost-all

# 列出目录中除当前目录"."和父目录".."之外的所有文件列表
[root@centos ~]#ls -lA /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

–author

# 配合-l,打印出每个文件的作者
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -l --author /data/
total 15360
-rw-r--r-- 1 root root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

-b, –escape

# 将文件中不可输出的字符以反斜线 " \ " 加字符编码的方式输出
ls -b

–block-size=SIZE

# 用指定特定的单位格式来列出文件和目录的大小
ls -l --block-size=K
ls -l --block-size=M

-B, –ignore-backups


# 列出目录中的文件,但是不显示备份文件,即那些文件名以"~"结尾的文件
ls -B

-c

# 与 -lt 选项连用时,按照文件状态时间排序输出目录内容,根据文件的索引节点中的 ctime 字段排序,与 -l 选项连用时,则排序的一句是文件的状态改变时间
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -ltc /data/
total 15360
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

-C

# 多列显示输出结果,默认选项
[root@centos ~]#ls /data/
file.txt logs testfile
[root@centos ~]#ls -C /data/
file.txt logs testfile
[root@centos ~]#

–color[=WHEN]

# 使用不同的颜色高亮显示不同的类型
ls --color=never
ls --color=auto
ls --color=always

-d, –directory

# 仅显示目录名,而不显示目录下的内容列表。显示符号链接文件本身,而不显示其所指向的目录列表
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -ld /data/
drwxr-xr-x. 3 root root 50 Mar 27 18:46 /data/
[root@centos ~]#

-D, –dired

generate output designed for Emacs’ dired mode

-f do not sort, enable -aU, disable -ls –color

-F, –classify

在每个输出项后追加文件的类型标识符,具体含义:
* 表示具有可执行权限的普通文件
/ 表示目录
@ 表示符号链接
| 表示命名管道 FIFO(First In First Out)
= 表示 sockets 套接字
当文件为普通文件时,不输出任何标识符
[root@centos ~]#ls -F1 /run/
NetworkManager/
agetty.reload
atd.pid
auditd.pid
console/
... 省略部分内容 ...
udev/
user/
utmp
[root@centos ~]#

–file-type

likewise, except do not append '*'

–format=WORD

# 以多种格式列出目录内容,如以逗号分隔、水平格式、长格式、垂直格式、交叉格式等列出
# 交叉
ls --format=across
# 逗号
ls --format=comma
# 水平
ls --format=horizontal
# 长格式
ls --format=long
# 单栏
ls --format=single-column
# 详情
ls --format=verbose
# 垂直
ls --format=vertical

–full-time:列出完整的日期与时间

like -l --time-style=full-iso
# 等于
ls -l --time-style=full-iso
# 示例
[root@centos /tmp]#ls -l --time-style=full-iso
total 16
-rw-r--r--. 1 root root 0 2021-03-25 14:21:20.744456987 +0800 14:21:20.log
-rw-r--r--. 1 root root 0 2021-03-25 14:21:37.077457392 +0800 2021-03-25.log
-rw-r--r--. 1 root root 12 2021-03-26 10:17:08.076137207 +0800 a.log
-rw-r--r--. 1 root root 12 2021-03-26 10:20:22.275142023 +0800 b.log
-rw-r--r--. 1 song root 0 2021-03-24 18:59:46.273586607 +0800 c.log
drwxr-xr-x. 2 root root 20 2021-03-25 14:23:14.837459816 +0800 console
-rw-r--r--. 1 root root 12 2021-03-26 10:29:36.934155776 +0800 d.log
-rwx------. 1 root root 1379 2021-03-24 12:23:57.576482018 +0800 ks-script-6_og6455
[root@centos /tmp]#

-g like -l, but do not list owner

# 等于 -l ,但不列出所有者
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -g /data/
total 15360
-rw-r--r-- 1 root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

–group-directories-first

# 包含有很多文件和文件夹的目录,使目录名显示在文件名之前
ls --group-directories-first

-G, –no-group

# ls 命令在与'-G'和‘-l’选项一起将会使用长列表格式列出文件名称但是不带文件所属组名称
ls -lG

-h, –human-readable

# 配合 -l,以易读(人类可阅)的方式显示大小
ls -lh
# 注意: ‘-h’选项使用1024(计算机中的标准)的幂,文件或文件夹的大小分别以K,M和G作为输出单位。
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -lh /data/
total 15M
-rw-r--r-- 1 root root 5.0M Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10M Mar 27 18:38 testfile
[root@centos ~]#

–si likewise, but use powers of 1000 not 1024

# 存在一个选项 ‘--si’与选项‘-h’相似,不同之处在于前者以使用1000的幂,后者使用1024的幂。
[root@centos ~]#ls -lh /data/
total 15M
-rw-r--r-- 1 root root 5.0M Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10M Mar 27 18:38 testfile
[root@centos ~]#ls -l --si /data/
total 16M
-rw-r--r-- 1 root root 5.3M Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 11M Mar 27 18:38 testfile
[root@centos ~]#

-H, –dereference-command-line

follow symbolic links listed on the command line

–dereference-command-line-symlink-to-dir

follow each command line symbolic link that points to a directory
syt

–hide=PATTERN

do not list implied entries matching shell PATTERN (overridden by -a or -A)

–indicator-style=WORD

append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F)

-i, –inode

# 显示文件索引节点号(inode),一个索引节点代表一个文件
[root@centos ~]#ls -li /data/logs/
total 131072
133 -rw-r--r-- 1 root root 20971520 Mar 27 18:38 time.log
131 -rw-r--r-- 1 root root 31457280 Mar 27 18:42 time1.log
135 -rw-r--r-- 1 root root 27262976 Mar 27 18:42 time2.log
137 -rw-r--r-- 1 root root 27262976 Mar 27 18:47 time3.log
138 -rw-r--r-- 1 root root 27262976 Mar 27 18:47 time4.log
[root@centos ~]#

-I, –ignore=PATTERN

#do not list implied entries matching shell PATTERN

-k like –block-size=1K

-l

# 以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名,文件类型,权限模式,硬链接数,所有者,组,文件大小和文件最后修改时间等
[root@centos ~]#ls -l /data/logs/
total 131072
-rw-r--r-- 1 root root 20971520 Mar 27 18:38 time.log
-rw-r--r-- 1 root root 31457280 Mar 27 18:42 time1.log
[root@centos ~]#

-L, –dereference

# 当显示符号链接的文件信息时,显示链接引用而不是链接本身的文件信息,列出文件的链接名

-m

# 用逗号分隔每个文件和目录的命令
[root@centos ~]#ls -m /data/logs/
time.log, time1.log, time2.log, time3.log, time4.log
[root@centos ~]## 注意:
# linux的ls命令当与其选项‘-m’一起使用时可以在打印目录内容时以逗号‘,’分割。由于逗号分割的内容是水平填充的,ls命令不能在垂直列出内容时使用逗号来分割内容。
# 当使用长列表格式时,‘-m’选项就没有什么效果了。
[root@centos ~]#ls -m /data/logs/ -ltr
total 131072
-rw-r--r-- 1 root root 20971520 Mar 27 18:38 time.log
-rw-r--r-- 1 root root 31457280 Mar 27 18:42 time1.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:42 time2.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time3.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time4.log
[root@centos ~]#

-n, –numeric-uid-gid

# 已用户识别码和群组织代替其名称
[root@centos ~]#ls -l /data/logs/
total 131072
-rw-r--r-- 1 root root 20971520 Mar 27 18:38 time.log
-rw-r--r-- 1 root root 31457280 Mar 27 18:42 time1.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:42 time2.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time3.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time4.log
[root@centos ~]#ls -n /data/logs/ # 以数字 0 代替 root
total 131072
-rw-r--r-- 1 0 0 20971520 Mar 27 18:38 time.log
-rw-r--r-- 1 0 0 31457280 Mar 27 18:42 time1.log
-rw-r--r-- 1 0 0 27262976 Mar 27 18:42 time2.log
-rw-r--r-- 1 0 0 27262976 Mar 27 18:47 time3.log
-rw-r--r-- 1 0 0 27262976 Mar 27 18:47 time4.log
[root@centos ~]#

-N, –literal

print raw entry names (don't treat e.g. control characters specially)

-o like -l, but do not list group information

-p, –indicator-style=slash

append / indicator to directories

-q, –hide-control-chars

print ? instead of non graphic characters

–show-control-chars

show non graphic characters as-is (default unless program is 'ls' and output is a terminal)

-Q, –quote-name

# 解释:将ls命令的输出内容用双引号引起来
[root@centos ~]#ls -1 /data/
file.txt
logs
testfile
[root@centos ~]#ls -1Q /data/
"file.txt"
"logs"
"testfile"
[root@centos ~]#

–quoting-style=WORD

use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape

-r, –reverse

# 以倒叙(逆序)方式显示文件和目录
[root@centos ~]#ls -ltr /data/
total 15360
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
[root@centos ~]#

-R, –recursive

# 递归处理,将指定目录下的所有文件及子目录一并显示
[root@centos ~]#tree /data/
/data/
|-- file.txt
|-- logs
| |-- time.log
| |-- time1.log
| |-- time2.log
| |-- time3.log
| `-- time4.log
`-- testfile1 directory, 7 files
[root@centos ~]#ls -lR /data/
/data/:
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile/data/logs:
total 131072
-rw-r--r-- 1 root root 20971520 Mar 27 18:38 time.log
-rw-r--r-- 1 root root 31457280 Mar 27 18:42 time1.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:42 time2.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time3.log
-rw-r--r-- 1 root root 27262976 Mar 27 18:47 time4.log
[root@centos ~]#

-s, –size

# 以区块的形式显示每个文件和目录的大小
[root@centos ~]#ls -s /data/
total 15360
5120 file.txt 0 logs 10240 testfile
[root@centos ~]#du -sh /data/
143M/data/
[root@centos ~]#

-S sort by file size

# 按文件大小(从大到小)的顺序排序
[root@centos ~]#ls -l /data/
total 15360
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#ls -lsSr /data/
total 15360
0 drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
5120 -rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
10240 -rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
[root@centos ~]#

–sort=WORD

# 根据扩展名、大小、时间和版本对目录内容排序。
# 我们可以使用--extension选项来按照扩展名对输出结果排序,--size扩展选项按照大小排序,-t扩展选项按照时间排序,-v扩展选项对版本排序。
# 我们也可以使用--none选项,它会以常规方式输出结果而不会进行排序。
ls --sort=extension
ls --sort=size
ls --sort=time
ls --sort=version
ls --sort=none

–time=WORD

with -l, show time as WORD instead of modification time: atime -u, access -u, use -u, ctime -c, or status -c; use specified time as sort key if --sort=time

–time-style=STYLE


# Linux使用ls命令时,ls -l 默认只显示“月-日”,而不显示“年”,这样就给我们查看文件的时间属性带来困扰,这时侯,我们可以指定time-style(时间显示类型)
# STYLE:full-iso,long-iso,iso,locale,+%H:%M:%S:%D
# 注意 - 在上面行中,H(时),M(分),S(秒),D(日)的顺序可以任意调整。此外,你只需选择那些相关的选项,而不是所有选项。# 将只显示小时
ls -l --time-style=+%H# 将显示小时、分钟和日
ls -l --time-style=+%H:%M:%D
ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
ls -l --time-style="+%b %e %Y$newline%b %e %H:%M"
ls -l --time-style="iso"
ls -l --time-style="locale"
ls -l --time-style=full-iso
ls -l --time-style=long-iso
ls --full-time

–time-style示例:

function check_log()
{
path=/search/miquery
n=`ls $path/log/ie_log/ie_log -l --time-style=+%s | awk '{print $6}'`
def=$((`date +%s` - $n))
#300s内不滚动则报警
if [ $def -gt 300 ]
then
sendMsg "MiQuery logroll stop"
fi
}

-t

# 按文件和目录的更改时间排序
[root@centos ~]#ls -ltr /data/
total 15360
-rw-r--r-- 1 root root 10485760 Mar 27 18:38 testfile
-rw-r--r-- 1 root root 5242880 Mar 27 18:38 file.txt
drwxr-xr-x 2 root root 90 Mar 27 18:47 logs
[root@centos ~]#

-T, –tabsize=COLS

# 通过ls命令为列出的目录内容手动指定的制表符大小而不是默认的8ls --tabsize=4

-u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time

-U do not sort; list entries in directory order

-v natural sort of (version) numbers within text

-w, –width=COLS

# 标准输出结果中打印目录内容中比默认指定的更多栏目。
# 我们可以手动分配屏幕宽度的值和出现的栏目的控制数。这可以通过使用‘--width’开关实现。
#命令:
ls --width 80
ls --width 100
ls --width 150

-x list entries by lines instead of by columns

-X sort alphabetically by entry extension

-1

# 与 -C 选项功能相反,所有输出信息用单列格式输出,不输出为多列
[root@centos ~]#ls -C /data/logs/
time.log time1.log time2.log time3.log time4.log
[root@centos ~]#
[root@centos ~]#ls -1 /data/logs/
time.log
time1.log
time2.log
time3.log
time4.log
[root@centos ~]#

SELinux options:

–lcontext

Display security context.   Enable -l. Lines will probably be too wide for most displays.

-Z, –context

Display security context so it fits on most displays.  Displays only mode, user, group, security context and file name.

–help

# 显示帮助信息
[root@centos ~]#ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file

–version

# 输出版本信息并退出
[root@centos ~]#ls --version
ls (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.Written by Richard M. Stallman and David MacKenzie.
[root@centos ~]#
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896