zoukankan      html  css  js  c++  java
  • Linux 基本命令入门

    什么是shell?

    当我们说到命令行,其实我们是在指shell。shell就是一个程序,它接受键盘命令并将命令传递给操作系统来执行。几乎所有的Linux版本都支持GNU项目的名叫bash的shell程序。

    终端模拟器

    在使用图形用户界面的时候,我们使用终端模拟器(terminal emulator)来与shell交互。KDE使用Konsole,GNOME使用gnome-terminal,虽然它们在我们的菜单中都被简单地称为“终端”。

    在个人计算机出现之前的时代,一个叫做主机(host)的单独的处理机器带有多个叫做终端的界面单元(interface units)。一般主机都身躯庞大,单独存放在一个机房中。当时运行在主机上多是支持多用户的UNIX操作系统。这就意味着只要每个人都有自己的终端,并且终端连接到主机上,那同一时刻可以不止一个人使用计算机。想象一下一个布满终端的房间。它们不是计算机。事实上,它们不过是一个键盘、一个显示器和一些基本的电路。在每个终端背后都是一条的电缆线,插入到地板上的小洞中,从那儿连接到看不见的主机计算机。

    除了这些连接到主机的终端外,有一种特殊的终端。这种终端可以看成主机的一部分,用来管理系统。它就是控制台(console)。即使我们不运行终端模拟器,也有几个终端会话继续在图形界面的背后运行。这些会话叫做虚拟终端(virtual terminals)或者虚拟控制台(virtual consoles),在多数linux系统上,这些会话可以通过按CTRL-ALT-F1到CTRL-ATL-F6来访问。访问其中一个会话时,它呈现出一个登录提示符,可以输入用户名和密码。从一个虚拟终端切换到另一个,按ALT和F1-F6。返回图形界面,可以按ALT-F7。

    开始命令行

    启动终端模拟器!一旦它运行起来,会看到类似于下面的东西:

    --------------------------------------------------------------------

    [me@linuxbox ~]$

    --------------------------------------------------------------------

    这叫做shell提示符(shell prompt),出现在shell准备好接受输入的时候。根据linux版本的不同,它的样子可能会有所不同。它通常包含你的username@machinename,之后是当前的工作目录(the current working directory)和一个$符号。

    如果提示符的最后一个字符不是$符号,而是#(hash mark),那么这个终端会话具有超级用户(superuser)权限。这意味着要不就是我们使用根用户(root user)登录的,要不我们就是选择了一个提供超级用户权限的终端模拟器。

      假设现在所有东西都是正常的,让我们试着敲入命令。在提示符后敲入一些胡言乱语(gibberish)如下:

    --------------------------------------------------------------------

    [me@linuxbox ~]$ kaekfjaeifj

    --------------------------------------------------------------------

         因为这个命令没有任何意义,shell告诉我们找不到命令然后给我们另一次机会:

    --------------------------------------------------------------------

    bash: kaekfjaeifj: command not found

    [me@linuxbox ~]$

    --------------------------------------------------------------------

        我们可以在将几个命令放在一行中,命令间使用分号间隔,如:command1; command2; command3 ...

    命令历史

    我们可以使用方向键中的上 下来在之前输入的命令中选择。大多数linux发行版本默认记录最后输入的500条命令。这叫做命令历史。

    光标移动

    我们可以使用方向键中的左右键移动光标。

    一些简单的命令

    date:显示当前日期和时间,并能修改系统的日期和时间。

    cal: 显示当前月份的日历(calendar)。

    df: 显示当前硬盘驱动器的空闲空间(disk free)。

    free: 显示当前未使用内存的数量。

    exit:结束终端会话。

    pwd: 打印出当前的工作目录 (print name of current working directory)。

    cd: 改变工作目录 (change directory)。

    cd 将工作目录改变成当前用户的主目录(host directory)
    cd - 将工作目录改变成之前的工作目录
    cd ~username 将工作目录改成用户username的主目录

    ls: 列出目录的内容 (list directory contents)。

    file:确定文件类型 (determine file type)。

    less: 查看文件内容 (view file content)。

    cp: 复制文件和目录 (copy files and directories)。

    mv:移动或重命名文件和目录 (move/rename files and directories)。

    mkdir: 创建目录 (make directories)。

    rm: 删除文件和目录 (remove files and directories)。

    ln: 创建硬链接和符号链接 (create hard and symbolic links)。

    type: 表明一个命令是如何解释的 (Indicate how a command name is interpreted)。一个命令是下面四种之一。

    An executable program

    Within this category, programs can be compiled binaries, such as programs written in C and C++, or programs written in

    scripting languages, such as the shell, Perl, Python, Ruby, and so on.

    A command built into the shell itself

    bash supports a number of commands internally called shell builtins. The cd command, for example, is a shell builtin.

    A shell function

    Shell functions are miniature shell scripts incorporated into the environment. We will cover configuring the environment

    and writing shell functions in later chapters, but for now just be aware that they exist.

    An alias

    An alias is a command that we can define ourselves, built from other commands.

    which: 显示哪个可执行文件要被执行 (display which executable program will be executed)。

    man: 显示一个命令的手册页 (display a command's manual page)。

    apropos:显示一系列相似的命令 (display a list of appropriate commands)。

    info: 显示一个命令的info项 (display a command's info entry)。

    whatis:显示一个命令的简要描述 (display a very brief description of a command)。

    alias:给命令创建别名 (create an alias for a command)。格式为:alias name='string'

    id: (display user identity)显示用户身份信息。

    chmod:(change file mode)修改文件的权限模式。1. Octal Representation 2. Symbolic Representation 一般文件或者文件夹的权限主要分成三种:读、写、执行 (r、w、x)。此外还有三种特殊的setuid bit, setgid bit, sticky bit.

    setuid bit 当给一个可执行文件设置了setuid bit时,它将真实用户(实际运行这个程序的用户)的用户ID(effective user id)替换掉程序所有者的ID,如passwd命令程序
    setgid bit 当个一个文件夹设置了setgid bit时,在这个目录下新创建的文件的所属组都是这个文件夹的所属组,而不是文件创建者的所属组。
    sticky  bit linux忽略对文件设置的sticky bit,但当对一个文件夹设置了sticky bit时,除非是文件夹的所有者、文件的所有者和超级用户,否则不能删除和重命名该文件夹下的文件。例如/tmp文件夹

    su: (switch user)切换用户。语法格式:su [-[l]] [user] 或者 su -c 'command'

    chown:(change file owner and group)修改文件的所属人和所属组。语法格式:chown [owner][:[group]] file...

    passwd: (change password)修改密码。语法格式:passwd [user]

    ps: (process)显示当前运行进程的信息。

    top: 按照进程的CPU使用率列出正在运行的进程。

    jobs:列出由当前终端(terminal)发起的任务。jobs命令结果中的+号代表当前任务,-号代表前一个任务。

    fg:(foreground)将一个任务转到前台运行。语法格式:fg %[jobspec] 或者 fg %[command] 

    bg: (background)将一个任务转到后台运行。语法格式:bg %[jobspec] 

          如何将一个前台任务转到后台运行呢?首先按Ctrl+Z暂停前台任务,此时该任务被放到了jobs列表中;然后使用bg命令将其转到后台运行。

    kill: (send a signal to a process)给进程发送信号,控制进程。语法格式:kill [-signal] PID... 注:可以使用 %jobspec 代替PID。

    killall:将信号发给所指定的名字的所有进程,来杀死或者控制它们。语法格式:killall  [-signal] name...

    pstree:(process tree)以进程树的方式列出系统中的进程,并展现进程间的父与子的关系。

    vmstat:(virtual memory state)报告虚拟内存的使用情况,给出内存使用情况、交换页和 I/O 块使用情况等统计信息。

    tload:(print the current system load average to the terminal)将系统当前的平均负载结果实时显示到当前终端上。 注:与此类似,xload绘制动态图表。

    标准输入输出、重定向和管道

    如果非要说一个对高效使用Linux重要的概念,那就是标准输入和输出。基本的想法很简单:每一个基于文本的程序都应该能从任何源接收输入,并将输出可以写入任何目标。为了实现这个想法,Unix/Linux的开发人员设计了一种通用的读取数据的方式叫标准输入(STANDARD INPUT)和两种通用的输出数据的方式叫标准输出(STANDARD OUTPUT)和标准错误(STANDARD ERROR)。在一个Unix/Linux进程内,每一个输入源和输出目标都使用不同的数字(unique number)来表示, 这个数字称为文件描述符 (FILE DESCRIPTOR)。Unix/Linux默认给每个进程提供了在大多数情况下都会使用的三个预设的文件描述符。默认的文件描述符0给标准输入,1给标准输出,2给标准错误输出。

    重定向(redirection):

         标准输出:

               truncate a file (or create a new, empty file):command > filename  append to a file: command >> filename

         标准错误输出:

               truncate a file (or create a new, empty file): command 2> filename append to a file: command 2>> filename

         标准输入: command < filename

         标准输出和标准错误输出到同一个文件:

               truncate a file: 1.  command > filename 2>&1       2. command &> filename

               append to a file: command >> filename 2>&1

         丢弃不需要的输出:

               bit bucket: /dev/null  它是系统的一个特殊文件,只接收输入但并不处理。

     管道(pipelines):  command1 | command2       

     过滤器:准确地说,过滤器就是一行接着一行(一次一行)读写文本数据的程序,从标准输入中读取,向标准输出写出。足够有意思的是,管道的第一个和最后一个程序没必要是过滤器。

         cat: (catenate) 从标准输入复制数据到标准输出。 (输入结束符(EOF)在linux 命令行下是 Ctrl+D, 在windows命令行下是Ctrl+Z

              The -n (number) option will place a line number in front of each line.

        The -b (blank) option is used with -n and tells cat not to number blank lines.

        The -s (squeeze) option replaces more than one consecutive blank line with a single blank line.

        tac:(The name tac is cat spelled backwards.) 从标准输入读入,将行逆序复制到标准输出。

        split:将一个大文件分割成多个小文件。格式为:split [-d] [-a num] [-l lines] [file [prefix]]

        rev:(reverse the characters within each line.) 将每一行中的字符反序。

        head:从文本数据中取最前面的几行文本,默认是10。格式:head [-n lines]

        tail: 从文本数据中取最后几行文本,默认是10。格式: tail [-n lines]  使用 -f 选项,tail会继续监视文件,当有新的内容添加到文件时,它们会立刻显示到显示器上。

        colrm:(column remove) 从标准输入中读入数据,删除指定的列,将结果输出到标准输出。格式: colrm [startcol [endcol]] 

        cmp:(compare) 比较两个文件内容是否一样。按字节比较。语法格式: cmp file1 file2

        comm: 比较两个排过序的文件,按行比较。 语法格式: comm [-123] file1 file2 

        diff: 比较两个文本文件,按行比较。语法格式:diff [-bBiqswy] [-c|-Clines|-u|-Ulines] file1 file2

        sdiff: 并排比较两个文本文件,按行比较。语法格式:sdiff [-bBilsW] [-w columns] file1 file2

        cut:从标准输入中读取数据,保留指定的列,删除其他内容(与colrm相反)。语法格式:cut -c list [file...]  cut -f list [-d delimiter] [-s] [file...]

        paste:将文件按照列拼接在一起。语法格式:paste [-d char...] [file...]

        nl:(number lines)给文本插入行号。 语法格式: nl [-v start] [-i increment] [-b a] [-n ln|rn|rz] [file...]

        wc:(word count)统计行数、单词数和字符数。语法格式:wc [-clLw] [file...]

        expand: 将文本中的tab字符转换成空格。语法格式:expand [-i] [-t size | -t list] [file...]

        unexpand:将文本中的空格折叠成tab字符。语法格式:unexpand [-a] [-t size | -t list] [file...]

        fold:将一行拆成多个短行。语法格式:fold [-s] [-w width] [file...]

        fmt:(format)格式化段落(段落间是用空白行分割的)。语法格式:fmt [-su] [-w width] [file...]

        pr:(print)格式化文本,使之变成适于打印的格式。语法格式:pr [-dtm] [+beg[:end]] [-columns] [-h text] [-l n] [-o margin] [-W width] [file...]

        grep:(global regular expression print)从标准输入中读入,抽取出含有特定模式的行并输出。语法格式:grep [-cilLnrsvwx] pattern [file...]

        sort:给数据排序和检查数据是否有序。语法格式:sort [-dfnru] [-o outfile] [infile...] 或者 sort -c[u] [file]

        look:在按字母表排序的文件中,查找以特定模式开头的行。语法格式:look [-df] pattern file...

        uniq:使所有行都只出现一次(去掉冗余行),选择重复出现的行,选择非重复出现的行,计算重复出现的次数。语法格式:uniq [-cdu] [infile [outfile]]

        join:根据两个文件中的列的内容对两个文件进行连接。语法格式: join [-i] [-a1|-v1] [-a2|-v2] [-1 field1] [-2 field2] file1 file2

        tsort:拓扑排序。语法格式: tsort [file]

        string:在二进制文件中查找字符串。语法格式:strings [-length] [file...]

        tr:(translate)将字符替换成别的字符。语法格式:tr [-cds] [set1 [set2]]

        sed:(stream editor)一种重要的非交互文本编辑器。语法格式:sed [-i] command | -e command... [file...]

        tee:(T)从标准输入读,写到标准输出和文件。语法格式:tee file

  • 相关阅读:
    [BZOJ 2820]YY的GCD
    [POI 2007]ZAP-Queries
    [USACO 04OPEN]MooFest
    [HAOI 2011]Problem b
    [COGS 2258][HZOI 2015]复仇的序幕曲
    [UOJ 41]【清华集训2014】矩阵变换
    [POJ 3487]The Stable Marriage Problem
    [POJ 3252]Round Numbers
    [COGS 1799][国家集训队2012]tree(伍一鸣)
    [SDOI 2011]计算器
  • 原文地址:https://www.cnblogs.com/purgiant/p/3488234.html
Copyright © 2011-2022 走看看