linux常用命令格式

Linux命令由 ‘命令+选项+参数’组成

参数是指命令的操作对象,一般文件、目录、用户和进程等可以作为参数被命令操作。

[10:03:40 mo@Centos8\ [ ~]$ls -a ./data
.  ..  a.txt  b.txt  lsa.log  mima
[10:03:49 mo@Centos8\ [ ~]$

Linux命令的基本格式及ls选项

1.以ls(列出目录)命令为例,直接敲入命令会列出当前目录下的内容

ls 命令后面不加选项和参数也能执行,不过只能执行最基本的功能

[09:37:36 mo@Centos8\ [ ~]$ls
data  dead.letter  Desktop  Documents  Downloads  mail  Music  Pictures  Public  Templates  Videos


2.加入参数后可以显示更加丰富的数据

例:

ls -l

-l :长数据串列出,包含文件的属性与权限等等数据

[09:40:14 mo@Centos8\ [ ~]$ls -l
total 12
drwxrwxr-x. 2 mo mo   59 Jan 20 20:24 data
-rw-------. 1 mo mo 4773 Jan 15 10:54 dead.letter
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Desktop
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Documents
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Downloads
-rw-rw-r--. 1 mo mo   32 Jan 12 20:38 mail
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Music
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Pictures
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Public
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Templates
drwxr-xr-x. 2 mo mo    6 Dec 16 16:26 Videos

3.选项又分为短格式选项(-a)和长格式选项(--all)。

4.短格式选项是英文的简写,用一个减号调用,长格式选项是英文完整单词,一般用两个减号调用。(是不所有选项都有长格式)

[09:40:20 mo@Centos8\ [ ~]$ls -a
.              .bash_logout   .cache   dead.letter  Downloads    .ICEauthority  mail      Music     Public     .viminfo
..             .bash_profile  .config  Desktop      .esd_auth    .local         .mailrc   Pictures  Templates  .Xauthority
.bash_history  .bashrc        data     Documents    .fontconfig  .log           .mozilla  .pki      Videos
[09:42:08 mo@Centos8\ [ ~]$ls --all
.              .bash_logout   .cache   dead.letter  Downloads    .ICEauthority  mail      Music     Public     .viminfo
..             .bash_profile  .config  Desktop      .esd_auth    .local         .mailrc   Pictures  Templates  .Xauthority
.bash_history  .bashrc        data     Documents    .fontconfig  .log           .mozilla  .pki      Videos

5.选项可以多个并用

例:

ls -α -l

-a :全部的文件,连同隐藏( 开头为 . 的文件) 一起列出来

-l :长数据串列出,包含文件的属性与权限等等数据

[09:42:57 mo@Centos8\ [ ~]$ls -a -l
total 76
drwx------. 17 mo   mo    4096 Jan 26 09:37 .
drwxr-xr-x.  6 root root    58 Jan 20 20:00 ..
-rw-------.  1 mo   mo   23702 Jan 25 09:50 .bash_history
-rw-r--r--.  1 mo   mo      18 Jul 21  2020 .bash_logout
-rw-r--r--.  1 mo   mo     216 Dec 19 08:23 .bash_profile
-rw-r--r--.  1 mo   mo     402 Jan  1 21:27 .bashrc
drwx------. 14 mo   mo    4096 Dec 16 20:39 .cache
drwx------. 15 mo   mo     278 Dec 16 20:40 .config
drwxrwxr-x.  2 mo   mo      59 Jan 20 20:24 data
-rw-------.  1 mo   mo    4773 Jan 15 10:54 dead.letter
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Desktop
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Documents
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Downloads
-rw-------.  1 mo   mo      16 Dec 16 16:26 .esd_auth
drwxr-xr-x.  2 mo   mo      79 Dec 16 20:34 .fontconfig
-rw-------.  1 mo   mo    4058 Dec 22 12:09 .ICEauthority
drwx------.  3 mo   mo      19 Dec 16 16:26 .local
-rw-rw-r--.  1 mo   mo       0 Dec 21 14:37 .log
-rw-rw-r--.  1 mo   mo      32 Jan 12 20:38 mail
-rw-rw-r--.  1 mo   mo     165 Jan 15 09:19 .mailrc
drwxr-xr-x.  6 mo   mo      81 Dec 16 20:32 .mozilla
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Music
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Pictures
drwxrw----.  3 mo   mo      19 Dec 16 16:26 .pki
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Public
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Templates
drwxr-xr-x.  2 mo   mo       6 Dec 16 16:26 Videos
-rw-------.  1 mo   mo    1042 Dec 28 09:54 .viminfo
-rw-------.  1 mo   mo     384 Jan 26 09:37 .Xauthority

6.--help获取当前命令可用选项,

  • 并不是所有的帮助信息都是--help,相同作用的选项还有-h(有时候是-h,有时候是--help,部分命令不太相同,--help较多),
  • 类似的命令还要info + [选项] 命令,可以查看帮助信息.
  • 还有一种就是查看帮助文档,即man命令,相关的命令有 whatis + 命令 可以查看命令的文档位置,在第几章节,使用man + 章节数 + 命令可以查看命令的文档,
  • 新安装的命令文档不会立刻加载进man数据库,可以运行 mandb 自行加载进去
[09:58:30 mo@Centos8\ [ ~]$whatis ls
ls (1)               - list directory contents
ls (1p)              - list directory contents
[09:58:37 mo@Centos8\ [ ~]$man 1 ls
[09:59:46 mo@Centos8\ [ ~]$

[09:59:46 mo@Centos8\ [ ~]$man 1p ls
[10:02:07 mo@Centos8\ [ ~]$

例:

ls --help

[09:44:13 mo@Centos8\ [ ~]$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
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -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
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
      --si                   likewise, but use powers of 1000 not 1024
  -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
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
  -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
  -N, --literal              print entry names without quoting
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (overrides QUOTING_STYLE environment variable)
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size, largest first
      --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 (newest first)
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
posted @ 2021-01-26 10:07  不聪明的小子  阅读(147)  评论(0编辑  收藏  举报