Linux基础命令 2017-07-12日课

Linux基础命令学习总结,随着学习的前进,将不断完善。网友们,有错误之处,请给我留言改正喔,谢谢!

基础操作

<a href="#clear">clear</a> <a href="#hostname">hostname</a> <a href="#tty">tty</a> <a href="#who">who</a> <a href="#whoami">whoami</a> <a href="#w">w</a> <a href="#whatis">whatis</a> <a href="#which">which</a> <a href="#whereis">whereis</a> <a href="#hash">hash</a> <a href="#enable">enable</a> <a href="#alias">alias</a> <a href="#unalias">unalias</a> <a href="#date">date</a> <a href="#clock">clock</a> <a href="#hwclock">hwclock</a> <a href="#cal">cal</a> <a href="#ldd">ldd</a> <a href="#uname">uname</a> <a href="#timedatectl">timedatectl</a> <a href="#screen">screen</a> <a href="#script">script</a> <a href="#chvt">chvt</a> <a href="#man">man</a>

文件、目录、磁盘、文件系统

<a href="#cd">cd</a> <a href="#pwd">pwd</a> <a href="#ls">ls</a> <a href="#cat">cat</a> <a href="#tac">tac</a> <a href="#nl">nl</a> <a href="#head">head</a> <a href="#tail">tail</a> <a href="#less">less</a> <a href="#more">more</a> <a href="#hexdump">hexdump</a> <a href="# od"> od</a> <a href="#cp">cp</a> <a href="#mv">mv</a> <a href="#rm">rm</a> <a href="#mkdir">mkdir</a> <a href="#rmdir">rmdir</a> <a href="#dd">dd</a> <a href="#gedit">gedit</a> <a href="#nano">nano</a> <a href="#file">file</a> <a href="#type">type</a> <a href="#iconv">iconv</a>

用户和组

<a href="#id">id</a> <a href="#getent">getent</a>

BASHELL 特性

<a href="#echo">echo</a> <a href="#history">history</a> <a href="#hash">hash</a> <a href="#history">history</a>

网络属性

<a href="#ifconfig">ifconfig</a> <a href="#ping">ping</a> <a href="#rz">rz</a> <a href="#sz">sz</a>

系统启动,关机

<a href="#exit">exit</a> <a href="#logout">logout</a> <a href="#init">init</a> <a href="#quit">quit</a> <a href="#reboot">reboot</a> <a href="#shutdown">shutdown</a> <a href="#runlevel">runlevel</a> <a href="#halt">halt</a> <a href="#poweroff">poweroff</a>

进程

<a href="#free">free</a> <a href="#lsblk">lsblk</a> <a href="# lscpu"> lscpu</a>

程序包管理

<a href="#rpm">rpm</a> <a href="#.">.</a> <a href="#source">source</a>


<a id="clear">clear</a>

clear the terminal screen 清空屏幕内容

<a id="hostname">hostname</a>

Display the hostname 显示主机名

  • hostname [-s|--short]
    -s | --short Display the shourt host name, This is the hostname cut at the firs dot. 显示短主机名,取第一个点号之前的字符串
[centos6@root ~]# hostname
centos6.zhubiao.science
# -s 取第一个点号之前的字符串作为短主机名
[centos6@root ~]# hostname -s 
centos6

<a id="tty">tty</a>

print the file name of the terminal connected to standard input 显示当前工作终端下的终端的名字

  • 终端分为:
    • 物理终端
      物理控制台:表示为 /dev/console, Linux单用户模式下显示为物理终端
    • 虚拟终端
      系统自带6个虚拟终端,表示为/dev/tty#, #为1-6
    • 图形终端
    • 伪终端
      图形界面打开的命令行接口,通过ssh或telnet协议打开的命令行界面均为伪终端,表现为/dev/pts/#
# 将运行级别切换为单用户模式后,使用的将是物理终端,如下所示,当前运行级别为3的多用户模式,将其切换为1后查看终端。
[root@zb01 ~]# tty
/dev/tty1
[root@zb01 ~]# runlevel #查看运行级别
S 3
[root@zb01 ~]# init 1
...
[root@zb01 /]# runlevel
1 S
[root@zb01 /]# tty
/dev/console

<a id="who">who</a>

Who is logged on 显示当前登录的用户

  • who [options]
    -b #系统登录的时间
    -r #当前运行级别
[centos7@root dir]# who
root     tty1         2017-07-15 16:20
root     pts/0        2017-07-15 07:24 (gateway)
[centos7@root dir]# who -b
         system boot  2017-07-14 20:54
[centos7@root dir]# who -r
         run-level 3  2017-07-14 20:54                   last=5

<a id="whoami">whoami</a>

显示当前登录的用户名

  • who
[centos7@root dir]# whoami
root

<a id="w">w</a>

Show who is logged on and what they are doing. 显示当前登录的用户和正在执行的命令

  • w
[centos7@root dir]# w 
 17:08:16 up 12:09,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      16:20   47:20   0.04s  0.04s -bash
root     pts/0    gateway          07:24    0.00s  3.05s  0.01s w

<a id="whatis">whatis</a>

查找外部命令的man手册帮助文档所在的章节

  • whatis command #等同于man -f
[centos7@root ~]# whatis ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[centos7@root ~]# whatis cp
cp (1)               - copy files and directories
cp (1p)              - copy files

[centos7@root ~]# whatis ifconfig
ifconfig (8)         - configure a network interface

[centos7@root ~]# man -f ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[centos7@root ~]# man -f cp
cp (1)               - copy files and directories
cp (1p)              - copy files

<a id="which">which</a>

按照PATH路径查找命令后显示其完整路径

  • which [options][command]
    -a 显示命令所有PATH路径
    --skip-alias 不显示别名
[centos6@root test]# which --skip-alias ls
/bin/ls
[centos6@root test]# which -a ls
alias ls='ls --color=auto'
    /bin/ls

<a id="whereis">whereis</a>

查找命令所在路径,源码路径,帮助手册路径
whereis [options][command]

[centos6@root test]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

[centos6@root test]# whereis -b mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql

[centos6@root test]# whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz

<a id="cd">cd</a>

Change the shell working dirctory 切换工作目录

  • cd [-P] DIR
    -P 若DIR为符号链接目录,cd DIR切换到实际的工作目录,而不是符号链接目录
    cd - #切换到上一个工作目录
    cd ~ #切换到当前用户的家目录
    cd ~USER #切换到USER用户的家目录,只有root有权限切换到任意用户的家目录
    cd .. #切换到上一级目录

    PWD #显示当前工作目录

    OLDPWD #显示上一次工作目录,echo #OLDPWD可查看其值,若修改了其值里的路径,则使用cd -将跳转到所修改路径下

[centos6@root test]# pwd
/root/test
[centos6@root test]# cd ~  
[centos6@root ~]# pwd
/root
[centos6@root ~]# cd -
/root/test
[centos6@root test]# pwd
/root/test
[centos6@root test]# cd ~zb10
[centos6@root zb10]# pwd
/home/zb10
[centos6@root zb10]# cd ..
[centos6@root home]# pwd
/home
[centos6@root home]# 
# -P 切换到实际路径的工作目录下
[centos6@root test]# ll dir
lrwxrwxrwx. 1 root root 6 Jul 15 17:16 dir -> a/b/c/
[centos6@root test]# cd dir
[centos6@root dir]# pwd
/root/test/dir
[centos6@root dir]# cd -
/root/test
[centos6@root test]# cd -P dir
[centos6@root c]# pwd
/root/test/a/b/c

<a id="pwd">pwd</a>

print the name of the current working dirctory 显示当前工作目录

  • pwd [-P]
    -P - 若当前工作目录为符号链接路径,加-P选项显示实际路径
[centos7@root test]# pwd
/root/test  #显示当前工作目录
# -P 显示出实际路径而不是符号链接的路径
[centos7@root test]# mkdir -p a/b/c 
[centos7@root test]# ln -s a/b/c dir  
[centos7@root test]# ll dir
lrwxrwxrwx. 1 root root 5 Jul 15 15:33 dir -> a/b/c
[centos7@root test]# cd dir 
[centos7@root dir]# pwd  
/root/test/dir
[centos7@root dir]# pwd -P  #加了-P选项,显示实际路径
/root/test/a/b/c

<a id="ls">ls</a>

List dirctory contents 列出当前或指定目录(或所有子目录)下的内容

  • ls [options][dirs]
    - l 列出详细内容
    -a | --all 列出所有内容包括. ..
    -A 列出所有内容,不包括. ..
    -d 仅列出指定的目录
    --full-time 列出完整的修改时间格式

    -S 按文件大小进行排序

    -t 按修改时间大小(mtime)进行排序

    -u 显示访问时间(atime),默认显示的是mtime,

    -t -u 显示访问时间(atime),并按atime进行排序

    -r | --reverse 反向排序

    -R | --recursive 递归显示所有路径

    --time = atime, ctime 显示访问时间或修改属性时间(大小,权限。。。)

    [centos6@root test]# ls
    a  dir
    [centos6@root test]# ls -l
    total 4
    drwxr-xr-x. 3 root root 4096 Jul 15 17:15 a
    lrwxrwxrwx. 1 root root    6 Jul 15 17:16 dir -> a/b/c/
    [centos6@root test]# ll
    total 4
    drwxr-xr-x. 3 root root 4096 Jul 15 17:15 a
    lrwxrwxrwx. 1 root root    6 Jul 15 17:16 dir -> a/b/c/
    [centos6@root test]# ls -a
    .  ..  a  dir
    [centos6@root test]# ls -A
    a  dir
    [centos6@root test]# ls /root/test/
    a  dir
    [centos6@root test]# ls --full-time /root/test/
    total 4
    drwxr-xr-x. 3 root root 4096 2017-07-15 17:15:41.550009093 +0800 a
    lrwxrwxrwx. 1 root root    6 2017-07-15 17:16:44.490009154 +0800 dir -> a/b/c/
    

<a id="cat">cat</a>

Concatenate files and print on the standard output # 读取一个或多个文件(也可以从键盘输入) 并输出

  • cat [options][files]
    -n # 在每行(包括空行)前面加上行号
    -b # 在非空行前面加上行号
[centos7@root dir]# cat /etc/resolv.conf 
# Generated by NetworkManager
search magedu.com zhubiao.science
  
nameserver 192.168.17.1
  
nameserver 172.18.0.1
# -n 加入行号,包括空行
[centos7@root dir]# cat -n /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
     3  
     4  nameserver 192.168.17.1
     5  
     6  nameserver 172.18.0.1
# -b非空行前加入行号
[centos7@root dir]# cat -b /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
      
     3  nameserver 192.168.17.1
      
     4  nameserver 172.18.0.1

<a id="tac">tac</a>

Concatenate and print files #倒叙显示文件内容,即从文件尾页到首页的顺序显示内容

  • tac
# 如正常顺序显示/etc/resolv.conf 如下:
[centos7@root dir]# cat /etc/resolv.conf 
# Generated by NetworkManager
search magedu.com zhubiao.science

nameserver 192.168.17.1

nameserver 172.18.0.1
#使用tac 将倒序输出文件内容,如下:
[centos7@root dir]# tac /etc/resolv.conf 
nameserver 172.18.0.1

nameserver 192.168.17.1

search magedu.com zhubiao.science
# Generated by NetworkManager

<a id="nl">nl</a>

读取文件内容后输出左侧加入行号的内容

  • nl [options][files]
    • -b #与选项a, t一起使用,控制行号的输出
      a #-b a 所有行都有行号
      t #-b t 空行不输出行号
      n #-b n 所有行均不输出行号
    • -n #与ln, rn, rz一起使用控制行号位置及补0的输出
      ln #-n ln 行号在所在字段的左侧
      rn #-n rn 行号在所在字段的右侧
      rz #-n rz 行号在所在字段的右侧,左侧补0
[centos7@root dir]# nl /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
       
     3  nameserver 192.168.17.1
       
     4  nameserver 172.18.0.1
[centos7@root dir]# nl -b a /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
     3  
     4  nameserver 192.168.17.1
     5  
     6  nameserver 172.18.0.1
[centos7@root dir]# nl -b t /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
       
     3  nameserver 192.168.17.1
       
     4  nameserver 172.18.0.1
[centos7@root dir]# nl -b n /etc/resolv.conf 
       # Generated by NetworkManager
       search magedu.com zhubiao.science
       
       nameserver 192.168.17.1
       
       nameserver 172.18.0.1
[centos7@root dir]# nl -n ln /etc/resolv.conf 
1       # Generated by NetworkManager
2       search magedu.com zhubiao.science
       
3       nameserver 192.168.17.1
       
4       nameserver 172.18.0.1
[centos7@root dir]# nl -n rn /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
       
     3  nameserver 192.168.17.1
       
     4  nameserver 172.18.0.1
[centos7@root dir]# nl -n rz /etc/resolv.conf 
000001  # Generated by NetworkManager
000002  search magedu.com zhubiao.science
       
000003  nameserver 192.168.17.1
       
000004  nameserver 172.18.0.1
[centos7@root dir]# nl -n rz -w2 /etc/resolv.conf 
01  # Generated by NetworkManager
02  search magedu.com zhubiao.science
   
03  nameserver 192.168.17.1
   
04  nameserver 172.18.0.1

<a id="head">head</a>

Out put the first part of files 读取文件的前几行(默认为10行,也可指定)并输出

  • head [options][files]
    -n NUM #输出前NUM行
    -v 行首打印所读取文件内容的完整路径名
    -c NUM #输出前NUM个字符
[centos7@root dir]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 行首输出文件路径名
[centos7@root dir]# head -v /etc/passwd
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 指定输出前3行
[centos7@root dir]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
# 输出前4个字符
[centos7@root dir]# head -c 4 /etc/passwd
root[centos7@root dir]#

<a id="tail">tail</a>

Out put the last part of files

  • tail [options][fies]
    -n NUM #输出文件后NUM行
    -v #输出首行显示文件完整路径内容
    -c NUM #输出文件最后NUM个字符
[centos7@root dir]# tail /etc/passwd
setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:990:985::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
[centos7@root dir]# tail -n 3 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
[centos7@root dir]# tail -v -n 3 /etc/passwd
==> /etc/passwd <==
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
# 输出文件末尾9个字符
[centos7@root dir]# tail -c 9 /etc/passwd
bin/bash

<a id="less">less<a>

非常好用的一个浏览文件内容的工具,可以通过快捷键向前/后浏览文件内容,可以通过关键字搜索文件中的字符串。对文件的前后翻屏功能类似VIM, MAN手册的浏览就是通过less命令实现的。

  • less FILES
    • 翻屏快捷键
      Ctrl + F #向文件首部翻一屏
      Ctrl + B #向文件尾部翻一屏
      Ctrl + D #向文件首部翻半屏
      Ctrl + U #向文件尾部翻半屏
      j #向文件尾部滚动一行
      k #向文件首部滚动一行
  • 搜索
    • / [patern]
      n #向前搜索下一个匹配的模式或字符串
      N #向后搜索上一个匹配的模式或字符串
    • ?[patern]
      n #向后搜索上一个匹配的模式或字符串
      N #向前搜索下一个匹配的模式或字符串

<a id="more">more</a>

more功能和less类似,但没有less功能多,比如不可以向文件首部翻屏,只能向尾部翻屏,所以建议使用less

<a id="hexdump">hexdump</a>

通常用来查看二进制文件,但不限于只能看二进制文件。

  • hexdump [option][files]
    -o #八进制显示
    -d #十进制显示
    -x #十六进制显示
    -C #十六进制和ASCII字符显示

以下实例通过hexdump读取cat命令的二进制文件,因内容过多,此处使用<a href="#head">head</a> -n 3 来显示前三行内容

[centos7@root dir]# hexdump /bin/cat | head -n 3
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0002 003e 0001 0000 2644 0040 0000 0000
0000020 0040 0000 0000 0000 cbc0 0000 0000 0000
[centos7@root dir]# hexdump -o /bin/cat | head -n 3
0000000  042577  043114  000402  000001  000000  000000  000000  000000
0000010  000002  000076  000001  000000  023104  000100  000000  000000
0000020  000100  000000  000000  000000  145700  000000  000000  000000
[centos7@root dir]# hexdump -d /bin/cat | head -n 3
0000000   17791   17996   00258   00001   00000   00000   00000   00000
0000010   00002   00062   00001   00000   09796   00064   00000   00000
0000020   00064   00000   00000   00000   52160   00000   00000   00000
[centos7@root dir]# hexdump -x /bin/cat | head -n 3
0000000    457f    464c    0102    0001    0000    0000    0000    0000
0000010    0002    003e    0001    0000    2644    0040    0000    0000
0000020    0040    0000    0000    0000    cbc0    0000    0000    0000
[centos7@root dir]# hexdump -C /bin/cat | head -n 3
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 3e 00 01 00 00 00  44 26 40 00 00 00 00 00  |..>.....D&@.....|
00000020  40 00 00 00 00 00 00 00  c0 cb 00 00 00 00 00 00  |@...............|

<a id="od">od</a>

读取二进制文件

  • od [option][files]
    -o #八进制显示
    -d #十进制显示
    -x #十六进制显示
    以下实例通过od读取cat命令的二进制文件,因内容过多,此处使用<a href="#head">head</a> -n 2 来显示前三行内容
[centos6@root test]# od -o /bin/cat | head -n 2
0000000 042577 043114 000402 000001 000000 000000 000000 000000
0000020 000002 000076 000001 000000 014120 000100 000000 000000
[centos6@root test]# od -d /bin/cat | head -n 2
0000000 17791 17996   258     1     0     0     0     0
0000020     2    62     1     0  6224    64     0     0
[centos6@root test]# od -x /bin/cat | head -n 2
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000020 0002 003e 0001 0000 1850 0040 0000 0000

<a id="cp">cp</a>

  • cp [options][Source_File] [Destination_File]
    -p #same as --preserve=mode,ownership,timestamps
    -u #当原文件更新时才覆盖目标文件
    -R | r #若原文件有子目录时,连同目录一起复制
    -d #当原文件内容包含符号链接时,选项复制符号链接,不加-d选项是复制符号链接指向的原文件内容
    -a same as -dr
    -i | --interactive #prompt before overwrite 若目标文件存在普通用户复制时,提示是否覆盖,建议使用cp时加-i选项
    -f | --force
-p 复制时目标文件保留原文件的属性(时间戳,属主属组,文件权限)
[centos6@zb10 ~]$ ll 1.txt
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 1.txt
[centos6@root zb10]# cp 1.txt 2.txt
[centos6@root zb10]# cp -p 1.txt 3.txt
[centos6@root zb10]# ll [[:digit:]].txt
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 1.txt #原文件
-rw-r--r--. 1 root root 0 Jul 15 22:25 2.txt  #未加-p选项属性将变化
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 3.txt  #加-p选项的目标文件属性不变
# -u 只有当原文件比目标文件修改时间更新才覆盖目标文件
# 如下file2 比file1更新,-u 选项时将file1复制到file2时不会覆盖file2, 而反之则覆盖file1的内容
[centos6@zb10 ~]$ ll file*
-rw-rw-r--. 1 zb10 zb10 15 Jul 15 22:06 file1
-rw-rw-r--. 1 zb10 zb10 15 Jul 15 22:07 file2 
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file1 Contents

==> file2 <==
file2 Contents
[centos6@zb10 ~]$ cp -u file1 file2
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file1 Contents

==> file2 <==
file2 Contents
[centos6@zb10 ~]$ cp -u file2 file1
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file2 Contents

==> file2 <==
file2 Contents
# -d 当原文件内容包含符号链接时,选项复制符号链接,不加-d选项是复制符号链接指向的原文件内容
[centos6@root test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2

3 directories, 2 files
[centos6@root test]# cp dir1/file.ln dir2/
[centos6@root test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2
    └── file.ln

3 directories, 3 files
[centos6@root test]# cp -d dir1/file.ln dir2/
cp: overwrite `dir2/file.ln'? y
[centos6@root test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2
    └── file.ln -> a/file

3 directories, 3 files

<a id="mv">mv</a>

Move or rename files 移动或重命名文件

  • mv [options][source] [destination]
    -f | --force 目标文件存在时,直接覆盖
    -i | --interactive 提示用户是否覆盖目标文件
    -u | --update 只有当原文件更新时,才执行移动
# 如下所示当前目录下有两个目录,dir1,dir2,将dir2 移动到dir1下
[centos6@root test]# tree
.
├── dir1
└── dir2
    └── a
        └── file1

3 directories, 1 file
[centos6@root test]# mv dir2 dir1
[centos6@root test]# tree
.
└── dir1
    └── dir2
        └── a
            └── file1

3 directories, 1 file
[centos6@root test]# 
# 修改dir1目录名为dir3
[centos6@root test]# mv dir1 ./dir3
[centos6@root test]# ll
total 4
drwxr-xr-x. 3 root root 4096 Jul 16 09:41 dir3

<a id="rm">rm</a>

Remove files or directories 删除文件或目录(子目录)

  • rm [options][files]
    -f | --force #删除时,文件或目录不存在时不提示
    -r | -R | --recursive #删除目录及其下的子目录或文件
    -i #删除每一个文件、目录时均提示是否删除
[centos6@root test]# rm dir3/dir2/a/file1 
rm: remove regular empty file `dir3/dir2/a/file1'? y
[centos6@root test]# tree
.
└── dir3
    └── dir2
        └── a

3 directories, 0 files
[centos6@root test]# rm -rf dir3 #彻底删除dir3及其目录下的所有子目录、文件

<a id="mkdir">mkdir</a>

Make Directories 创建目录

  • mkdir [options][directories]
    -p | --parents 创建多级目录时,其父目录不存在则先创建其父目录
    -m | --mode=MODE 创建目录时指定目录的权限
    -v 显示创建目录的过程
#创建目录dir1
[centos6@root test]# mkdir -v dir1
mkdir: created directory `dir1'
[centos6@root test]# ll -d dir1
drwxr-xr-x. 2 root root 4096 Jul 16 10:06 dir1

#-m 创建目录dir2,并指定目录权限为rw-rw-rw-(666)
[centos6@root test]# mkdir -m 666 dir2
[centos6@root test]# ll -d dir1 dir2
drwxr-xr-x. 2 root root 4096 Jul 16 10:06 dir1
drw-rw-rw-. 2 root root 4096 Jul 16 10:07 dir2

#-p 创建多级目录
[centos6@root test]# mkdir -pv dir3/a/b/c
mkdir: created directory `dir3'
mkdir: created directory `dir3/a'
mkdir: created directory `dir3/a/b'
mkdir: created directory `dir3/a/b/c'
[centos6@root test]# ll -d dir3/a/b/c
drwxr-xr-x. 2 root root 4096 Jul 16 10:08 dir3/a/b/c

<a id="rmdir">rmdir</a>

remove empty directories 删除空目录

  • rmdir
    -p | --parents #Remove empty directories and its ancestors 删除其空目录及其父辈空目录
#如下所示,除了file是文件,其它均为空文件夹,下面练习是使用rmdir
[centos6@root test]# tree 
.
├── dir1
├── dir2
│   └── file
└── dir3
    └── a
        └── b

5 directories, 1 file

#删除空目录dir1
[centos6@root test]# rmdir dir1
[centos6@root test]# tree
.
├── dir2
│   └── file
└── dir3
    └── a
        └── b
4 directories, 1 file

#若不是空目录,比如文件file无法使用rmdir删除
[centos6@root test]# rmdir dir2/file 
rmdir: failed to remove `dir2/file': Not a directory

# -p 连同其父空目录一起删除
[centos6@root test]# rmdir dir3/a/b/ #不适用-p仅删除其自身空目录
[centos6@root test]# tree
.
├── dir2
│   └── file
└── dir3
    └── a

3 directories, 1 file
[centos6@root test]# rmdir -p dir3/a/  #使用-p连同其父空目录一起删除
[centos6@root test]# tree
.
└── dir2
    └── file

1 directory, 1 file

<a id="dd">id</a>

Copy and Convert files 复制或转换文件格式
dd 功能很强大,可以直接读取磁盘扇区的内容,在此列举一个dd的复制工具

  • dd if=input_file of=output_file bs=block_size count=number
    if #Read from file instead of stdin 从文件读入(包括装置,Linux中一切皆文件喔),若无if将从标准输入(从键盘输入)
    of #Write to file instead of stdout 写入到文件,无此项将输出到屏幕
    bs #读取和写入的block大小,默认为512bytes

    • ibs #读取的block大小
    • obs #写入的block大小

    count #bs的数量

#将/bin/cat文件备份到当前目录下
  [centos7@root ~]# dd if=/bin/cat of=./cat.bk bs=512
105+1 records in
105+1 records out
54080 bytes (54 kB) copied, 0.000900543 s, 60.1 MB/s
[centos7@root ~]# ll -h cat.bk 
-rw-r--r--. 1 root root 53K Jul 16 11:06 cat.bk

<a id="gedit">gedit</a>

gdit是图形终端的一个文本编辑工具,通过下图方式打开

image.png

gedit也可以通过图形终端上的命令行接口打开[centos6@root Desktop]# gedit

<a id="nano">nano</a>

nano #命令行下的文本编辑工具,命令行下面有更强大的文本编辑工具vim,建议使用vim

<a id="file">file</a>

file Determine file types |判断文件类型

#查看cat命令文件类型
[centos7@root ~]# file /bin/cat
/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=fac04659ab9a437b5384c09f4731023373821a39, stripped

#查看/etc/passwd文件类型
[centos7@root ~]# file /etc/passwd
/etc/passwd: ASCII text

#查看/etc/类型
[centos7@root ~]# file /etc
/etc: directory

<a id="type">type</a>

type 判断命令类型

  • type [options] command
    -a 显示所有包含该命令的位置
  • 命令类型
    • builtin Shell内建命令
    • 外部命令,不包含在shell内建命令里的命令
    • alias | 别名
    • keword | 关键字
    • function | 函数
#echo即有内建命令,也有外部命令,为了演示type,我们再给echo定义个别名
[centos6@root test]# alias echo
alias echo='echo -e'
#不加任何选项,使用type查询,若有别名,只能查到别名
[centos6@root test]# type echo
echo is aliased to `echo -e'
# -a 选项
[centos6@root test]# type -a echo
echo is aliased to `echo -e'
echo is a shell builtin
echo is /bin/echo

<a id="id">id</a>

Print user and group information for specified the USERNAME or the currentuser

  • id [options][users]
    -g 用户所在有效组
    -G 用户所在的所有组
# 不指定user将显示当前用户的信息
[centos7@root ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

# 指定用户
[centos7@root ~]# id user1
uid=1001(user1) gid=1003(user1) groups=1003(user1),1001(group1),1002(group2)

[centos7@root ~]# id -g user1
1003

[centos7@root ~]# id -G user1
1003 1001 1002

<a id="getent">getent</a>

Get entries from name server switch 从定义的数据库里获得记录
数据库定义在/etc/nsswitch.conf 文件中

  • getent database [entries]
# 从/etc/passwd数据库内获取包含root用户的条目
[centos7@root ~]# getent passwd root
root:x:0:0:root:/root:/bin/bash
# 从/etc/group 数据库内获取包含zb20的条目
[centos7@root ~]# getent group zb20
zb20:x:1000:zb20

<a id="iconv">iconv</a>

Convert text from one character encoding to another | 转换字符编码

  • iconv -f from_encoding -t to_encoding input_file -o output_file
    -f 原文件编码
    -t 目标文件编码
    -o 输出文件
    -l 列出iconv能转换的编码
# windows系统上保存默认文本编码格式,在linux系统中无法打开,就可以通过iconv转换成linux系统识别的编码
[centos7@root ~]# cat source.txt 
βo³¤³ȍ󁯿գ¬°ڊS¢ћ°ڊC
[centos7@root ~]# iconv -f gb2312 -t UTF-8 source.txt -o destination.txt      
[centos7@root ~]# cat destination.txt 
万里长城万里空,百世英雄百世梦

<a id="echo">echo</a>

打印shell变量,指定的字符串

  • echo [options][string]
    -n #输出字符后末尾不换行,默认换行
    -e #输出字符中的转义字符生效
  • \t #TAB
  • \n #newline
#显示shell变量的值
[centos6@root test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[centos6@root test]# echo $PS1
\[\][\h@\u \W]\$ \[\]
[centos6@root test]# a=50
[centos6@root test]# echo $a
50

# -n 末尾不换行
[centos6@root test]# echo "hello everyone"
hello everyone
[centos6@root test]# echo -n "hello everyone"
hello everyone[centos6@root test]# 

# -e 转义字符生效
[centos6@root test]# echo "hello\t welcome to QuJing\nI believe..."
hello\t welcome to QuJing\nI believe...
[centos6@root test]# echo -e "hello\t welcome to QuJing\nI believe..."
hello    welcome to QuJing
I believe...

#输出连续或不连续字符
[centos7@root ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[centos7@root ~]# echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
[centos7@root ~]# echo {1..9}
1 2 3 4 5 6 7 8 9
[centos7@root ~]# echo {9..1}
9 8 7 6 5 4 3 2 1
[centos7@root ~]# echo {1..10..2}
1 3 5 7 9
[centos7@root ~]# echo {001..10..2}
001 003 005 007 009
[centos7@root ~]# echo {1,7,q,b}
1 7 q b

<a id="history">history</a>

读取历史命令记录
。。。更多功能待续

[centos7@root ~]# history | head -n 5
    1  clear
    2  cat /etc/gdm/custom.conf 
    3  vim /etc/gdm/custom.conf
    4  reboot
    5  shutdown -h now

<a id="ifconfig">ifconfig</a>

配置或显示网卡参数

#显示eth1网卡参数
[centos6@root test]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:E3:FB:0C  
          inet addr:172.18.253.214  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fee3:fb0c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:440093 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3225 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:43780248 (41.7 MiB)  TX bytes:241256 (235.6 KiB)

# 关闭eth1网卡
[centos6@root test]# ifconfig eth1 down
[centos6@root test]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:E3:FB:0C  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:440107 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3227 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:43781936 (41.7 MiB)  TX bytes:241410 (235.7 KiB)

# 启动eth1网卡
[centos6@root test]# ifconfig eth1 up

<a id="ping">ping</a>

测试网络是否连通

  • ping [options][ip/name]
    -c COUNT 发送ECHO_REQUEST 包的数量
[centos6@root test]# ping -c 3 192.168.17.20
PING 192.168.17.20 (192.168.17.20) 56(84) bytes of data.
64 bytes from 192.168.17.20: icmp_seq=1 ttl=64 time=3.66 ms
64 bytes from 192.168.17.20: icmp_seq=2 ttl=64 time=0.508 ms
64 bytes from 192.168.17.20: icmp_seq=3 ttl=64 time=0.848 ms

--- 192.168.17.20 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.508/1.673/3.664/1.414 ms

<a id="exit">exit</a> <a id="quit">quit</a> <a id="logout">logout</a>

quit, exit, logout 退出登录

<a id="init">init</a>

切换运行级别

  • init NUM #NUM为:1,2,3,4,5,6
    CentOS6 上在/etc/inittabe中定义了各运行级别
    # Default runlevel. The runlevels used are:
    # 0 - halt (Do NOT set initdefault to this) 关机
    # 1 - Single user mode 单用户模式
    # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 无网络多用户模式(命令行界面)
    # 3 - Full multiuser mode 多用户模式(命令行界面)
    # 4 - unused 未使用,保留
    # 5 - X11 图形界面
    # 6 - reboot (Do NOT set initdefault to this) 重启

<a id="runlevel">runlevel</a>

查看当前系统运行级别

# 可以通过runlevel 或 who -r 查看当前系统运行级别
[centos6@root test]# runlevel
[centos6@root test]# who -r
         run-level 5  2017-07-15 06:26
N 5

<a id="shutdown">shutdown</a>

最常用的关机或重启命令

  • shutdown [options][time] [string]
    -r reboot after shutdown 重启
    -h 关机
    -c 取消关机或重启
 # 立即关机
shutdown -h now

# 过5分钟后关机
[centos6@root test]# shutdown -h 5 &
[1] 10237
[centos6@root test]# 
Broadcast message from root@centos6.zhubiao.science
    (/dev/pts/0) at 15:51 ...

The system is going down for halt in 5 minutes!

#取消关机
[centos6@root test]# shutdown -c
shutdown: Shutdown cancelled
[1]+  Done                    shutdown -h 5

# 5分钟后重启
[centos6@root test]# shutdown -r 5

Broadcast message from root@centos6.zhubiao.science
    (/dev/pts/0) at 15:53 ...

The system is going down for reboot in 5 minutes!

<a id="halt">halt</a> <a id="reboot">reboot</a> <a id="poweroff">pwoeroff</a>

  • reboot 重启
  • halt, poweroff 关机

<a id="free">free</a>

显示当前内存使用情况

  • free [options]
    -h #human readable output 自动确定最佳单位显示内存使用量
    -b, -k, -m, -g 以bytes, KB, MB, GB为单位显示内存使用量
    -t 最后一行汇总RAM+SWAP内存的使用总量
    -l
[centos7@root ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           976M        202M        252M         13M        522M        539M
Swap:          2.0G          0B        2.0G

[centos7@root ~]# free -th
              total        used        free      shared  buff/cache   available
Mem:           976M        202M        251M         13M        522M        539M
Swap:          2.0G          0B        2.0G
Total:         3.0G        202M        2.2G

[centos7@root ~]# free -lh
              total        used        free      shared  buff/cache   available
Mem:           976M        202M        251M         13M        522M        539M
Low:           976M        724M        251M
High:            0B          0B          0B
Swap:          2.0G          0B        2.0G

# 也可通过查看内核进程数据来查看内存使用情况
[centos7@root ~]# cat /proc/meminfo 
MemTotal:         999964 kB
MemFree:          257648 kB
MemAvailable:     551900 kB
Buffers:            1804 kB
Cached:           390520 kB
SwapCached:            0 kB
Active:           266780 kB
Inactive:         224516 kB
Active(anon):      94740 kB
Inactive(anon):    17896 kB
Active(file):     172040 kB
Inactive(file):   206620 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:         99036 kB
Mapped:            35392 kB
Shmem:             13664 kB
Slab:             142844 kB
SReclaimable:      86632 kB
SUnreclaim:        56212 kB
KernelStack:        4832 kB
PageTables:         7260 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2597128 kB
Committed_AS:     634440 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      181824 kB
VmallocChunk:   34359310332 kB
HardwareCorrupted:     0 kB
AnonHugePages:      8192 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       98176 kB
DirectMap2M:      950272 kB

<a id="lsblk">lsblk</a>

列出块状设备大小及挂载点

[centos7@root ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0   50G  0 part /app
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  7.7G  0 rom  /run/media/root/CentOS 7 x86_64
[centos7@root ~]# lsblk -d
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda    8:0    0  200G  0 disk 
sr0   11:0    1  7.7G  0 rom  /run/media/root/CentOS 7 x86_64

<a id="lscpu">lscpu</a>

显示CPU信息

[centos7@root ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Model name:            Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Stepping:              9
CPU MHz:               2493.686
BogoMIPS:              4988.79
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0,1

#通过内核进程文件/proc/cpuinfo查看cpu信息
[centos7@root ~]# cat /proc/cpuinfo 
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 58
model name  : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping    : 9
microcode   : 0x12
cpu MHz     : 2493.686
cache size  : 3072 KB
physical id : 0
siblings    : 1
core id     : 0
cpu cores   : 1
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase tsc_adjust smep
bogomips    : 4988.79
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 58
model name  : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping    : 9
microcode   : 0x12
cpu MHz     : 2493.686
cache size  : 3072 KB
physical id : 2
siblings    : 1
core id     : 0
cpu cores   : 1
apicid      : 2
initial apicid  : 2
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase tsc_adjust smep
bogomips    : 4988.79
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:

<a id=".">.</a> <a id="source">source</a>

执行shell脚本

#在 /etc/profile.d/env.sh中设置命令提示符,然后使用. DIR或soruce DIR执行脚本,后命令提示符将生效
[~] cat /etc/profile.d/env.sh
export PS1="[\h@\u \W]\\$ "

[~] . /etc/profile.d/env.sh
[centos6@root ~]# 

<a id="enable">enable</a>

Enable and disable shell builtins | 启用或停用SHELL内建命令

  • enable [options][command]
    -a 列出所有内建命令,不管是否被禁用
    -n command 停用命令command
#停用cd命令
[centos6@root ~]# enable -n cd 
[centos6@root ~]# cd
-bash: cd: command not found

[centos6@root ~]# enable -a | grep cd
enable -n cd

#启用cd命令
[centos6@root ~]# enable cd
[centos6@root ~]# cd ..
[centos6@root /]# cd -
/root

<a id="hash">hash</a>

缓存已执行的外部命令的路径,以便下次使用节省搜索命令的时间

  • hash [options][command]
    -d command 从hash缓存中删除command命令记录
    -r 清空hash 缓存
    -l 列出路径及命令名
    -p name #在hash缓存中给路径起别名
# 查看已缓存的外部命令
[centos6@root ~]# hash 
hits    command
   1    /sbin/ifconfig
   1    /usr/bin/man
   1    /bin/ls
   8    /usr/bin/clear

# 列出缓存的路径及其命令名
[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /usr/bin/man man
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear

#从hash缓存中删除命令man
[centos6@root ~]# hash -d man
[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear

#起别名
[centos6@root ~]# hash -p /sbin/ifconfig ifcon
[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /sbin/ifconfig ifcon
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear
[centos6@root ~]# ifcon eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:FB:02  
          inet addr:192.168.17.10  Bcast:192.168.17.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fee3:fb02/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21356 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18035 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1995596 (1.9 MiB)  TX bytes:3239265 (3.0 MiB)

#清空hash缓存
[centos6@root ~]# hash -r
[centos6@root ~]# hash
hash: hash table empty

<a id="alias">alias</a> <a id="unalis">unalis</a>

  • alias #Define or display aliases 定义别名
    alias name="value"
  • unalias 取消别名
    unalias name
#查看以定义的别名
[centos6@root ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

#定义别名
[centos6@root ~]# alias grep="grep --color"
[centos6@root ~]# alias
alias cp='cp -i'
alias grep='grep --color'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

#在配置文件/etc/bashrc中定义别名,所有用户均生效
[centos6@root ~]# vim /etc/bashrc 
[centos6@root ~]# tail -n 3 /etc/bashrc
#Define alias
alias grep="grep --color"
[centos6@root ~]# . /etc/bashrc 

#在自己家目录配置文件~/.basrc中定义,只有当前用户生效
[centos6@root ~]# vim ~/.bashrc 
[centos6@root ~]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

<a id="date">date</a>

Print or set system date or time 显示或设置系统时间

  • date [option][+format]
    设定时间的两种方法

  • date [MMDDhhmm[[CC]YY][.ss]]

  • date -s "string"

    -d "string"

* string: 
  "1 year"
  "1 year ago"
  "3 years"
  "3 years ago"
  "1 day"
  "1 day ago"
  "3 days"
  "3 days ago"
  • format
    %Y year
    %m month
    %d day
    %H hour
    %M minute
    %S second
    %F fulldate same as %Y-%m-%d
    %T time,same as %H:%M:%S
#以不同的格式显示时间
[centos6@root ~]# date +%F
2017-07-17
[centos6@root ~]# date +%T
22:57:11
[centos6@root ~]# date "+%F %T"
2017-07-17 22:57:19
[centos6@root ~]# date +"%F %T"
2017-07-17 22:57:27
[centos6@root ~]# date "+%Y-%m-%d %H:%M:%S"
2017-07-17 22:57:55

#设定时间  [MMDDhhmm[[CC]YY][.ss]]
[centos6@root ~]# date 062012121990.33
Wed Jun 20 12:12:33 CDT 1990
[centos6@root ~]# date +"%F %T"
1990-06-20 12:12:48

#设定时间 date -s STRING
[centos6@root ~]# date -s "20170717 23:03:10"
Mon Jul 17 23:03:10 CST 2017
[centos6@root ~]# date +"%F %T"
2017-07-17 23:03:22

# 当前时间
[centos6@root ~]# date +"%F %T"
2017-07-17 23:09:11
#显示4年前的日期
[centos6@root ~]# date  -d "4 years ago" +"%F %T"
2013-07-17 23:09:32
#显示10天前的日期
[centos6@root ~]# date  -d "10 days ago" +"%F %T"
2017-07-07 23:09:52
#显示10天后的日期
[centos6@root ~]# date  -d "10 days" +"%F %T"
2017-07-27 23:10:01

<a id=clock>clock</a> <a id="hwclock">hwclock</a>

hwclock,clock功能相同,都是设定硬件时间

  • hwclock [options]
    • hwclock --set --date="string" #按字符串描述的时间设定硬件时间
    • hwclock --systohc #将系统时间更新到硬件时间
    • hwclock --hctosys #将硬件时间更新到系统时间
# clock其实是hwclock的符号链接
[centos6@root ~]# ll /sbin/clock 
lrwxrwxrwx. 1 root root 7 Jul 15 01:17 /sbin/clock -> hwclock

[centos7@root ~]# hwclock
Mon 17 Jul 2017 11:20:50 PM CST  -0.663348 seconds

#设定硬件时间
[centos7@root ~]# hwclock --set --date="1990-06-20 10:10:10"
[centos7@root ~]# hwclock ; date
Wed 20 Jun 1990 10:10:24 AM CDT  -0.100626 seconds
Mon Jul 17 23:21:39 CST 2017

[centos7@root ~]# hwclock --systohc
[centos7@root ~]# hwclock ; date
Mon 17 Jul 2017 11:22:01 PM CST  -0.944138 seconds
Mon Jul 17 23:22:00 CST 2017
[centos7@root ~]# date -s "19900620" +%F
1990-06-20

[centos7@root ~]# hwclock --hctosys
[centos7@root ~]# hwclock ; date
Mon 17 Jul 2017 11:22:58 PM CST  -0.411873 seconds
Mon Jul 17 23:22:57 CST 2017

<a id="cal">cal</a>

Display a calendar | 显示日历

  • cal [options][[[day], month], year]
    -1 显示一个月
    -3 display prev/current/next month
[centos6@root ~]# cal
      July 2017     
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
[centos6@root ~]# cal 20 06 1990
      June 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

<a id="history">history</a>

Display or manipulate the history list | 显示或操作命令历史
从登录开始所操作的命令历史若没有人为追加至命令历史文件中,则存在于内存中,当用户退出登录时自动追加至命令历史文件中
命令历史文件~/.bash_history, m

  • 命令历史大小,显示受以下变量控制
    这些变量要永久生效需将其写入配置文件中/etc/profile,~/.bash_profile
    • HISTFILE #定义了命令历史文件的位置
    • HISTSIZE #定义内存中命令历史记录的最大数量
    • HISTFILESIZE #定义命令历史文件中存放记录的最大数量
    • HISTCONTROL
      • ignoredups #连续重复的命令只记录一条
      • ignorespace #命令执行前有空白的,不记录
      • ignoreboth #上面两条同时生效
    • HISTTIMEFORMAT ="%F %T" 定义命令历史记录中显示的时间格式
    • HISTIGNORE="str*" #以str开头的命令不记录
  • history [options][name]
    -c 清空当前内存中的命令历史
    -d NAME 从内存中删除某条命令历史记录
    -a 将内存中未从文件中读取,新产生的命令历史记录追加至文件中
    -w 将当前内存中的命令历史覆盖命令历史文件
    -p 不将当前执行的扩展命令记录至命令历史中
# 默认HISTSIZE为1000,在/etc/profile中定义了其大小
[centos6@root ~]# echo $HISTSIZE
1000
[centos6@root ~]# cat /etc/profile | grep HISTSIZE
HISTSIZE=1000
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

[centos6@root ~]# echo $HISTFILESIZE
1000

[centos6@root ~]# history | tail -n 10
  445  help history
  446  clear
  447  history 
  448  history -d clear
  449  history -d 440
  450  clear
  451  pwd
  452  ifconfig
  453  hostname
  454  history | tail -n 10

[centos6@root ~]# history -d 450
[centos6@root ~]# history | tail -n 10
  446  clear
  447  history 
  448  history -d clear
  449  history -d 440
  450  pwd
  451  ifconfig
  452  hostname
  453  history | tail -n 10
  454  history -d 450
  455  history | tail -n 10

[centos6@root ~]# history -c
[centos6@root ~]# history
    1  history

[centos6@root ~]# cat ~/.bash
.bash_history  .bash_logout   .bash_profile  .bashrc        
[centos6@root ~]# cat ~/.bash_history 
history -r
。。。
[centos6@root ~]# cat ~/.bash_history | wc -l
356

# -a 追加
[centos6@root ~]# history -a
[centos6@root ~]# cat ~/.bash_history | wc -l
363
[centos6@root ~]# clear
[centos6@root ~]# history -c
[centos6@root ~]# history
    1  history

# -w  覆盖
[centos6@root ~]# history -w
[centos6@root ~]# cat ~/.bash_history | wc -l
2

<a id="rz">rz</a> <a id="">sz</a>

rz, sz 是在安装了lrzsz程序包后产生的两个工具,用于与windows系统互传文件

  • rz #从widows系统上选择需上传至linux的文件
  • sz FILE #将FILE从Liux中下载至windows系统

<a id="ldd">ldd</a>

Print shared library dependencies | 显示命令所依赖的共享库

  • ldd command
[centos6@root ~]# ldd /bin/ls
    linux-vdso.so.1 =>  (0x00007fffccfa2000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00000030d2e00000)
    librt.so.1 => /lib64/librt.so.1 (0x00000030d1e00000)
    libcap.so.2 => /lib64/libcap.so.2 (0x00000030d8200000)
    libacl.so.1 => /lib64/libacl.so.1 (0x00000030dc600000)
    libc.so.6 => /lib64/libc.so.6 (0x00000030d1600000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00000030d1200000)
    /lib64/ld-linux-x86-64.so.2 (0x00000030d0e00000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00000030d1a00000)
    libattr.so.1 => /lib64/libattr.so.1 (0x00000030dbe00000)

[centos6@root ~]# ldd /bin/cat
    linux-vdso.so.1 =>  (0x00007ffe611f0000)
    libc.so.6 => /lib64/libc.so.6 (0x00000030d1600000)
    /lib64/ld-linux-x86-64.so.2 (0x00000030d0e00000)

<a id="uname">uname</a>

Print system information | 显示系统版本信息

  • uname [option]
    -a 显示所有
    -r 内核版本
  • 查看发现版本号可通过查看以下两个文件
    /etc/redhat-release
    /etc/centos-release
[centos6@root ~]# uname 
Linux

[centos6@root ~]# uname -a
Linux centos6.zhubiao.science 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

[centos6@root ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)

[centos6@root ~]# cat /etc/centos-release 
CentOS release 6.9 (Final)

<a id="timedatectl">timedatectl</a>

Control the system time and date 显示、设定时间(centos7)

  • timedatectl [options]
    set-time string 设定时间
    set-ntp bool 是否启用ntp同步时间
    list-timezones #列出所有时区
    set-timezone #设定时区

对于centos7系统,修改时区其实是修改了/etc/localtime的符号链接指向,centos6也可以通过直接修改链接指向达到修改时区的目的

以下为CentOS7上的操作
设定时间
[centos7@root ~]# timedatectl set-time "2017-07-18 15:35:00"
#查看当前时区设置为上海
[centos7@root ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Jul 17 20:01 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai 

[centos7@root ~]# timedatectl list-timezones | grep New_York
America/New_York

#设定时区为New_York
[centos7@root ~]# timedatectl set-timezone America/New_York
[centos7@root ~]# date
Mon Jul 17 21:12:38 EDT 2017
[centos7@root ~]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 38 Jul 17 21:12 /etc/localtime -> ../usr/share/zoneinfo/America/New_York

# 对于CentOS6系统直接建立符号链接指向相应的时区即可,操作前备份原文件
[centos6@root ~]# ll /usr/share/zoneinfo/America/New_York 
-rw-r--r--. 3 root root 3519 Dec  1  2016 /usr/share/zoneinfo/America/New_York
[centos6@root ~]# ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

<a id="screen">screen</a>

要求同一个用户,同一台主机
screen非常有用的一个工具,可以实现多人在同一个会话中操作Linux,实现字符界面的共享,当你由于各种原因导致退出会话,会话中所运行的程序并不会结束,仍然继续运行

  • screen [options]
    -S name #创建名为name的会话
    -x name #加入name会话
    ctrl+a, d #脱离会话
    -r #恢复会话
    exit 结束会话
    -ls 列出会话
#创建会话
[centos6@root ~]# screen -S zb10
#列出现发起的会话
[centos6@root ~]# screen -ls
There is a screen on:
    50753.zb10  (Attached)
1 Socket in /var/run/screen/S-root.

#加入会话
screen -x zb10

# 脱离会话
同时按下ctrl+a, 再按d

#再加入刚脱离的会话
[centos6@root ~]# screen -r

#退出会话
[centos6@root ~]# exit
2017-07-18_095843.png

<a id="chvt">chvt</a>

虚拟终端之间互相切换,或图形终端上开启的伪终端与虚拟终端之间的切换

  • chvt NUM #NUM终端号1-6
[centos6@root ~]# chvt 1

<a id="script">script</a> <a id="scriptreplay">scriptreplay</a>

  • script
    make typescript of terminal session | 录屏(字符界面)
    -o script_file 将录制的内容存放于文件中
    -t 时间通过标准错误输出到屏幕,可通过重定向到到文件中2>time_file
    exit 退出录屏
  • scriptreplay 回放
    scriptreplay time_file script_file
    playback typescripts, using time information
#开始录屏
[centos6@root ~]# script -a script_file -t 2>time_file 
Script started, file is script_file
[centos6@root ~]# pwd
/root
[centos6@root ~]# ls
1.log  a                Desktop      install.log.syslog  Pictures     test
1.txt  anaconda-ks.cfg  Documents    localtime           Public       time_file
2.txt  a.tar            Downloads    man.1               script_file  typescript
3.txt  core.3283        install.log  Music               Templates    Videos
[centos6@root ~]# exit #退出录屏
exit
Script done, file is script_file

#回放,下面除了第一条回放操作命令以外,其它的操作均是回放
[centos6@root ~]# scriptreplay time_file script_file 
[centos6@root ~]# pwd
/root
[centos6@root ~]# ls
1.log  a                Desktop      install.log.syslog  Pictures     test
1.txt  anaconda-ks.cfg  Documents    localtime           Public       time_file
2.txt  a.tar            Downloads    man.1               script_file  typescript
3.txt  core.3283        install.log  Music               Templates    Videos
[centos6@root ~]# exit[centos6@root ~]# 

<a id="man">man</a>

查看外部命令的帮助文档

  • man command
  • man 手册配置文件位置
    centos6 /etc/man.config
    centos7 /etc/man_db.conf

<a id="touch">touch</a>

Change file timestamps | 修改文件时间戳

  • touch [options] file

    不指定选项则同时修改atime,mtime,ctime为当前系统时间,若指定-d,或-t 则修改atime和mtime

    -a change only the access time 仅修改访问时间(指定时间),取当前时间则同时修改atime,ctime

    -m change only the modification 仅修改访问时间(指定时间),取当前时间则同时修改mtime,ctime

    -d "Time_String" 指定修改的时间,不指定则取当前时间

    -t [[CC]YY]mmddHHMM.[SS] 修改atime 和 mtime

[centos7@root ~]# stat destination.txt | tail -n 4
Access: 2017-07-19 08:06:07.188923685 +0800
Modify: 2017-07-16 13:49:13.717259219 +0800
Change: 2017-07-16 13:49:13.717259219 +0800

 # 不指定任何选项,修改atime,mtime,ctime的时间为当前时间
[centos7@root ~]# touch destination.txt 
[centos7@root ~]# stat destination.txt | tail -n 4
Access: 2017-07-20 08:21:19.069932595 +0800
Modify: 2017-07-20 08:21:19.069932595 +0800
Change: 2017-07-20 08:21:19.069932595 +0800

[centos7@root ~]# stat 3.txt
 。。。
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2017-07-19 08:06:07.183923685 +0800
Modify: 2017-07-17 13:47:26.513884898 +0800
Change: 2017-07-17 13:47:26.513884898 +0800

# -d 指定特定时间,则修改atime和mtime
[centos7@root ~]# touch -d "20191010" 3.txt
[centos7@root ~]# stat 3.txt | tail -n 4
Access: 2019-10-10 00:00:00.000000000 +0800
Modify: 2019-10-10 00:00:00.000000000 +0800
Change: 2017-07-20 08:22:31.254930888 +0800

# -a,-m若不指定时间则,即去当前时间,则分别修改atime、ctime和mtime、ctime,若和-d,-t配合使用,则仅修改atime和mtime
[centos7@root ~]# stat cat.bk | tail -n 4
Access: 2017-07-19 08:06:07.187923685 +0800
Modify: 2017-07-16 11:06:02.177594191 +0800
Change: 2017-07-16 11:06:02.177594191 +0800

[centos7@root ~]# touch -a -d "19900620" cat.bk
[centos7@root ~]# stat cat.bk | tail -n 4
Access: 1990-06-20 00:00:00.000000000 +0900
Modify: 2017-07-16 11:06:02.177594191 +0800
Change: 2017-07-20 08:26:12.297925663 +0800

[centos7@root ~]# touch -m cat.bk; stat cat.bk | tail -n 4
Access: 1990-06-20 00:00:00.000000000 +0900
Modify: 2017-07-20 08:27:32.736923762 +0800
Change: 2017-07-20 08:27:32.736923762 +0800

# 同时修改atime和mtime
[centos7@root ~]# stat etc.tar | tail -n 4
Access: 2017-07-19 08:06:07.192923685 +0800
Modify: 2017-07-18 17:00:01.774825466 +0800
Change: 2017-07-18 17:00:01.774825466 +0800
[centos7@root ~]# touch -t 199006201010.10 etc.tar 
[centos7@root ~]# stat etc.tar | tail -n 4
Access: 1990-06-20 10:10:10.000000000 +0900
Modify: 1990-06-20 10:10:10.000000000 +0900
Change: 2017-07-20 08:35:25.104912595 +0800

<a id="tree">tree</a>

-L NUM #指定所列目录层次

[centos7@root app]# tree
.
├── dir1
│   └── a
│       └── bc
└── dir2

4 directories, 0 files
# 列出第一层目录
[centos7@root app]# tree -L 1
.
├── dir1
└── dir2

2 directories, 0 files

<a id="ln">ln</a>

创建链接

  • ln [options] TAREGET LINK_NAME

    不加选项则创建硬链接

    -s 创建符号链接(软链接)

#创建符号链接
[centos7@root app]# tree
.
├── dir1
│   └── a
│       └── bc
└── dir2

4 directories, 0 files
[centos7@root app]# cd dir2
[centos7@root dir2]# ln -s dir1/a/bc ../bc.link 
[centos7@root dir2]# cd ..
[centos7@root app]# tree
.
├── bc.link -> dir1/a/bc
├── dir1
│   └── a
│       └── bc
└── dir2

5 directories, 0 files

#创建硬链接
[centos7@root app]# ln 1.txt 1.ln
[centos7@root app]# ll -i ./*
67 -rw-r--r--. 2 root root 0 Jul 20 09:06 ./1.ln
67 -rw-r--r--. 2 root root 0 Jul 20 09:06 ./1.txt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容