常用命令
接上一节 【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,--all | do not ignore entries starting with . | 不忽略已.开头的文件 | 一般以.开头的文件是隐藏文件,单独ls是看不到的 下面给出ls和ls -a的对比: (base) [root@theThree test]# ls (base) [root@theThree test]# ls -a . .. .test (base) [root@theThree test]# |
-A, --almost-all | do not list implied . and .. | .和..不会被列出 | 这里的.是当前目录,..是上一级目录,也叫父目录; -a 和-A的对比如下: (base) [root@theThree test]# ls -a . .. .test (base) [root@theThree test]# ls -A .test (base) [root@theThree test]# |
--author | with -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, --escape | print 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=SIZE | scale 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-backups | do 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]# |
-c | with -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]# |
-C | list entries by columns | 按列列出条目 | 结果和ls相同 |
--color[=WHEN] | colorize the output; WHEN can be ‘never’, ‘auto’, or ‘always’ (the default); more info below | 为输出着色;WHEN 可以是“从不”、“自动或“总是”(默认值); | |
-d, --directory | list directories themselves, not their contents | 列出目录本身,而不是它们的内容 | 这里显示了一个点,表示当前目录: |
-D, --dired | generate output designed for Emacs’ dired mode | 生成为 Emacs 的 dired 模式设计的输出 | 平时很少用: |
-f | do not sort, enable -aU, disable -ls --color | 不排序,启用 -aU,禁用 -ls --color | ls -aU的无色版本 |
-F, --classify | append indicator (one of */=>@|) to entries | 将指示符(/=>@| 之一)附加到条目;ls -F一般结合grep使用,会将目录文件最后加上/符号,链接文件最后加上@符号,可执行文件最后加上 | |
--file-type | likewise, except do not append ‘*’ | 我这边测试结果和-F是一致的,除了对可执行文件最后加上* | 图中 ls -f 那行忽略 |
--format=WORD | across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C | ls --format=对应的单词与后面的参数意义一致;比如 ls --format=across 与ls -意义相同 | |
--full-time | like -l --time-style=full-iso | 和-l --time-style=full-iso选项结果一致,将文件时间的详情显示出来 | |
-g | like -l, but do not list owner | 和ls -l的结果一致,不过不显示所属者 | |
--group-directories-first | group 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-group | in a long listing, don’t print group names | 和ls -l的结果一致,不过不显示所属组,注意和-g的区别 | |
-h, --human-readable | with -l, print sizes in human readable format (e.g., 1K 234M 2G) | 结合 -l参数使用,以人类可读格式打印尺寸(例如,1K 234M 2G) | |
–si | likewise, but use powers of 1000 not 1024 | 和-h一样, 不过使用1000的幂而不是1024 | |
-H --dereference-command-line | follow symbolic links listed on the command line | 按照命令行上列出的符号链接 | |
-H --dereference-command-line-symlink-to-dir | follow each command line symbolic link that points to a directory | 遵循指向目录的每个命令行符号链接 | |
-H --hide=PATTERN | do not list implied entries matching shell PATTERN (overridden by -a or -A) | 不列出与 shell PATTERN 匹配的隐含条目(被 -a 或 -A 覆盖) | |
-H --indicator-style=WORD | append indicator with style WORD to entry names: none (default), slash (-p), file-type (–file-type),classify (-F) | 将 WORD 样式的指示符附加到条目名称:无(默认)、斜线 (-p)、文件类型 (–file-type)、分类 (-F) | |
-i, --inode | print the index number of each file | 打印每个文件的索引号 | |
-I, --ignore=PATTERN | do not list implied entries matching shell PATTERN | 不列出与 shell PATTERN 匹配的隐含条目 | |
-k, --kibibytes | default to 1024-byte blocks for disk usage | 默认为 1024 字节块用于磁盘使用,与ls -l结果一致 | |
-l | use a long listing format | 使用长列表格式 | 具体结果的详细意义后续会介绍 |
-L, --dereference | when showing file information for a symbolic link, show information for the file the link references rather than for the link itself | 当显示符号链接的文件信息时,显示链接引用的文件的信息而不是链接本身的信息 | |
-m | fill width with a comma separated list of entries | 用逗号分隔的条目列表填充宽度 | |
-n, --numeric-uid-gid | like -l, but list numeric user and group IDs | 和ls -l结果一致,不过显示用户和属组的id号 | |
-N, --literal | print raw entry names (don’t treat e.g. control characters specially) | 打印原始条目名称(不要特别对待例如控制字符) | 这个我还没整明白,欢迎大佬们评论区交流 |
-o | like -l, but do not list group information | 类似 -l,但不列出组信息,注意和ls -G的区别 | |
-p, --indicator-style=slash | append / indicator to directories | 将 / 指示符附加到目录,功能没-F强大 | |
-q, --hide-control-chars | print ? instead of nongraphic characters | 打印 ?而不是非图形字符 | 本来以为和-b类似,结果不是,欢迎大佬交流 |
--show-control-chars | show nongraphic characters as-is (the default, unless program is ‘ls’ and output is a terminal) | 按原样显示非图形字符(默认值,除非程序是 ‘ls’ 并且输出是终端) | |
-Q, --quote-name | enclose entry names in double quotes | 用双引号将条目名称括起来 | |
--quoting-style=WORD | use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape | 对条目名称使用引用样式 WORD:文字、语言环境、shell、shell-always、c、escape | |
-r, --reverse | reverse order while sorting | 排序时倒序 | 分析日志时个人习惯使用ls -arlt,最新修改的文件在最下方,十分方便 |
-R, --recursive | list subdirectories recursively | 递归列出子目录 | 什么时递归,什么时递归,如果你想知道什么时递归,执行ls -R |
-s, --size | print the allocated size of each file, in blocks | 打印每个文件的分配大小,以块为单位 | |
-S | sort by file size | 按文件大小排序 | 如果根目录满了的话,使用du定位到目录后,可以结合-S快速定位到文件 |
--sort=WORD | sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X) | 结合前面的单词后,与后面括号中的参数效果一致 | |
--time=WORD | with -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=STYLE | with -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 语言环境之外生效 | |
-t | sort by modification time, newest first | 按修改时间排序,最新的在前 | |
-T, --tabsize=COLS | assume tab stops at each COLS instead of 8 | 假设制表符停在每个 COLS 而不是 8 | -T 参数现在无法使用了,不过–tablesize还保留着 |
-u | with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time | 带 -lt:按访问时间排序并示; with -l:显示访问时间并按名称排序;否则:按访问时间排序 | |
-U | do not sort; list entries in directory order | 不要排序;按目录顺序列出条目 | |
-v | natural sort of (version) numbers within text | 文本中的自然排序(版本)编号 | |
-w, --width=COLS | assume screen width instead of current value | 假设屏幕宽度而不是当前值 | |
-x | list entries by lines instead of by columns | 按行而不是按列列出条目 | |
-X | sort alphabetically by entry extension | 按条目扩展名按字母顺序排序 | 和ls结果一致 |
-1 | list one file per line | 每行显示一个文件 | |
--lcontext | Display security context. Enable -l. Lines will probably be too wide for most displays. | 显示安全上下文。启用 -l。对于大多数显示器来说,线条可能太宽了。 | |
-Z, --context | Display security context so it fits on most displays. Displays only mode, user, group, security context and file name. | 显示安全上下文,使其适合大多数显示器。仅显示模式、用户、组、安全上下文和文件名。 | |
--scontext | Display only security context and file name. | 仅显示安全上下文和文件名。 | |
--help | display this help and exit | 打印帮助文档 | |
–version | output version information and exit | 输出ls的版本信息并退出 | |
其他注意项:
SIZE 是一个整数和可选单位(例如:10M 是 1010241024)。
单位是 K、M、G、T、P、E、Z、Y(1024 的幂)或 KB、MB、…(1000 的幂)。
退出码说明:
0 : 正常,
1 :小问题(例如,无法访问子目录)
2 : 出现严重问题(例如,无法访问命令行参数)