平时工作中经常用到linux,有时候会用到一些比较有趣的命令或者比较效率酷炫的命令,而这些命令经常忘记,导致再次用的时候还要去查,打算整理一下自己经常用到的命令,以便快速查阅。随着整理,不自觉的就进行了分类,主要分为实用、高效、酷炫,担心整理的内容丢失,所以我决定写三篇博客,并会随着个人的成长不定期更新这三篇博客。

本篇为第一篇,是我认为linux中非常实用的命令,也是我频繁用到的命令。在此提醒大家,相关命令我都会给出简单的描述和经典例子,很多命令不会讲太多知识点,毕竟每一个linux指令深讲都要单独写一篇文章才行,想深入学习某个命令建议大家自行google查阅。

  • ls
  • linux的理念是一切皆文件,查看系统有哪些文件的重要性可想而知,ls(List Directory Contents)就是列出目录,可以说没有ls,你基本上什么都干不了。

    ls 会列出目录,目录中包含文件和文件夹:

    root [~] ls
    file1  file2  folder1  folder2
    root [~] ls folder1
    info.php  test.php

    ls -a 会列出隐藏文件,ls -l 会列出详细信息,由于ls -l 非常有用,一般会简化为ll方便快速使用:

    root [~] ls -a
    .  ..  file1  file2  folder1  folder2  .read
    root [~] ls -l
    total 8
    -rw-rw-r-- 1 root   root      0 Jan  7 20:44 file1
    -rw-rw-r-- 1 root   root      0 Jan  7 20:44 file2
    drwxrwxr-x 2 root   root   4096 Jan  7 20:47 folder1
    drwxrwxr-x 2 root   root   4096 Jan  7 20:44 folder2
    root [~] alias ll
    alias ll='ls -l --color=auto'

    有时候想看整个文件树,-R 命令会非常有帮助,不过请避免文件目录很大很复杂时候使用,因为输出太多帮助不大:

    root [~] ll -R
    .:
    total 8
    -rw-rw-r-- 1 ranshy ranshy    0 Jan  7 20:44 file1
    -rw-rw-r-- 1 ranshy ranshy    0 Jan  7 20:44 file2
    drwxrwxr-x 2 ranshy ranshy 4096 Jan  7 20:57 folder1
    drwxrwxr-x 2 ranshy ranshy 4096 Jan  7 20:44 folder2

    ./folder1:
    total 4
    -rw-rw-r-- 1 ranshy ranshy 0 Jan  7 20:47 info.php
    -rw-rw-r-- 1 ranshy ranshy 8 Jan  7 20:57 test.php

    ./folder2:
    total 0
  • cd
  • cd(change directory),此命令可以进行任意目录的切换,pwd为查看当前目录。

    cd除了接受目录参数外,也支持一些很有用的技巧性参数:

    '进入当前用户home目录'
    root [~] cd ~
    '查看home目录具体路径'
    root [~] pwd
    /root
    '切回到上一次所在目录'
    root [~] cd -
    /home/test
    '切到父级目录'
    root [~] cd ..
    root [~] pwd
    /home

    工作中,经常会用cd切到某个父级目录,这里有一个hack技巧,就是快速切换到某个父级目录:

    '有一个很多层的目录'
    root [~] mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
    root [~] cd /tmp/very/long/directory/structure/that/is/too/deep
    root [~] pwd
    /tmp/very/long/directory/structure/that/is/too/deep
    '一般做法是用一堆../进行多层级切换,不建议这么做'
    root [~] cd ../../../../
    root [~] pwd
    /tmp/very/long/directory/structure
    '我们可以自定义快速切换层级指令,高效多了是不是'
    root [~] alias ..="cd .."
    root [~] alias ..2="cd ../.."
    root [~] alias ..3="cd ../../.."
    root [~] alias ..4="cd ../../../.."
    root [~] alias ..5="cd ../../../../.."

    经常有人会因为自己的小错误导致无法进入到指定目录,比如文件夹某个字母大小写错误、多打了个字母、字母顺序弄反了,这些小错误由于眼神问题有时候很难发现,让人变得暴躁。
    cd命令有一个很人性化的提示,shopt -s cdspell,这个指令会让cd出错时进行纠错提示,是不是很爽:

    '开启cd错误纠正'
    root [~] shopt -s cdspell
    root [~] ls
    file1  file2  folder1  folder2
    '打错了目录名字,会自动纠错进入正确的目录'
    root [~] cd folderr1
    folder1
  • touch 和 mkdir
  • mkdir创建目录,touch为创建文件,它们都不能创建已存在的名字:

    '创建名为folder的文件夹'
    root [~] mkdir folder
    root [~] ls
    folder
    '若文件夹名字含有空格,则需要反斜杠'
    root [~] mkdir my\ folder
    root [~] ll
    total 8
    drwxrwxr-x 2 ranshy ranshy 4096 Jan  7 20:01 folder
    drwxrwxr-x 2 ranshy ranshy 4096 Jan  7 20:01 my folder
    '递归创建多层级目录'
    root [~] mkdir -p dir1/dir2/dir3/dir4/
    '创建一个空文件'
    root [~] touch file
  • chmod 和 chown
  • linux系统下的所有文件,对不同用户都是有不同权限的,而控制用户权限的是chmod指令。介绍chmod指令前,我们首先了解下文件和目录的相关权限。通过ll指令我们可以看到详细信息,这些信息一共有九个,我们以folder为例,具体描述如下:

    mytest [~] ll
    total 4
    drwxr-xr-x. 2 mytest root 4096 Mar 20 14:09 folder
    mytest [~] ls -l | tail -n1 | awk '{gsub(/ /,"\n");print}'
    drwxr-xr-x.  #文件类型和文件权限,第一个字符是文件的类型标志,后9个为权限标志
    2            #硬链接数
    mytest       #文件或目录的拥有者
    root         #文件或目录所属的分组
    4096         #文件或目录的大小
    Mar          #创建月
    20           #创建日
    14:09        #创建具体时间
    folder       #文件或目录的创建名称

    在这里,重点讲解文件类型以及权限,需要注意几个知识点:
    1. 第一个字符代表文件类型,“-”表示普通文件,“d”表示目录,“b”表示块设备文件,“c”为字符设备文件,“l”表示文件链接。
    2. linux文件权限有三种,即可读、可写、可执行,对应的就是rwx,没有权限则用字符“-”标识,对于目录而言可执行代表可以cd进入。
    3. 而文件权限的又分为三组,因此这里由九个字符组成rwxr-xr-x,红色为文件拥有者权限,绿色为文件所属用户组的权限,黄色代表其它用户的权限,root拥有所有文件拥有者的权限。
    4. 文件权限采用八进制设计,各个权限的值代表的数字为r(4)、w(2)、x(1)、-(0),例如folder目录权限rwxr-xr-x对应的数字为755。

    理解了权限相关知识,我们开始真正学习chmod指令,chmod指令可以改变文件的权限,在安全隐私等方面都有很大的用途。chmod指令非常简单,可以直接配合之前所说的八进制,来修改具体的文件或文件夹:

    mytest [~] ls -l
    total 4
    -rw-r--r--. 1 mytest root    0 Mar 20 14:09 file
    drwxr-xr-x. 2 mytest root 4096 Mar 20 14:09 folder
    #修改文件权限为755
    mytest [~] chmod 755 file
    #若想修改目录以及子目录,需要加-R进行递归
    mytest [~] chmod -R 777 file
    mytest [~] ls -l
    total 4
    -rwxr-xr-x. 1 mytest root    0 Mar 20 14:09 file
    drwxrwxrwx. 2 mytest root 4096 Mar 20 14:09 folder

    当然,我们也可以直接使用字符r、w、x来进行权限修改,使用u表示修改所有者权限,使用g表示修改组权限,其它用户(other)使用o来表示,而所有人(all)使用a来表示。使用“+”来表示添加权限,“-”表示减少权限,“=”表示授予权限:

    #给文件的拥有者添加执行权限
    mytest [~] chmod u+x file
    #给组和其它用户减去读权限
    mytest [~] chmod go-r file
    #文件对所有人拒绝访问
    mytest [~] chmod a-rwx file
    #文件对其他人仅只读
    mytest [~] chmod o=r file

    更改文件的所属对象,指令是chown,该指令常用参数如下。
    chown [options] user:group file/folder
    options:
    -R :递归执行,修改对象为目录时,子目录同样被修改

    root [~] ls -l
    total 4
    -rw-r--r--. 1 mytest root    0 Mar 20 14:09 file
    drwxr-xr-x. 2 mytest root 4096 Mar 20 14:09 folder
    #修改文件的用户为root,组为nobody
    mytest [~] chown root:nobody file
    #修改目录的组为nobody
    root [~] chmod :nobody folder
    root [~] ls -l
    total 4
    -rw-r--r--. 1 root nobody    0 Mar 20 14:09 file
    drwxr-xr-x. 2 mytest nobody 4096 Mar 20 14:09 folder

    chgrp指令为修改文件或文件夹的所属组,比chown指令更灵活一下。但是,一般情况下,我们只需要使用chown命令即可完成组更改的任务。
  • cp、mv、rm
  • cp拷贝,mv相当于剪切粘贴,rm删除。

    '对于cp和rm命令,-r/-R/--recursive都代表递归的意思,大小写随便用'
    root [~] cp -r folder1 folder2
    'rm是个容易造成危险的指令,删除的内容很难找回来'
    '这是我从stackoverflow学到的技巧'
    '它会提示你,并且给你停顿的时间让你去后悔取消'
    root [~] alias rm
    alias rm='pwd;read;rm -i'
  • tar
  • tar命令是重要的压缩解压工具,这里列出最常用的几个参数。
    -c:创建新的压缩文档,相当于打包。
    -x:从文档中解压文件,相当于拆包。
    -t:列出文档详细的内容,查看已经备份了哪些文件。
    -z:用gzip压缩或解压,一般格式为.tar.gz或.tgz。
    -j:用bzip2压缩或解压,一般格式为xx.tar.bz2,和gzip相比bzip2可以压缩的更小,但是执行过程更长。
    -v :压缩的过程中显示文件。
    -f :使用文档名,请留意,在f之后要立紧接文档名。

    '创建一个tar包'
    root [~] tar cvf archive_name.tar dirname/
    '解压缩一个tar包'
    root [~] tar xvf archive_name.tar
    '查看tar包里面具体内容'
    root [~] tar tvf archive_name.tar
  • zip和unzip
  • 对于zip大家应该不陌生,在windows环境下,大家经常会对文件进行zip压缩和解压缩,而对应到linux中就是zip和unzip这两个命令。

    有些linux系统默认不自带这两个命令,比如centos,需要自行安装:

    '两个命令需要分别安装'
    root [~] yum install zip
    root [~] yum install unzip

    命令参数非常多,不过我觉得一般都不用,你只需要记得-r是递归可压缩文件夹。

    '压缩当前目录下所有文件和文件夹'
    root [~] zip -r my.zip ./*
    updating: file (deflated 20%)
    updating: file2 (stored 0%)
    updating: folder/ (stored 0%)
      adding: folder/a.txt (deflated 38%)
      adding: folder/test.php (stored 0%)
      adding: folder/info.php (stored 0%)
    '解压到/tmp/目录下面'
    root [~] unzip my.zip -d /tmp/
    Archive:  my.zip
      inflating: /tmp/file              
     extracting: /tmp/file2              
       creating: /tmp/folder/
      inflating: /tmp/folder/a.txt      
     extracting: /tmp/folder/test.php    
     extracting: /tmp/folder/info.php
  • cat
  • cat命令让我们可以快速查看一个文件内容。

    root [~] cat folder3/info.php
    <?php
    phpinfo();
    '-n参数显示行号'
    root [~] cat -n folder3/info.php
         1  <?php
         2  phpinfo();
    '将多个文件内容拼接起来,生成新的文件'
    root [~] cat a.txt b.txt c.txt d.txt >> abcd.txt

    >>和>符号在linux中都代表重定向,它们可以将输出重定向到一个文件中。
    >:如果文件存在,则会删除创建新文件。
    >>:如果文件存在,不会触发删除,将会追加。


    出于安全考虑,我们应该尽量使用>>,避免删除重要文件。

    相应,linux还有个vim命令,此命令非常强大,可以查看编辑一个文件,有些程序员用它当做编辑器,由于vim命令太过于丰富了,本文不做深入讨论,有兴趣的可以参考以下三篇文章:
    linux——vim常用操作(一)
    linux-vim编辑器(二)
    linux-vim宏定义(三)

  • grep
  • 使用grep可以在文件中快速搜索指定的内容,并将所有符合条件的行内容输出到屏幕中。

    'file文件内容如下'
    root [~] cat file
    Hello World!
    I am RanShy!
    Hello RanShy!
    '通过grep查找Hello'
    root [~] grep Hello file
    Hello World!
    Hello RanShy!
    '默认是区分大小写,-i参数可以忽略大小写'
    root [~] grep -i ranshy file  
    I am RanShy!
    Hello RanShy!
    '使用-c参数,则返回匹配到的行数'
    root [~] grep -ic shy file
    2
    '-r/-R/--recursive,递归搜索'
    root [~] grep -r 'h' *
    file:I am RanShy!
    file:Hello RanShy!
    folder/info.php:<?php
    folder/info.php:phpinfo();
    '-v参数代表不匹配此字符串'
    root [~] grep -i hello file | grep -v Ran
    Hello World!
  • find
  • find命令,在指定目录结构中搜索文件并执行指定的操作,它有相当多的查找条件,具有强大的功能,所以它的选项也很多。
    find path -options [-print -exec -ok ...]
    path 为待查找的目录路径,options 为命令选项,它有很多命令参数。
    print 将匹配的文件输出到标准输出,exec 对匹配的文件执行shell命令,ok 和exec的作用类似,它是一种更为安全的模式来执行shell命令,会给出提示让用户来确定是否执行。

    '通过文件名查找当前目录'
    root [~] find -name info.php
    ./folder1/info.php
    '忽略大小写,支持模糊查询'
    root [~] find /etc/ -iname dir_*
    /etc/DIR_COLORS.256color
    /etc/DIR_COLORS.lightbgcolor
    /etc/DIR_COLORS
    '对文件内容进行md5校验'
    root [~] find / -name passwd -exec md5sum {} \;
    d9b9000fe9be6bcb919a27fff432f501  /usr/bin/passwd
    115dafd7158bff85f4370a40e61a619c  /etc/passwd
    1590b6c8be9806d3c30da4994429eb5c  /etc/pam.d/passwd
    '查找用户目录下的空文件'
    root [~] find ~ -empty
    /root/.subversion/auth/svn.ssl.server
    /root/.subversion/auth/svn.ssl.client-passphrase
    /root/.subversion/auth/svn.simple
    /root/.subversion/auth/svn.username
    '查找服务器所有zip包,且大小大于10M的,进行删除'
    root [~] find / -type f -name *.zip -size +1M -exec rm {} \;
  • wget
  • wget命令可以非常方便的下载网络上的资源,比如各种安装包、文档。

    初学者一般只知道wget后面接一个url地址,就可以下载了,其实它还有很多有用的技巧:

    'wget会显示进度,和具体下载过程'
    root [~] wget http://www.vim.org/scripts/download_script.php?src_id=7701
    Resolving www.vim.org... 216.34.181.97
    Connecting to www.vim.org|216.34.181.97|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 50243 (49K) [application/octetstream]
    Saving to: “download_script.php?src_id=7701
    100%[===============>] 50,243      29.1K/s   in 1.7s
    '不过下载下来后名字会根据url而定,并不友好'
    root [~] ll
    total 272
    -rw-r--r-- 1 root   root    50243 Feb  2 14:21 download_script.php?src_id=7701
    '-O参数可以指定文件名字'
    root [~] wget -O my.zip http://www.vim.org/scripts/download_script.php?src_id=7701
    root [~] ll
    -rw-r--r-- 1 root   root   50243 Feb  2 14:30 my.zip
    '--limit-rate可以进行限速'
    root [~] wget --limit-rate=20k http://www.openss7.org/repos/tarballs/strx25-0.9.2.1.tar.bz2
    HTTP request sent, awaiting response... 200 OK
    Length: 3852374 (3.7M) [application/x-bzip2]
    Saving to: “strx25-0.9.2.1.tar.bz2”
    2% [===>            ] 93,726      20.1K/s   in 5s
    '下载被终端,-c可以继续下载'
    '对于大文件的下载非常有用'
    root [~] wget -c http://www.openss7.org/repos/tarballs/strx25-0.9.2.1.tar.bz2
  • yum 和 rpm
  • rpm 是linux的一种软件包名称,以.rmp结尾,rpm包的安装有一个很大的缺点就是文件的关联性太大,有时候装一个软件要安装很多其他的软件包,很麻烦。所以为此RedHat小红帽开发了yum安装方法,他可以彻底解决这个关联性的问题,很方便,只要配置两个文件即可安装,yum并不是一种包而是安装包的软件。

    '安装'
    root [~] yum install zip
    '更新'
    root [~] yum update zip
    '删除'
    root [~] yum remove zip
    '搜索关键字,找到所有可能的安装包'
    root [~] yum search java
    '安装'
    root [~] rpm -ivh ***.rpm
    '更新'
    root [~] rpm -uvh ***.rpm
    '删除'
    root [~] rpm -ev ***.rpm
  • date 和 cal
  • date命令是用来显示时间的,cal命令是用来显示日历的。

    '查看当前时间'
    root [~] date
    Tue Feb  2 22:03:58 CST 2016
    '查看当月日期'
    root [~] cal
        February 2016  
    Su Mo Tu We Th Fr Sa
        1  2  3  4  5  6
     7  8  9 10 11 12 13
    14 15 16 17 18 19 20
    21 22 23 24 25 26 27
    28 29
    '查看1990年8月'
    root [~] cal 8 1990
         August 1990    
    Su Mo Tu We Th Fr Sa
              1  2  3  4
     5  6  7  8  9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31
    '查看今年已经过了多少天了'
    root [~] cal -j
           February 2016      
    Sun Mon Tue Wed Thu Fri Sat
         32  33  34  35  36  37
     38  39  40  41  42  43  44
     45  46  47  48  49  50  51
     52  53  54  55  56  57  58
     59  60
  • uname
  • uname 命令可显示关于计算机的基本信息,有很多参数可选,一般情况下使用-a。
    -a:详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
    -m:主机的硬件(CPU)名
    -n:主机在网络节点上的名称或主机名称
    -r:操作系统内核版本号
    -s:内核名称
    -v:操作系统版本
    -p:处理器类型
    -i:硬件平台类型
    -o:操作系统名

    root [~] uname -a
    Linux CentOS7 2.6.32-358.2.1.el6.x86_64 #1 SMP Wed Mar 13 00:26:49 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux