初学者的Linux常用命令基础

Linux常用命令精讲

Linux命令概述

在 Linux 操作系统中,凡是在字符操作界面中输入能够完成特定操作和任务的字符串都可以称为命令。严格来说,命令通常只代表实现某一类功能的指令或程序的名称。

本节将学习 Linux 命令的分类、基本格式及如何获得命令帮助。

Linux命令的分类

Linux 命令的执行必须依赖于 Shell 命令解释器。Shell 实际上是在 Linux 操作系统中运行的一种特殊程序,它位于操作系统内核与用户之间,负责接收用户输入的命令并进行解释, 将需要执行的操作传递给系统内核执行,Shell 在用户和内核之间充当了“翻译官”的角色。当用户登录到 Linux 系统时,会自动加载一个 Shell 程序,以便给用户提供可以输入命令的操作系统。

​ Bash 是 Linux 操作系统中默认使用的 Shell 程序,文件位于/bin/bash。关于 Shell 程序的更多知识将在后续课程中进行讲解。根据 Linux 命令与 Shell 程序的关系,Linux 命令一般分为以下两种类型。

​ Ø 内部命令:是指集成于 Shell 解释器程序(如 Bash)内部的一些特殊指令,也称为内建(Built-in)指令。内部命令属于 Shell 的一部分,所以没有单独对应的系统文件,只要 Shell 解释器被运行,内部指令也就自动载入内存了,用户可以直接使用。内部命令无须从硬盘中重新读取文件,因此执行效率较高。

​ Ø 外部命令:是指 Linux 操作系统中能够完成特定功能的脚本文件或二进制程序,每个外部命令对应系统中的一个文件,是属于 Shell 解释器程序之外的命令,所以称为外部命令。Linux 操作系统必须知道外部命令对应的文件位置,才能够由 Shell 加载并执行。

​ Linux 操作系统默认会将存放外部命令、程序的目录(如/bin、/usr/bin、/usr/local/bin 等)添加到用户的“搜索路径”中,当使用位于这些目录中的外部命令时,用户不需要指定具体的位置。因此在大多数情况下,不用刻意去分辨内部命令和外部命令,其使用方法是基本类似的。

命令行的格式

在使用内部命令或外部命令时,参照一个通用的命令行使用格式,可方便理解 Linux 命令的作用和工作方式。通用的命令行使用格式如下:

命令字 [选项] [参数]

其中,命令字、选项、参数之间用空格分开,多余的空格将被忽略。[ ]括起来的部分表示可以省略,即命令行可以只有命令字,也可以只有命令字、选项,或者只有命令字、参数。 下面分别介绍这三个组成部分的含义和作用。

命令字

命令字即命令名称,是整条命令中最关键的一部分。在 Linux 的字符操作界面中,使用命令字唯一确定一条命令,因此在输入命令时一定要确保输入的命令字正确。并且,在 Linux 的命令环境中,无论是命令名还是文件名,对英文字符的处理是区分大小写的,操作时需要 细心。

选项

选项的作用是调节命令的具体功能,决定这条命令如何执行。同一个命令字配合不同的 选项使用时,可以获得相似但具有细微差别的功能。命令使用的选项有如下一些特性。

  • 不同的命令字能够使用的选项也会不同(选项的个数和内容)。

  • 选项的数量可以是多个,也可以省略。同时使用多个选项时,选项之间使用空格分 隔。若不使用选项,将执行命令字的默认功能。

  • 使用单个字符的选项时,一般在选项前使用“-”符号(半角的减号符)引导,称为短 格式选项,如“-l”。多个单字符选项可以组合在一起使用,如“-al”等同于“-a -l”。

  • 使用多个字符的选项时,一般在选项前使用“–”符号(两个半角的减号符)引导, 称为长格式选项,如“–help”。

有些命令字对于同一功能会同时提供长、短两种格式的选项。长格式的选项意义明确, 容易记忆,而短格式的选项结构简单、输入快捷。两种格式可以混用。

参数

​ 命令参数是命令字的处理对象,通常情况下命令参数可以是文件名、目录(路径)名 或用户名等内容。根据所使用命令字的不同,命令参数的个数可以是零到多个。在输 入一条 Linux 命令时,应根据该命令字具体的格式提供对应的命令参数,以确保命令的正常运行。

​ 注意:在实际使用 Linux 命令行的过程中,“选项”和“参数”的称谓经常混淆,甚至前后顺序也允许颠倒,但一般不会影响命令的执行效果,所以很多时候并不做严格区分。

​ 在按照上述格式输入一条 Linux 命令后,按 Enter 键表示输入结束并提交给系统执行。

​ 在没有按 Enter 键之前,命令行中的字符内容都处于编辑状态,可以进行任意编辑修改。编辑 Linux 命令行时,可以使用以下几个辅助操作,以提高输入效率。

  • Tab 键:可以将输入的不完整命令字或文件、目录名自动补齐,如输入“ifcon”后按Tab 键,即可自动补齐为“ifconfig”命令字。该功能只能向后补齐,且必须以已输入的部分字符开头,能够唯一定位一个命令字或文件、目录名,否则可按两次 Tab 键,系统将输出可用的名称列表。

  • 反斜杠“\”:如果输入的一行命令内容太长,终端会自动换行。有时候为了显示美观 及方便查看,也可以插入“\”符号强制换行,在下一行出现的“>”提示符后可以继续输入内容,作为上一行命令的延续。

  • Ctrl+U 组合键:快速删除当前光标处之前的所有字符内容。

  • Ctrl+K 组合键:快速删除从当前光标处到行尾的所有字符内容。

  • Ctrl+L 组合键:快速清空当前屏幕中的显示内容,只在左上角显示命令提示符。

  • Ctrl+C 组合键:取消当前命令行的编辑,并切换为新的一行命令提示符。

​ 从下面开始,将逐渐接触到更多的命令行应用实例。大家可以结合各个命令的具体用法 来熟悉 Linux 命令行的基本格式及相关快捷操作。

获得命令帮助

Linux 命令行的通用格式有助于在学习命令的过程中举一反三,了解使用大多数命令的基本方法。Linux 操作系统中能够使用的命令数量繁多,具体选项也各不相同,使用格式也可能存在细微区别。教材中介绍的内容毕竟有限,对于 Linux 命令的更多详细选项及具体使用格式,除了查阅书本、手册和上网查询之外,最简单、快速的方法是使用命令的在线帮助 功能。下面介绍几种常用的使用在线帮助的方法。

Linux help命令

help 命令本身是 Linux Shell 中的一个内建指令,其用途是查看各 Shell 内部命令的帮助信息。使用 help 命令时,只需要添加内部命令的名称作为参数即可。例如,执行“help pwd” 命令可以查看 Shell 内部命令 pwd 的帮助信息(pwd 命令用于显示当前用户所在的工作目录),具体操作如下:

[root@localhost ~]# help pwd
pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L	print the value of $PWD if it names the current working
    	directory
      -P	print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.
    
    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.

对于大多数 Linux 外部命令,可以使用一个通用的命令选项“–help”来显示对应命令字的格式及选项等帮助信息。若该命令字没有“–help”选项,一般只会提示简单的命令格式。例如,执行“ls --help”命令可以查看 ls 命令的帮助信息(ls 命令用于显示文件或目录列表信息),具体操作如下:

[root@localhost ~]# 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 can be 'never', 'auto',
                               or 'always' (the default); 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, 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            default to 1024-byte blocks for disk usage
  -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 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, 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), 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
      --time-style=STYLE     with -l, show times using style STYLE:
                               full-iso, long-iso, iso, locale, or +FORMAT;
                               FORMAT is interpreted like in 'date'; if FORMAT
                               is FORMAT1<newline>FORMAT2, 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
  -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
  -1                         list one file per line

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.
  --scontext                 Display only security context and file name.
      --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).

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'ls invocation'

使用man手册页

man 手册页(Manual Page)是 Linux 操作系统中最为常用的一种在线帮助形式,绝大部分的外部软件在安装时为执行程序、配置文件提供了详细的帮助手册页。这些手册页中的信息按照特定的格式进行组织,通过统一的手册页浏览程序 man 进行阅读。例如,执行“man file”命令可以查看 file 命令的手册页信息(file 命令用于判断文件的类型),具体操作如下:

[root@localhost ~]# man file
FILE(1)                                BSD General Commands Manual                               FILE(1)

NAME
     file ? determine file type

SYNOPSIS
     file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-type] [-e testname] [-F separator]
          [-f namefile] [-m magicfiles] file ...
     file -C [-m magicfiles]
     file [--help]

DESCRIPTION
     This manual page documents version 5.11 of the file command.

     file tests each argument in an attempt to classify it.  There are three sets of tests, performed in
     this order: filesystem tests, magic tests, and language tests.  The first test that succeeds causes
     the file type to be printed.

     The type printed will usually contain one of the words text (the file contains only printing char?
     acters and a few common control characters and is probably safe to read on an ASCII terminal),
     executable (the file contains the result of compiling a program in a form understandable to some
     UNIX kernel or another), or data meaning anything else (data is usually “binary” or non-printable).
     Exceptions are well-known file formats (core files, tar archives) that are known to contain binary
     data.  When modifying magic files or the program itself, make sure to preserve these keywords.
     Users depend on knowing that all the readable files in a directory have the word “text” printed.
     Don't do as Berkeley did and change “shell commands text” to “shell script”.

     The filesystem tests are based on examining the return from a stat(2) system call.  The program
     checks to see if the file is empty, or if it's some sort of special file.  Any known file types
     appropriate to the system you are running on (sockets, symbolic links, or named pipes (FIFOs) on
     those systems that implement them) are intuited if they are defined in the system header file
     <sys/stat.h>.

     The magic tests are used to check for files with data in particular fixed formats.  The canonical
     example of this is a binary executable (compiled program) a.out file, whose format is defined in
 Manual page file(1) line 1 (press h for help or q to quit)
......

在阅读 man 手册页时将以全屏的文本方式显示,并且提供了交互式的操作环境。按↑、

↓方向键可以向上、向下滚动一行文本内容;按 Page Up 键和 Page Down 键可以向上、向下翻页显示;按 Q 键或 q 键可以随时退出手册页的阅读环境;按/键后可以对手册内容进行

查找,如输入“/-v”可以查找到“-v”选项的帮助信息,若找到的结果有多个,还可以按 n 键或

N 键分别向下、向上进行定位选择。

如果需要将 man 手册页的内容保存成文本文件以便在其他系统中查看,可以结合 col 命令去除手册页中的格式控制字符,并将显示结果保存为新的文件。例如,以下操作可以将 ls 命令的帮助手册保存为文本文件 lshelp.txt。

[root@localhost ~]# *man ls | col -b > lshelp.txt*

在这个例子中,col 是一个命令程序,用于过滤文本中的一些特殊控制字符;另外还使用了“|”和“>”符号,这是 Shell 环境中的两种特殊功能应用,分别称为管道、重定向输出。其含义及作用如下所述。

Ø 管道:用竖杠符号“|”表示,用于将前面命令的屏幕输出结果作为后面命令的操作对象(输入)。就好像一根管道一样,从这头塞入内容,从另一头出来。

Ø 重定向输出:用大于符号“>”表示,前面的命令成功执行以后,其屏幕输出结果将保存到“>”号后边指定的文件中,而不是直接输出到屏幕,因此称为重定向。


Linux grep 命令

Linux grep 命令用于查找文件里符合条件的字符串。

grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示出来。若不指定任何文件名称,或是所给予的文件名为 -,则 grep 指令会从标准输入设备读取数据

  • -i : 忽略字符大小写的差别
  • -l : 列出文件内容符合指定的样式的文件名称
  • -v : 显示不包含匹配文本的所有行
  • -s : 不显示错误信息
  • -V : 显示版本信息
  • -n : 在显示符合样式的那一行之前,标示出该行的列数编号

Linux find 命令

Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示

find / #从根目录下开始查找

find ./#从当前目录下开始查找

举例:

将目前目录及其子目录下所有延伸档名是 aaa 的文件列出来

# find . -name "*.aaa"

将目前目录及其子目录下所有最近 10 天内更新过的文件列出

# find . -ctime -10

查找系统中所有文件长度为0的普通文件

# find / -type f -size 0

Linux sort命令

Linux sort命令用于将文本文件内容加以排序。

sort可针对文本文件的内容,以行为单位来排序

  • b 忽略每行前面开始出的空格字符

  • d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符

  • f 排序时,将小写字母视为大写字母

  • i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符

  • n 依照数值的大小排序

  • r 以相反的顺序来排序


Linux uniq 命令

Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用

uniq 可检查文本文件中重复出现的行列

  • help 显示帮助

  • version 显示版本信息

  • c 在每列旁边显示该行重复出现的次数

  • d 仅显示重复出现的行列

  • u 只显示文件中不重复的行


Linux comm 命令

Linux comm 命令用于比较两个已排过序的文件。

这项指令会一列列地比较两个已排序文件的差异,并将其结果显示出来,如果没有指定任何参数,则会把结果分成 3 列显示:第 1 列仅是在第 1 个文件中出现过的列,第 2 列是仅在第 2 个文件中出现过的列,第 3 列则是在第 1 与第 2 个文件里都出现过的列。若给予的文件名称为 - ,则 comm 指令会从标准输入设备读取数据。

  • -1 不显示只在第 1 个文件里出现过的列
  • -2 不显示只在第 2 个文件里出现过的列
  • -3 不显示只在第 1 和第 2 个文件里出现过的列
  • -help 在线帮助
  • -version 显示版本信息

Linux cp命令

Linux cp命令主要用于复制文件或目录

  • -a:此选项通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容
  • -d:复制时保留链接
  • -f:覆盖已经存在的目标文件而不给出提示
  • -i:与-f选项相反,在覆盖目标文件之前给出提示,要求用户确认是否覆盖,回答"y"时目标文件将被覆盖
  • -p:除复制文件的内容外,还把修改时间和访问权限也复制到新文件中
  • -r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件
  • -l:不复制文件,只是生成链接文件。

Linux wc命令

Linux wc命令用于计算字数。

利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。

  • -c或–bytes或–chars 只显示Bytes数
  • -l或–lines 只显示行数
  • -w或–words 只显示字数
  • –help 在线帮助
  • –version 显示版本信息

Linux mkdir命令

Linux mkdir命令用于创建文件夹

  • m 数字 对新建目录设置存取权限,存取限用8进制
  • p 递归创建

Linux rmkdir命令

Linux rmkdir命令用于删除文件夹

  • p 递归删除目录

Linux cd命令

Linux cd命令用于切换当前工作目录至 dirName(目录参数)。

其中 dirName 表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的 home 目录 (也就是刚 login 时所在的目录)。

另外,"~" 也表示为 home 目录 的意思,"." 则是表示目前所在的目录,"…" 则表示目前目录位置的上一层目录

举例:

进入/opt目录

cd /opt

回到上一层目录

cd ..

回到root(回到当前的家目录)

cd ~

回到上一次目录

cd - 

Linux ls命令

Linux ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录)

  • -a 显示所有文件及目录 (ls内定将文件名或目录名称开头为"."的视为隐藏档,不会列出)
  • -l 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出
  • -r 将文件以相反次序显示(原定依英文字母次序)
  • -t 将文件依建立时间之先后次序列出
  • -A 同 -a ,但不列出 “.” (目前目录) 及 “…” (父目录)
  • -F 在列出的文件名称后加一符号;例如可执行档则加 “*”, 目录则加 “/”
  • -R 若目录下有文件,则以下之文件亦皆依序列出

Linux ln命令

Linux ln命令是一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。

当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在 其它的目录下用ln命令链接(link)它就可以,不必重复的占用磁盘空间

  • -b 删除,覆盖以前建立的链接
  • -d 允许超级用户制作目录的硬链接
  • -f 强制执行
  • -i 交互模式,文件存在则提示用户是否覆盖
  • -n 把符号链接视为一般目录
  • -s 软链接(符号链接)
  • -v 显示详细的处理过程

命令功能 :
Linux文件系统中,有所谓的链接(link),我们可以将其视为档案的别名,而链接又可分为两种 : 硬链接(hard link)与软链接(symbolic link),硬链接的意思是一个档案可以有多个名称,而软链接的方式则是产生一个特殊的档案,该档案的内容是指向另一个档案的位置。硬链接是存在同一个文件系统中,而软链接却可以跨越不同的文件系统。

不论是硬链接或软链接都不会将原本的档案复制一份,只会占用非常少量的磁碟空间

软链接

  • 1.软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
  • 2.软链接可以 跨文件系统 ,硬链接不可以
  • 3.软链接可以对一个不存在的文件名进行链接
  • 4.软链接可以对目录进行链接

硬链接

  • 1.硬链接,以文件副本的形式存在。但不占用实际空间。
  • 2.不允许给目录创建硬链接
  • 3.硬链接只有在同一个文件系统中才能创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值