【shell学习|004】- 常用命令之基础命令之ls详解(没有比这个更全的了,建议收藏)

常用命令

接上一节 【shell学习|003】- 查找文档

Linux 的优势之一是它带有数千个可执行无数功能的实用程序。
无论何时使用 Linux,您都将使用实用程序,无论您是从命令行直接按名称使用它们,还是从菜单或图标间接使用它们。
以下部分讨论了一些最基本和最重要的实用程序;这些命令个人认为是最最常用且必须掌握的

特殊字符

shell中有一些特殊字符,这些字符对shell来说有特殊含义,日常工作中尽量避免在文件中使用到这些特殊字符,这些字符的具体特殊点在本系列后续文章中会逐步介绍,现在先记住即可:

& ; | * ? ' " ‘ [ ] ( ) $ < > { } # / \ ! ~

小tips,特殊字符组成的fork炸弹:(建议在虚拟机中测试)

:()
{
	:|:&
};
:

基础命令

当我们登录到机器上时,默认的路径是我们的家目录;管理员(root)的家目录是/root,其他用户的家目录是/home/username;
本地用户的话这些信息可以在/etc/passwd文件中找到;
ldap用户的家目录则是在ldap上设置的:
/etc/passwd具体内容会在后续用户管理篇介绍

[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

ls:列出文件名

如果要选出linux上最常用的命令,我会投票给ls
个人常用的参数组合:
ls -arlt:按时间反向排序,查看目录下所有文件(最近被修改的文件在最下方)

ls 选项

(没有比这个更全的了,建议收藏):

参数释义中文释义说明
-a,--alldo not ignore entries starting with .不忽略已.开头的文件一般以.开头的文件是隐藏文件,单独ls是看不到的
下面给出lsls -a的对比:
(base) [root@theThree test]# ls
(base) [root@theThree test]# ls -a
. .. .test
(base) [root@theThree test]#            
-A, --almost-alldo not list implied . and ...和..不会被列出这里的.是当前目录,..是上一级目录,也叫父目录;
-a 和-A的对比如下:
(base) [root@theThree test]# ls -a
. .. .test
(base) [root@theThree test]# ls -A
.test
(base) [root@theThree test]#
--authorwith -l, print the author of each file结合-l参数使用,打印每个文件的作者信息-l是显示详细信息,加了–author则额外再显示作者信息:
(base) [root@theThree test]# ls -l
total 4
-rw-r–r-- 1 root root 5 Jun 21 19:19 test
-rw-r–r-- 1 root root 0 Jun 21 19:58 test2
(base) [root@theThree test]# ls -l --author
total 4
-rw-r–r-- 1 root root root 5 Jun 21 19:19 test
-rw-r–r-- 1 root root root 0 Jun 21 19:58 test2
(base) [root@theThree test]#
-b, --escapeprint C-style escapes for nongraphic characters为非图形字符打印 C 风格的转义符如果文件名中有特殊字符的话用-b参数会在特殊字符前加上转移字符\来显示:
(base) [root@theThree test]# touch test\ 3
(base) [root@theThree test]# ls
test test2 test 3
(base)
[root@theThree test]# ls -b
test test2 test\ 3
(base) [root@theThree test]#
--block-size=SIZEscale sizes by SIZE before printing them; e.g., ‘--block-size=M’ prints sizes in units of 1,048,576
bytes; see SIZE format below
在打印之前按 SIZE 缩放尺寸;
例如,’–block-size=M’ 以 1,048,576 字节为单位打印大小;具体可以看下放其他注意项中的SIZE
(base) [root@theThree test]# dd if=/dev/zero of=./test_10M count=10 bs=1M
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied,0.013587 s, 772 MB/s
(base) [root@theThree test]# ls -l
total 10244
-rw-r–r-- 1 root root 5 Jun 21 19:19 test
-rw-r–r-- 1 root root 10485760 Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0 Jun 21 19:58 test2
-rw-r–r-- 1 root root 0 Jun 22 10:02 test 3
(base) [root@theThree test]# ls -lh
total 11M
-rw-r–r-- 1 root root 5 Jun 21 19:19 test
-rw-r–r-- 1 root root 10M Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0 Jun 21 19:58 test2
-rw-r–r-- 1 root root 0 Jun 22 10:02 test 3
(base) [root@theThree test]# ls -l --block-size=M
total 11M
-rw-r–r-- 1 root root 1M Jun 21 19:19 test
-rw-r–r-- 1 root root 10M Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0M Jun 21 19:58 test2
-rw-r–r-- 1 root root 0M Jun 22 10:02 test 3
(base) [root@theThree test]# ls -l --block-size=G
total 1G
-rw-r–r-- 1 root root 1G Jun 21 19:19 test
-rw-r–r-- 1 root root 1G Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0G Jun 21 19:58 test2
-rw-r–r-- 1 root root 0G Jun 22 10:02 test 3
(base) [root@theThree test]# ls -l --block-size=K
total 10244K
-rw-r–r-- 1 root root 1K Jun 21 19:19 test
-rw-r–r-- 1 root root 10240K Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0K Jun 21 19:58 test2
-rw-r–r-- 1 root root 0K Jun 22 10:02 test 3
(base) [root@theThree test]#
-B, --ignore-backupsdo not list implied entries ending with ~不显示以~结尾的备份文件(base) [root@theThree test]# ls
test test_10M test2 test 3
(base) [root@theThree test]# cp test test~
(base) [root@theThree test]# ls
test test~ test_10M test2 test 3
(base) [root@theThreetest]# ls -B
test test_10M test2 test 3
(base) [root@theThree test]#
-cwith -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest first结合-lt使用,按最后修改时间排序,ctime(change time)ls -l 默认是按文件名排序,加上-ltc后会按照ctime排序:
(base) [root@theThree test]# ls -l
total 10248
-rw-r–r-- 1 root root 5 Jun 21 19:19 test
-rw-r–r-- 1 root root 5 Jun 22 10:28 test~
-rw
r–r-- 1 root root 10485760 Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0 Jun 21 19:58 test2
-rw-r–r-- 1 root root 0 Jun 22 10:02 test 3
(base) [root@theThree test]# ls -ltc
total 10248
-rw-r–r-- 1 root root 5 Jun 22 10:28 test~
-rw-r–r-- 1 root root 10485760 Jun 22 10:11 test_10M
-rw-r–r-- 1 root root 0 Jun 22 10:02 test 3
-rw-r–r-- 1 root root 0 Jun 21 19:58 test2
-rw-r–r-- 1 root root 5 Jun 21 19:19 test
(base) [root@theThree test]#
-Clist entries by columns按列列出条目结果和ls相同
--color[=WHEN]colorize the output; WHEN can be ‘never’, ‘auto’, or ‘always’ (the default); more info below为输出着色;WHEN 可以是“从不”、“自动或“总是”(默认值);在这里插入图片描述
-d, --directorylist directories themselves, not their contents列出目录本身,而不是它们的内容这里显示了一个点,表示当前目录:在这里插入图片描述
-D, --diredgenerate output designed for Emacs’ dired mode生成为 Emacs 的 dired 模式设计的输出平时很少用:在这里插入图片描述
-fdo not sort, enable -aU, disable -ls --color不排序,启用 -aU,禁用 -ls --colorls -aU的无色版本在这里插入图片描述
-F, --classifyappend indicator (one of */=>@|) to entries将指示符(/=>@| 之一)附加到条目;ls -F一般结合grep使用,会将目录文件最后加上/符号,链接文件最后加上@符号,可执行文件最后加上在这里插入图片描述
--file-typelikewise, except do not append ‘*’我这边测试结果和-F是一致的,除了对可执行文件最后加上*图中 ls -f 那行忽略在这里插入图片描述
--format=WORDacross -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -Cls --format=对应的单词与后面的参数意义一致;比如 ls --format=across 与ls -意义相同在这里插入图片描述
--full-timelike -l --time-style=full-iso和-l --time-style=full-iso选项结果一致,将文件时间的详情显示出来在这里插入图片描述
-glike -l, but do not list owner和ls -l的结果一致,不过不显示所属者在这里插入图片描述
--group-directories-firstgroup directories before files;
can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping
在文件之前对目录进行分组;可以使用 --sort 选项进行扩充,但任何使用 --sort=none (-U) 都会禁用分组在这里插入图片描述
-G, --no-groupin a long listing, don’t print group names和ls -l的结果一致,不过不显示所属组,注意和-g的区别在这里插入图片描述
-h, --human-readablewith -l, print sizes in human readable format (e.g., 1K 234M 2G)结合 -l参数使用,以人类可读格式打印尺寸(例如,1K 234M 2G)在这里插入图片描述
–silikewise, but use powers of 1000 not 1024和-h一样, 不过使用1000的幂而不是1024在这里插入图片描述
-H --dereference-command-linefollow symbolic links listed on the command line按照命令行上列出的符号链接
-H --dereference-command-line-symlink-to-dirfollow each command line symbolic link that points to a directory遵循指向目录的每个命令行符号链接
-H --hide=PATTERNdo not list implied entries matching shell PATTERN (overridden by -a or -A)不列出与 shell PATTERN 匹配的隐含条目(被 -a 或 -A 覆盖)
-H --indicator-style=WORDappend indicator with style WORD to entry names: none (default), slash (-p), file-type (–file-type),classify (-F)将 WORD 样式的指示符附加到条目名称:无(默认)、斜线 (-p)、文件类型 (–file-type)、分类 (-F)
-i, --inodeprint the index number of each file打印每个文件的索引号在这里插入图片描述
-I, --ignore=PATTERNdo not list implied entries matching shell PATTERN不列出与 shell PATTERN 匹配的隐含条目在这里插入图片描述
-k, --kibibytesdefault to 1024-byte blocks for disk usage默认为 1024 字节块用于磁盘使用,与ls -l结果一致在这里插入图片描述
-luse a long listing format使用长列表格式具体结果的详细意义后续会介绍在这里插入图片描述
-L, --dereferencewhen showing file information for a symbolic link, show information for the file the link references rather than for the link itself当显示符号链接的文件信息时,显示链接引用的文件的信息而不是链接本身的信息在这里插入图片描述
-mfill width with a comma separated list of entries用逗号分隔的条目列表填充宽度在这里插入图片描述
-n, --numeric-uid-gidlike -l, but list numeric user and group IDs和ls -l结果一致,不过显示用户和属组的id号在这里插入图片描述
-N, --literalprint raw entry names (don’t treat e.g. control characters specially)打印原始条目名称(不要特别对待例如控制字符)这个我还没整明白,欢迎大佬们评论区交流
-olike -l, but do not list group information类似 -l,但不列出组信息,注意和ls -G的区别在这里插入图片描述
-p, --indicator-style=slashappend / indicator to directories将 / 指示符附加到目录,功能没-F强大在这里插入图片描述
-q, --hide-control-charsprint ? instead of nongraphic characters打印 ?而不是非图形字符本来以为和-b类似,结果不是,欢迎大佬交流在这里插入图片描述
--show-control-charsshow nongraphic characters as-is (the default, unless program is ‘ls’ and output is a terminal)按原样显示非图形字符(默认值,除非程序是 ‘ls’ 并且输出是终端)在这里插入图片描述
-Q, --quote-nameenclose entry names in double quotes用双引号将条目名称括起来在这里插入图片描述
--quoting-style=WORDuse quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape对条目名称使用引用样式 WORD:文字、语言环境、shell、shell-always、c、escape在这里插入图片描述
-r, --reversereverse order while sorting排序时倒序分析日志时个人习惯使用ls -arlt,最新修改的文件在最下方,十分方便在这里插入图片描述
-R, --recursivelist subdirectories recursively递归列出子目录什么时递归,什么时递归,如果你想知道什么时递归,执行ls -R 在这里插入图片描述
-s, --sizeprint the allocated size of each file, in blocks打印每个文件的分配大小,以块为单位在这里插入图片描述
-Ssort by file size按文件大小排序如果根目录满了的话,使用du定位到目录后,可以结合-S快速定位到文件在这里插入图片描述
--sort=WORDsort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)结合前面的单词后,与后面括号中的参数效果一致在这里插入图片描述
--time=WORDwith -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c); also use specified time as sort key if --sort=time使用 -l,将时间显示为 WORD 而不是默认修改时间:atime 或访问或使用 (-u) ctime 或状态 (-c);如果 --sort=time 也使用指定的时间作为排序键在这里插入图片描述
--time-style=STYLEwith -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is inter‐preted like in ‘date’; if FORMAT is FORMAT1FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with ‘posix-’, STYLE takes effect only outside the POSIX locale使用 -l,使用样式 STYLE 显示时间:full-iso、long-iso、iso、locale 或 +FORMAT;FORMAT 的解释就像在 ‘date’ 中一样;如果 FORMAT 是 FORMAT1FORMAT2,则 FORMAT1 适用于非最近的文件,FORMAT2 适用于最近的文件;如果 STYLE 以 ‘posix-’ 为前缀,则 STYLE 仅在 POSIX 语言环境之外生效在这里插入图片描述
-tsort by modification time, newest first按修改时间排序,最新的在前在这里插入图片描述
-T, --tabsize=COLSassume tab stops at each COLS instead of 8假设制表符停在每个 COLS 而不是 8-T 参数现在无法使用了,不过–tablesize还保留着在这里插入图片描述
-uwith -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time带 -lt:按访问时间排序并示; with -l:显示访问时间并按名称排序;否则:按访问时间排序在这里插入图片描述
-Udo not sort; list entries in directory order不要排序;按目录顺序列出条目在这里插入图片描述
-vnatural sort of (version) numbers within text文本中的自然排序(版本)编号在这里插入图片描述
-w, --width=COLSassume screen width instead of current value假设屏幕宽度而不是当前值在这里插入图片描述
-xlist entries by lines instead of by columns按行而不是按列列出条目在这里插入图片描述
-Xsort alphabetically by entry extension按条目扩展名按字母顺序排序和ls结果一致在这里插入图片描述
-1list one file per line每行显示一个文件在这里插入图片描述
--lcontextDisplay security context. Enable -l. Lines will probably be too wide for most displays.显示安全上下文。启用 -l。对于大多数显示器来说,线条可能太宽了。在这里插入图片描述
-Z, --contextDisplay security context so it fits on most displays. Displays only mode, user, group, security context and file name.显示安全上下文,使其适合大多数显示器。仅显示模式、用户、组、安全上下文和文件名。在这里插入图片描述
--scontextDisplay only security context and file name.仅显示安全上下文和文件名。在这里插入图片描述
--helpdisplay this help and exit打印帮助文档在这里插入图片描述
–versionoutput version information and exit输出ls的版本信息并退出在这里插入图片描述
                                 

其他注意项:

SIZE 是一个整数和可选单位(例如:10M 是 1010241024)。
单位是 K、M、G、T、P、E、Z、Y(1024 的幂)或 KB、MB、…(1000 的幂)。

退出码说明:

0 : 正常,
1 :小问题(例如,无法访问子目录)
2 : 出现严重问题(例如,无法访问命令行参数)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值