天天看点

3.Linux命令基础及详解

    在Linux下,我们很多操作都是通过命令来实现的,所接下来我们就详解介绍Linux下命令的使用。首先要使用命令就得先知道他的格式:

    Linux命令的语法格式是  command options arguments,其中选项和参数是可以省略的。Linux下面命令是有多种的,有系统自身的命令、安装的应用程序命令、脚本执行所用的命令。

ls :list directory contents  用来列出目录内容的,

ls /path/to/dir|file[路径]  dir是目录名    file文件名     

[root@linux_basic ~]#ls

anaconda-ks.cfg                               blank     gldt1210.png  install.log.syslog  tcpestd.sh

bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm  blankdir  install.log   tcpestd1.sh         vbird_server

不使用路径,则是显示当前目录下的内容

[root@linux_basic ~]#ls anaconda-ks.cfg   其中anaconda-ks.cfg是文件

anaconda-ks.cfg

查看当前所使用的shell

[root@linux_basic ~]#echo $BASH

/bin/bash

     我们在命令行下操作的是shell,我们输入命令shell把命令提交给内核如果命令可执行,则内核把执行结果给我们返回,其实shell本身也是有内置命令的,可以通过type命令来查看命令是属于内置命令还是外部命令。

内置命令:是有shell程序自身提供的命令

外部命令:其本身是一个独立的可执行程序文件,命令名即是其程序文件名字

那我们输入命令后,系统是如何查找命令的呢?

对于内置命令,则直接到shell内部查找即可;

对于外部命令,则是通过环境变量PATH中指定的路径来查找的

环境变量是指全局的变量,一处定义后,在接下来的操作(不换终端的前提下)都生效,要使其永久生效则要写到配置文件(此处不讲解)中。

      通过type命令来查看所用的命令是内置的还是外部命令

[root@linux_basic ~]#type cd

cd is a shell builtin

[root@linux_basic ~]#type echo

echo is a shell builtin

[root@linux_basic ~]#type type

type is a shell builtin

[root@linux_basic ~]#type cat

cat is hashed (/bin/cat)

在命令的格式中,options是用来调整命令的作用方式的

    有两种格式:长选项和短选项

    其中长选项是不能合并使用的,短选项可以

短选项形如: -l ,  -a, -H   使用多个短选项用空格隔开

长选项形如: --list, --all, --help

    在此处需要主要,有些选项是需要带参数的,形如  useradd -u uid

cd:改变当前工作目录,在不指定选项和参数时,是调到当前用户家目录下的。

[root@linux_basic boot]#cd

[root@linux_basic ~]#pwd

/root

也可以通过cd ~当前家目录下    cd ~username 调到指定用户家目录下

cd - 在前一次所在目录和现在所在目录之间来回进行切换

[root@linux_basic ~]#ls -a

.                .bash_history  .bashrc                                       blankdir      install.log         .mysql_history  tcpestd1.sh  vbird_server

..               .bash_logout   bind-utils-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm  .cshrc        install.log.syslog  .rnd            tcpestd.sh   .viminfo

anaconda-ks.cfg  .bash_profile  blank    

其中有两个独特的路径   .  是指当前目录     ..  上一级的目录

Linux命令行下,还可以通过上下键来查看自己使用过的命令,其实Linux下是有命令历史的,命令历史查看通过history来查看的

如果在命令行上输入信息有错可以通过Ctrl + c在终止当前命令的执行

[root@linux_basic ~]#cd /etc/init.d/jdf^C

[root@linux_basic ~]#

在命令历史中保存的命令是用限制的,可以通过HISTSIZE来查看

[root@linux_basic ~]#echo $HISTSIZE

1000

命令历史保存的文件为用户家目录的.bash_history当中,命令保存是要在用户退出后,才会保存的。

命令历史文件也通过一个环境变量来保存了就是HISTFILE

[root@linux_basic ~]#echo $HISTFILE

/root/.bash_history

[root@linux_basic ~]#type history

history is a shell builtin

显示最近使用过的n条命令,包括当前命令自身

[root@linux_basic ~]#history 10

 1047  cd

 1048  pwd

 1049  ls

 1050  ls -a

 1051  history

 1052  echo $HISTORY

 1053  echo $HISTSIZE

 1054  echo $HISTFILE

 1055  type history

 1056  history 10

为了保证系统安全,不让别人查看到命令历史

可以使用history -c来清空所有的命令历史

也可以通过删除指定位置处的历史命令,要使用选项-d

1055  history 10

 1056  man history

 1057  whatis history

 1058  man 1 history

 1059  history -d 159

 1060  history 5

 1061  history -d 1059

 1062  history

[root@linux_basic ~]#history -d 1059

 1054  type history

 1055  history 10

 1059  history 5

 1060  history -d 1059

 1061  history

 1062  history -d 1059

 1063  history 10

history -a [/path/to/some_history_file]: 手动将当前会话中的命令历史写入指定文件

  [ -w 写入命令历史文件中并且追加到命令历史的列表中 ]   

只有在用户退出时,命令的历史才会自动保存到.bash_history文件中

bash调用命令历史中的命令:

      !#: 执行命令历史中的第#条命令

1064  ls

 1065  cd /boot/

 1066  hitory

 1067  history

 1068  ls

 1069  cd

 1070  history

[root@linux_basic ~]#!1068

ls

      !!: 执行上一条正确执行了的命令

      !string: 执行命令历史中最近一次以string开头的命令;

[root@linux_basic ~]#!s

su - cactiuser

[cactiuser@linux_basic ~]$

      !$: 调用上一条命令的最后一个参数

[root@linux_basic ~]#ls /boot/grub/

device.map     fat_stage1_5  grub.conf         jfs_stage1_5  minix_stage1_5     splash.xpm.gz  stage2         vstafs_stage1_5

e2fs_stage1_5  ffs_stage1_5  iso9660_stage1_5  menu.lst      reiserfs_stage1_5  stage1         ufs2_stage1_5  xfs_stage1_5

[root@linux_basic ~]#ls !$

ls /boot/grub/

e2fs_stage1_5  ffs_stage1_5  iso9660_stage1_5  menu.lst      reiserfs_stage1_5  stage1         ufs2_stage1_5

      ESC, .:功能同上  按Esc键后加'.'号

从使用上述命令我们知道,Linux有如此多的命令又有如此多的参数和选项,我们怎么能记住呢?

所以接下来我们来通过使用帮助可以快速的了解到命令具体都用哪些选项和参数。

其中内置命令的帮助信息都可以通过:  help command来查看的

[root@linux_basic ~]#help cd

cd: cd [-L|-P] [dir]

    Change the shell working directory.

外部命令:

  1、command --help  绝大部分命令都是支持的,获取命令简要帮助信息

[root@linux_basic ~]#cat --help

Usage: cat [OPTION]... [FILE]...

Concatenate FILE(s), or standard input, to standard output.

  -A, --show-all           equivalent to -vET

  -b, --number-nonblank    number nonempty output lines

  -e                       equivalent to -vE

在Linux上命令的帮助信息文件所在的位置是/usr/share/man/位置之一

[root@linux_basic ~]#ls /usr/share/man/

bg  da  el  es  fr  hu  it  ko     man1   man1x  man2x  man3p  man4   man5   man6   man7   man8   man9   mann  overrides  pt     ro  sk  sv  zh_CN

cs  de  en  fi  hr  id  ja  man0p  man1p  man2   man3   man3x  man4x  man5x  man6x  man7x  man8x  man9x  nl    pl         pt_BR  ru  sl  tr  zh_TW

Linux命令显示语言存放的位置

[root@linux_basic ~]#cat /etc/sysconfig/i18n

LANG="en_US.UTF-8"

SYSFONT="latarcyrheb-sun16"

在查看帮助信息时,有显示乱码,可以修改显示的语言

[root@linux_basic ~]#echo $LANG

en_US.UTF-8

# export LANG=en  导出LANG,这里是修改后当前是生效的

Linux帮助文件下有man,其实是manual的缩写的,同样Linux下是遵守可以简写则都简写的规则的。

通过man来查看帮助信息

root@linux_basic ~]#man tty

TTY(1)                           User Commands                          TTY(1)

NAME

       tty - print the file name of the terminal connected to standard input

SYNOPSIS

       tty [OPTION]...

DESCRIPTION

       Print the file name of the terminal connected to standard input.

       -s, --silent, --quiet

              print nothing, only return an exit status

       --help display this help and exit

       --version

              output version information and exit

AUTHOR

       Written by David MacKenzie.

REPORTING BUGS

       Report tty 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 tty translation bugs to <http://translationproject.org/team/>

COPYRIGHT

       Copyright ? 2010 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.

SEE ALSO

       The  full  documentation for tty is maintained as a Texinfo manual.  If the info and tty programs are properly installed at your site, the

       command

              info coreutils 'tty invocation'

       should give you access to the complete manual.

man手册是用章节之分的,可以通过whatis命令查看命令的章节,是从1-9

[root@linux_basic ~]#whatis tty

tty                  (1p)  - return user's terminal name

tty                  (1)  - print the file name of the terminal connected to standard input

tty                  (4)  - controlling terminal

tty ioctl [tty_ioctl] (4)  - ioctls for terminals and serial lines

注意:whatis根据数据库执行查找操作,此库为系统定期更新;可使用makewhatis手动更新;

man查看手册中各部分信息

       手册的段落:

                 NAME: 命令名称

                 DESCRIPTION: 命令功能的详细描述

                 OPTIONS: 所有选项

                 SYNOPSIS: 使用格式

                 EXAMPLES: 使用示例

                 FILES: 与当前命令相关的配置文件

                 SEE ALSO: 可参考的其它手册

帮助中的格式字串:表示命令书写时应安装的格式

     []:可省略

     <>: 不可省略

     |: 二选一或多选一,不能同时出现

      ...: 同类内容可以出现多个

在man下信息很多时,为方便查看,可以使用

上下翻屏

    空格键:向文件尾部翻一屏

    b: 向文件首部翻一屏

    j,回车键:向文件尾部翻一行

    k: 向文件首部翻一行     

    Ctrl+d: 向文件尾部翻半屏

    Ctrl+u: 向文件首部翻并屏

还可以进行字串搜索:

    /string: 从文件首部向尾部进行搜索

    ?string: 从文件尾部向首部进行搜索

        n: 显示找到的下一个   显示寻找字符在下一次出现的位置

        N:显示找到的上一个   显示寻找字符在上一次出现的位置

退出man手册使用:

    q

注意:man能够为除命令之外的配置文件、系统调用、库调用等都能提供帮助手册,它们分别位于不同的章节中;  可以使用man man来查看

[root@linux_basic ~]#man man

man(1)                                                                  man(1)

       man - format and display the on-line manual pages

       man  [-acdDfFhkKtwW]  [--path]  [-m  system]  [-p  string]  [-C config_file] [-M pathlist] [-P pager] [-B browser] [-H htmlpager] [-S sec-

       tion_list] [section] name ...

       man formats and displays the on-line manual pages.  If you specify section, man only looks in that section of the manual.   name  is  nor-

       mally  the  name  of  the manual page, which is typically the name of a command, function, or file.  However, if name contains a slash (/)

       then man interprets it as a file specification, so that you can do man ./foo.5 or even man /cd/foo/bar.1.gz.

       See below for a description of where man looks for the manual page files.

        1: 用户命令  User Commands

        2: 系统调用  System Calls

        3: C库函数     C Library Functions      

        4: 设备和特殊文件   Devices and Special Files

        5: 文件和约定  File Formats and Conventions

        6: 游戏说明   Games et. Al.

        7:杂项     Miscellanea

        8:系统管理员工具  System Administration tools and Deamons

很多应用程序都自带有帮助文档:/usr/share/doc/

    ChangeLog: 程序版本升级的变动情况

    INSTALL: 安装方法说明

    README:程序说明信息

我们学习Linux时,可以多阅读,对应发型版的官方文档

遇到没见过的命令多去gfsoso.com搜索,记住要勤搜索,因为很多问题别人都已经遇到过了

欢迎大家指出修正错误

继续阅读