最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【软件开发基础】Linux系统下使用命令时常见的逻辑和用法

Soft Dev Basics crifan 2428浏览 0评论

下面,总结一下,在Linux中,各种命令(工具)使用过程中,相对比较通用的一些逻辑和用法:

1.查看命令帮助(使用说明):xxx –help

比如Linux常见的两个命令:

显示目录下的文件(和文件夹的:ls

拷贝文件的:cp

对应的–help输出的内容为:

Administrator@PC-20130611GART /home/develop/crosstool-ng/src
$ 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      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.
  -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 defaults to `always'
                               or can be `never' or `auto'.  More info below
  -d, --directory            list directory entries instead of contents,
                               and do not dereference symbolic links
  -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.
                               augment 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, print sizes in human readable format
                               (e.g., 1K 234M 2G)
      --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)
      --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            use 1024-byte blocks
  -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 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           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always, c, escape
  -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
      --sort=WORD            sort by WORD instead of name: none -U,
                             extension -X, size -S, time -t, version -v
      --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     with -l, show times using style STYLE:
                             full-iso, long-iso, iso, locale, +FORMAT.
                             FORMAT is interpreted like `date'; if FORMAT is
                             FORMAT1<newline>FORMAT2, 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
  -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
  -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
  -Z, --context              print any SELinux security context of each file
  -1                         list one file per line
      --append-exe           append .exe if cygwin magic was needed
      --help     display this help and exit
      --version  output version information and exit

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

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).

Report ls bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report ls translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'ls invocation'

Administrator@PC-20130611GART /home/develop/crosstool-ng/src
$ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
  -d                           same as --no-dereference --preserve=links
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (redundant if
                                 the -n option is used)
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber             do not overwrite an existing file (overrides
                                 a previous -i option)
  -P, --no-dereference         never follow symbolic links in SOURCE
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: context, links, xattr,
                                 all
      --no-preserve=ATTR_LIST  don't preserve the specified attributes
      --parents                use full source file name under DIRECTORY
  -R, -r, --recursive          copy directories recursively
      --reflink[=WHEN]         control clone/CoW copies. See below
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -s, --symbolic-link          make symbolic links instead of copying
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 copy only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -x, --one-file-system        stay on this file system
      --help     display this help and exit
      --version  output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well.  That is the behavior
selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified.  If this is not possible the copy
fails, or if --reflink=auto is specified, fall back to a standard copy.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

Report cp bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report cp translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'cp invocation'

其中,可以看到,各种参数的用法和含义。

当你对某个命令,不是很熟悉的时候,多去看看help,绝对会有很大帮助。

 

2.-x等价于–yyy

Linux下的命令的参数,多数都是

短横线加上一个字母,即-x

其往往,也有个等价的,两个短横线加上一个单词的,写法。

比如:

ls的-h,表示以人类可读的形式输出内容,等价于–human-readable

示例如下:

Administrator@PC-20130611GART /home/develop/crosstool-ng/src/eglibc-2.10/localedef
$ ls -l -h
total 299K
-rw-r--r--  1 Administrator None  735 Aug 17 17:24 ChangeLog.eglibc
-rw-r--r--  1 Administrator None 5.4K Aug 17 17:24 Makefile.in
-rw-r--r--  1 Administrator None 1.8K Aug 17 17:24 asprintf.c
-rw-r--r--  1 Administrator None  42K Aug 17 17:24 config.guess
-rw-r--r--  1 Administrator None 4.7K Aug 17 17:24 config.h.in
-rw-r--r--  1 Administrator None  31K Aug 17 17:24 config.sub
-rwxr-xr-x  1 Administrator None 170K Aug 17 17:24 configure
-rw-r--r--  1 Administrator None 4.1K Aug 17 17:24 configure.ac
-rw-r--r--  1 Administrator None  610 Aug 17 17:24 getdelim.c
drwxr-xr-x+ 1 Administrator None    0 Aug 17 17:24 include
-rw-r--r--  1 Administrator None 5.5K Aug 17 17:24 install-sh
-rw-r--r--  1 Administrator None  148 Aug 17 17:24 localedef_extra.c
-rw-r--r--  1 Administrator None  405 Aug 17 17:24 obstack_printf.c
-rw-r--r--  1 Administrator None 3.5K Aug 17 17:24 vasprintf.c

Administrator@PC-20130611GART /home/develop/crosstool-ng/src/eglibc-2.10/localedef
$ ls -l --human-readable
total 299K
-rw-r--r--  1 Administrator None  735 Aug 17 17:24 ChangeLog.eglibc
-rw-r--r--  1 Administrator None 5.4K Aug 17 17:24 Makefile.in
-rw-r--r--  1 Administrator None 1.8K Aug 17 17:24 asprintf.c
-rw-r--r--  1 Administrator None  42K Aug 17 17:24 config.guess
-rw-r--r--  1 Administrator None 4.7K Aug 17 17:24 config.h.in
-rw-r--r--  1 Administrator None  31K Aug 17 17:24 config.sub
-rwxr-xr-x  1 Administrator None 170K Aug 17 17:24 configure
-rw-r--r--  1 Administrator None 4.1K Aug 17 17:24 configure.ac
-rw-r--r--  1 Administrator None  610 Aug 17 17:24 getdelim.c
drwxr-xr-x+ 1 Administrator None    0 Aug 17 17:24 include
-rw-r--r--  1 Administrator None 5.5K Aug 17 17:24 install-sh
-rw-r--r--  1 Administrator None  148 Aug 17 17:24 localedef_extra.c
-rw-r--r--  1 Administrator None  405 Aug 17 17:24 obstack_printf.c
-rw-r--r--  1 Administrator None 3.5K Aug 17 17:24 vasprintf.c

Administrator@PC-20130611GART /home/develop/crosstool-ng/src/eglibc-2.10/localedef
$

 

注意:

当给参数加上参数的时候,都是

-x para

等价于

–yyy=para

-x后面有空格,再加上参数

而–yyy后面是等于号,再加上参数。

比如,我之前整理的:

【整理】Linux下的make命令使用心得

中给make加上多线程,就是:

make –j 4

或:

make –jobs=4

 

3.查看命令的版本,一般都是-v(和,或–version)

Linux下想要查看某个命令的版本,多数都是可以通–version的

有些也还可以使用-v或-V,输出版本信息。

比如ls的–version:

Administrator@PC-20130611GART /home/develop/crosstool-ng/src/eglibc-2.10/localedef
$ ls --version
ls (GNU coreutils) 8.15
Packaged by Cygwin (8.15-1)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://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.

cp的–version:

Administrator@PC-20130611GART /home/develop/crosstool-ng/src/eglibc-2.10/localedef
$ cp --version
cp (GNU coreutils) 8.15
Packaged by Cygwin (8.15-1)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://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 Torbj"orn Granlund, David MacKenzie, and Jim Meyering.

 

4.-r或-R,很多时候都是表示递归Recursive的意思

Linux下面,有很多命令,都是用-r或-R,表示递归的意思:

比如ls是用-R,cp是用-R或-r都可以:

ls --help
...
  -R, --recursive            list subdirectories recursively

cp --help
...
-R, -r, --recursive          copy directories recursively

 

5.

转载请注明:在路上 » 【软件开发基础】Linux系统下使用命令时常见的逻辑和用法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.179 seconds, using 22.12MB memory