Linux系统命令从入门到完全忘记(一)

1,960次阅读
没有评论

Linux系统命令从入门到完全忘记(一)

线上查询及帮助命令

man

概要: 查看命令或配置文件的帮助手册

语法格式: man [Command]… man [FILE]…

使用示例:

  • 查看命令的帮助
<span class="line">1</span>
<span class="line">[root@ansheng ~]<span class="comment"># man passwd</span></span>
  • 查看配置文件的帮助信息
<span class="line">1</span>
<span class="line">[root@ansheng ~]<span class="comment"># man /etc/inittab</span></span>

目录操作命令

ls

简介: 显示指定目录下的内容

语法格式: ls [OPTION]… [FILE]…

参数:

参数 说明
-a 显示所有文件,包括隐藏文件
-A 不显示目录下面的”.”和”..”
-r 倒序显示文件
-t 以文件修改时间显示
-d 显示目录
-h 人类可读形式
-l 以长格式显示文件信息
-i 查看文件的i节点号
-F/-p 给不同文件结尾添加符号
–color 不同文件显示颜色
–time-style= [long-iso\full-iso\iso]时间的格式

使用示例:

  • 列出当前目录下所有包含”ansheng”的目录或者文件
<span class="line">1</span>
2
3
4
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">ansheng.me  sadan32shengwe.me  sadansheng.me  sadanshengwe.me</span>
<span class="line">[root@ansheng ~]<span class="comment"># ls *ansheng*    </span></span>
<span class="line">ansheng.me  sadansheng.me  sadanshengwe.me</span>
  • 显示当前文件所在目录所有文件的详细信息,包括隐藏文件
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span class="line">[root@ansheng ~]<span class="comment"># ls -laA</span></span>
<span class="line">total 36</span>
<span class="line">dr-xr-x---.  2 root root 4096 Apr 21 09:33 .</span>
<span class="line">dr-xr-xr-x. 22 root root 4096 Apr 21 09:13 ..</span>
<span class="line">-rw-r--r--   1 root root    0 Apr 21 09:33 ansheng.me</span>
<span class="line">-rw-------.  1 root root   28 Apr  9 18:29 .bash_<span class="built_in">history</span></span>
<span class="line">-rw-r--r--.  1 root root   18 May 20  2009 .bash_<span class="built_in">logout</span></span>
<span class="line">-rw-r--r--.  1 root root  176 May 20  2009 .bash_profile</span>
<span class="line">-rw-r--r--.  1 root root  176 Sep 23  2004 .bashrc</span>
<span class="line">-rw-r--r--.  1 root root  100 Sep 23  2004 .cshrc</span>
<span class="line">-rw-r--r--   1 root root    0 Apr 21 09:33 sadan32shengwe.me</span>
<span class="line">-rw-r--r--   1 root root    0 Apr 21 09:33 sadansheng.me</span>
<span class="line">-rw-r--r--   1 root root    0 Apr 21 09:33 sadanshengwe.me</span>
<span class="line">-rw-r--r--.  1 root root  129 Dec  4  2004 .tcshrc</span>
<span class="line">-rw-------.  1 root root  741 Apr  9 18:08 .viminfo</span>
  • 当文件过多时,ls要显示好几屏文件,如何显示最近修改的文件或目录
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span class="line">[root@ansheng ~]<span class="comment"># cd /var/</span></span>
<span class="line">[root@ansheng var]<span class="comment"># ls -lrt</span></span>
<span class="line">total 76</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 yp</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 preserve</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 opt</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 nis</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 <span class="built_in">local</span></span>
<span class="line">drwxr-xr-x.  2 root root 4096 Sep 23  2011 games</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Nov 22  2013 cvs</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Mar 23 00:12 crash</span>
<span class="line">drwxr-xr-x.  6 root root 4096 Mar 23 03:04 www</span>
<span class="line">lrwxrwxrwx.  1 root root   10 Apr  9 17:58 mail -> spool/mail</span>
<span class="line">drwxr-xr-x.  3 root root 4096 Apr  9 18:00 db</span>
<span class="line">drwxr-xr-x.  3 root root 4096 Apr  9 18:03 empty</span>
<span class="line">drwxr-xr-x. 11 root root 4096 Apr  9 18:03 spool</span>
<span class="line">drwxr-xr-x.  2 root root 4096 Apr  9 18:03 account</span>
<span class="line">drwxr-xr-x. 10 root root 4096 Apr  9 18:03 cache</span>
<span class="line">drwxr-xr-x. 25 root root 4096 Apr  9 18:27 lib</span>
<span class="line">drwxrwxrwt.  2 root root 4096 Apr  9 18:28 tmp</span>
<span class="line">drwxrwxr-x.  5 root lock 4096 Apr  9 18:28 lock</span>
<span class="line">drwxr-xr-x. 10 root root 4096 Apr 21 09:13 <span class="built_in">log</span></span>
<span class="line">drwxr-xr-x. 19 root root 4096 Apr 21 09:13 run</span>
  • 如何过滤出当前目录下中的所有一级目录?

当前目录下有用以下文件及目录

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
<span class="line">[root@ansheng ~]<span class="comment"># ll</span></span>
<span class="line">total 28</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng</span>
<span class="line">-rw-r--r-- 1 root root    0 Apr 21 09:33 ansheng.me</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 d</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 r</span>
<span class="line">-rw-r--r-- 1 root root    0 Apr 21 09:33 sadan32shengwe.me</span>
<span class="line">-rw-r--r-- 1 root root    0 Apr 21 09:33 sadansheng.me</span>
<span class="line">-rw-r--r-- 1 root root    0 Apr 21 09:33 sadanshengwe.me</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg</span>

第一种方法:

<span class="line">1</span>
2
3
4
5
6
7
8
<span class="line">[root@ansheng ~]<span class="comment"># ll -F|grep "/$"</span></span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 d/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 r/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg/</span>

第二种方法:

<span class="line">1</span>
2
3
4
5
6
7
8
<span class="line">[root@ansheng ~]<span class="comment"># ll -F|grep "^d"</span></span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ad/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 ansheng/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 d/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 gsdf/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 hf/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 r/</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 21 09:40 wdg/</span>

小结:可以用ls –l或者ll显示文件的属性信息,然后通过-F参数给目录后面加个标示符,过滤出以“/”结尾的即可,或者过滤出以”d”开头的

tree

概要: 把目录以树状的格式列出,如果系统没有这个命令请使用以下指令进行安装:

<span class="line">1</span>
<span class="line">[root@ansheng ~]<span class="comment"># yum -y install tree</span></span>

语法格式: tree [OPTION]… [PATH]…

参数:

参数 说明
-a 把所有文件以目录树的格式显示
-d 把所有的目录以树状形式显示
-g 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码
-L 指定目录层次
-l 当显示的目录或文件问链接是,则同时显示源文件
-p 显示文件或目录的权限
-s 列出文件或目录大小
-u 显示文件或目录的所属主

使用示例:

  • 以树状的形式显示/usr目录下的所有一级目录
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="line">[root@ansheng ~]<span class="comment"># tree -L 1 /usr</span></span>
<span class="line">/usr</span>
<span class="line">├── bin</span>
<span class="line">├── etc</span>
<span class="line">├── games</span>
<span class="line">├── include</span>
<span class="line">├── lib</span>
<span class="line">├── lib64</span>
<span class="line">├── libexec</span>
<span class="line">├── <span class="built_in">local</span></span>
<span class="line">├── sbin</span>
<span class="line">├── share</span>
<span class="line">├── src</span>
<span class="line">└── tmp -> ../var/tmp</span>

<span class="line">12 directories, 0 files</span>
  • 以树状形式列出/root目录的所有文件并且显示用户组合用户主
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<span class="line">[root@ansheng ~]<span class="comment"># tree -agu ./</span></span>
<span class="line">./</span>
<span class="line">├── [root     root    ]  ad</span>
<span class="line">├── [root     root    ]  ansheng</span>
<span class="line">├── [root     root    ]  ansheng.me</span>
<span class="line">├── [root     root    ]  .bash_<span class="built_in">history</span></span>
<span class="line">├── [root     root    ]  .bash_<span class="built_in">logout</span></span>
<span class="line">├── [root     root    ]  .bash_profile</span>
<span class="line">├── [root     root    ]  .bashrc</span>
<span class="line">├── [root     root    ]  .cshrc</span>
<span class="line">├── [root     root    ]  d</span>
<span class="line">├── [root     root    ]  gsdf</span>
<span class="line">├── [root     root    ]  hf</span>
<span class="line">├── [root     root    ]  r</span>
<span class="line">├── [root     root    ]  sadan32shengwe.me</span>
<span class="line">├── [root     root    ]  sadansheng.me</span>
<span class="line">├── [root     root    ]  sadanshengwe.me</span>
<span class="line">├── [root     root    ]  .tcshrc</span>
<span class="line">├── [root     root    ]  .viminfo</span>
<span class="line">└── [root     root    ]  wdg</span>

<span class="line">7 directories, 11 files</span>
  • 以树状形式列出/etc/yum.repos.d目录,并且显示文件权限以及大小
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
<span class="line">[root@ansheng ~]<span class="comment"># cd /etc/yum.repos.d/</span></span>
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># tree -sp ./</span></span>
<span class="line">./</span>
<span class="line">├── [-rw-r--r--        2572]  CentOS-Base.repo</span>
<span class="line">├── [-rw-r--r--        1083]  epel.repo</span>
<span class="line">└── [drwxr-xr-x        4096]  repo</span>
<span class="line">    ├── [-rw-r--r--        1991]  CentOS-Base.repo</span>
<span class="line">    ├── [-rw-r--r--         647]  CentOS-Debuginfo.repo</span>
<span class="line">    ├── [-rw-r--r--         289]  CentOS-fasttrack.repo</span>
<span class="line">    ├── [-rw-r--r--         630]  CentOS-Media.repo</span>
<span class="line">    └── [-rw-r--r--        6259]  CentOS-Vault.repo</span>

<span class="line">1 directory, 7 files</span>

pwd

概要: 打印现在所在目录名

语法格式: pwd [OPTION]…

参数:

参数 说明
-P 当所在目录为链接目录是,显示源目录

使用示例:

  • 显示当前所在目录
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># pwd</span></span>
<span class="line">/root</span>
  • 当所在目录为链接目录时,要求显示源目录
<span class="line">1</span>
2
3
4
5
6
7
8
<span class="line">[root@ansheng ~]<span class="comment"># ln -s /etc/sysconfig ./</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls -d sysconfig</span></span>
<span class="line">sysconfig</span>
<span class="line">[root@ansheng ~]<span class="comment"># cd sysconfig/</span></span>
<span class="line">[root@ansheng sysconfig]<span class="comment"># pwd</span></span>
<span class="line">/root/sysconfig</span>
<span class="line">[root@ansheng sysconfig]<span class="comment"># pwd -P</span></span>
<span class="line">/etc/sysconfig</span>

mkdir

使用示例: 创建一个或多个目录

语法格式: mkdir [OPTION]… DIRECTORY…

参数:

参数 说明
-m 创建目录时同时设置目录权限
-p 递归创建目录
-v 显示目录创建过程

使用示例:

  • 请用一条命令在当前目录下创建名为ansheng3,ansheng6的目录,其中ansheng3中含有子目录ansheng6,ansheng5,ansheng4中含有子目录test,并用一条命令验证输出结果
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
<span class="line">[root@ansheng ~]<span class="comment"># mkdir -pv ./oldboy3/oldboy{4/test,5} ./oldboy6 && tree</span></span>
<span class="line">mkdir: created directory `./oldboy3<span class="string">'</span>
<span class="line">mkdir: created directory `./oldboy3/oldboy4'</span></span>
<span class="line">mkdir: created directory `./oldboy3/oldboy4/<span class="built_in">test</span><span class="string">'</span>
<span class="line">mkdir: created directory `./oldboy3/oldboy5'</span></span>
<span class="line">mkdir: created directory `./oldboy6<span class="string">'</span>
<span class="line">.</span>
<span class="line">├── oldboy3</span>
<span class="line">│   ├── oldboy4</span>
<span class="line">│   │   └── test</span>
<span class="line">│   └── oldboy5</span>
<span class="line">└── oldboy6</span>

<span class="line">5 directories, 0 files</span></span>
  • 请用mkdir命令在/root目录下创建目录test,并且设置权限为700
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># mkdir -m 700 test && ll -d test</span></span>
<span class="line">drwx------ 2 root root 4096 Apr 21 09:58 <span class="built_in">test</span></span>
  • 请用一条命令在当前下分别创建d1 d2 d3这三个同级目录
<span class="line">1</span>
2
3
4
5
6
7
<span class="line">[root@ansheng ~]<span class="comment">#  mkdir ./d1 ./d2 ./d3 && tree</span></span>
<span class="line">.</span>
<span class="line">├── d1</span>
<span class="line">├── d2</span>
<span class="line">└── d3</span>

<span class="line">3 directories, 0 files</span>

rmdir

概要: 删除目录,删除的这个目录必须是空目录

语法格式: rmdir [OPTION]… DIRECTORY…

参数:

参数 说明
-p 递归删除空目录,删除子目录后当父目录为空目录时也一同删除
-v 显示命令执行过程

使用示例:

  • 删除linux/china这个目录,并且显示执行过程
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
<span class="line">[root@ansheng ~]<span class="comment"># tree ./</span></span>
<span class="line">./</span>
<span class="line">├── gun</span>
<span class="line">└── linux</span>
<span class="line">    └── china</span>

<span class="line">3 directories, 0 files</span>
<span class="line">[root@ansheng ~]<span class="comment"># rmdir -p -v linux/china && tree</span></span>
<span class="line">rmdir: removing directory, `linux/china<span class="string">'</span>
<span class="line">rmdir: removing directory, `linux'</span></span>
<span class="line">.</span>
<span class="line">└── gun</span>

<span class="line">1 directory, 0 files</span>

cd

概要: 更改当前所在目录,是系统内置命令

语法格式: cd [OPTION]… cd [directory]…

参数:

参数 说明
~ 用户家目录
返回上次所在目录
.. 返回所在目录的上一层目录

使用示例:

  • 当前目录在/tnp下,请用一条命令返回家目录(不可写家目录的绝对路径)
<span class="line">1</span>
2
3
4
5
[root@ansheng tmp]# pwd
/tmp
[root@ansheng tmp]# cd ~
<span class="line">[root@ansheng ~]<span class="comment"># pwd</span></span>
<span class="line">/root</span>
  • 假如我当前在/etc/yum.repos.d/下,然后又进入到了/etc/init.d/目录下,现在我希望快速返回上一次进入的目录,即/etc/yum.repos.d/目录,该如何做呢
<span class="line">1</span>
2
3
4
5
6
7
8
9
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># pwd</span></span>
<span class="line">/etc/yum.repos.d</span>
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># cd /etc/init.d/</span></span>
<span class="line">[root@ansheng init.d]<span class="comment"># pwd</span></span>
<span class="line">/etc/init.d</span>
<span class="line">[root@ansheng init.d]<span class="comment"># cd -</span></span>
<span class="line">/etc/yum.repos.d</span>
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># pwd</span></span>
<span class="line">/etc/yum.repos.d</span>
  • 当前所在目录/etc/yum.repos.d/,如何返回上一层目录,即/etc/
<span class="line">1</span>
2
3
4
5
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># pwd</span></span>
<span class="line">/etc/yum.repos.d</span>
<span class="line">[root@ansheng yum.repos.d]<span class="comment"># cd ../</span></span>
<span class="line">[root@ansheng etc]<span class="comment"># pwd</span></span>
<span class="line">/etc</span>

文件操作命令

touch

概要: 创建单个或多个文件,或者更改时间戳

语法格式: touch [OPTION]… FILE…

使用示例:

  • 同时创建文件f1 f2 f3 f4 f5

第一种方法:

<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># touch f1 f2 f3 f4 f5</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">f1  f2  f3  f4  f5</span>

第二种方法:

<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># touch f{1..5}</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">f1  f2  f3  f4  f5</span>
  • 更改file文件的时间戳
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<span class="line">[root@ansheng ~]<span class="comment"># stat /etc/issue</span></span>
<span class="line">  File: `/etc/issue<span class="string">'</span>
<span class="line">  Size: 47              Blocks: 8          IO Block: 4096   regular file</span>
<span class="line">Device: 803h/2051d      Inode: 1054636     Links: 1</span>
<span class="line">Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)</span>
<span class="line">Access: 2016-04-21 09:13:35.832310735 +0800</span>
<span class="line">Modify: 2015-08-04 00:13:46.000000000 +0800</span>
<span class="line">Change: 2016-04-09 18:01:45.370999965 +0800</span>
<span class="line">[root@ansheng ~]# touch issue</span>
<span class="line">[root@ansheng ~]# stat /etc/issue</span>
<span class="line">  File: `/etc/issue'</span></span>
<span class="line">  Size: 47              Blocks: 8          IO Block: 4096   regular file</span>
<span class="line">Device: 803h/2051d      Inode: 1054636     Links: 1</span>
<span class="line">Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)</span>
<span class="line">Access: 2016-04-21 09:13:35.832310735 +0800</span>
<span class="line">Modify: 2015-08-04 00:13:46.000000000 +0800</span>
<span class="line">Change: 2016-04-09 18:01:45.370999965 +0800</span>

cp

概要: 复制文件或者目录

语法格式: cp [OPTION]… SOURCE… DIRECTORY

参数:

参数 说明
-a 相当于同时执行”-dpR”参数
-b 备份目标文件,~表示备份的文件
-f 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 若源文件与目标文件重名则提示是否覆盖
-n 跳过已存在的文件或目录
-R/-r 复制源目录下面的所有文件包括目录
-p 保持源文件的属性
-s 为源文件创建连接

使用示例:

  • 复制/tmp/目录下的linux文件到/root目录下,已知/root目录下已有Linux这个文件,要求备份目标文件。
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
<span class="line">[root@ansheng ~]<span class="comment"># echo "root" > linux</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># echo "tmp" > /tmp/linux</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cp --backup /tmp/linux linux  </span></span>
<span class="line">cp: overwrite `linux<span class="string">'? y</span>
<span class="line">[root@ansheng ~]# ls</span>
<span class="line">linux  linux~</span>
<span class="line">[root@ansheng ~]# cat linux</span>
<span class="line">tmp</span>
<span class="line">[root@ansheng ~]# cat linux~ </span>
<span class="line">root</span></span>
  • 复制当前目录下面的linux文件到/opt下,要求保持源文件属性
<span class="line">1</span>
2
3
4
5
<span class="line">[root@ansheng ~]<span class="comment"># ll linux </span></span>
<span class="line">-rw-r--r-- 1 root root 4 Apr 21 10:30 linux</span>
<span class="line">[root@ansheng ~]<span class="comment"># cp -p linux /opt/</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ll /opt/linux    </span></span>
<span class="line">-rw-r--r-- 1 root root 4 Apr 21 10:30 /opt/linux</span>
  • 为/etc/passwd文件创建一个链接文件到/root/下
<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># cp -s /etc/passwd /root/</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">passwd</span>

mv

概要: 移动或更改文件/目录名

语法格式:

mv [参数] [源文件或目录] [目标文件或目录]

参数:

参数 说明
-b 如果源文件与目标文件同名时,则备份目标文件,用”~”区别
-f 如果目标文件已存在,则不提示直接覆盖
-i 若目标文件已存在,则提示是否覆盖
-u 若目标文件已经存在,且source比较新,才会更新(update)
-t 移动多个文件夹到一个目录

使用示例:

  • 将/root目录下面的test目录移动到/tmp目录下并且把目录名称改为“testlinux”
<span class="line">1</span>
2
3
4
5
6
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line"><span class="built_in">test</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># mv test /tmp/testlinux</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls -d /tmp/testlinux/</span></span>
<span class="line">/tmp/testlinux/</span>
  • 将/root目录下面的d1 d2 d3 d4目录移动到/tmp目录下面的d5目录中
<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># mv d1 d2 d3 d4 /tmp/d5</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls /tmp/d5/</span></span>
<span class="line">d1  d2  d3  d4</span>
  • 已知/roo目录和/tmp目录都有linux这个文件,请将/root目录下面的linux移动到/tmp目录下,并且没有任何提示。
<span class="line">1</span>
2
3
4
<span class="line">[root@ansheng ~]<span class="comment"># mv -f /root/linux /tmp/</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls /root/</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls /tmp/</span></span>
<span class="line">linux</span>

rm

概要: 删除单个或多个文件和目录

语法格式: rm [OPTION]… FILE…

参数:

参数 说明
-f 将目录下的文件及目录全部删除
-i 删除前询问
-r/-R 递归式的删除
-v 显示命令执行过程

使用示例:

  • 删除当前目录下的所有文件和目录,并且显示执行过程
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">asdasd  assgrf  d  df  dg  e  g  saf  sd  sdf</span>
<span class="line">[root@ansheng ~]<span class="comment"># rm -rfv ./*</span></span>
<span class="line">removed `./asdasd<span class="string">'</span>
<span class="line">removed directory: `./assgrf'</span></span>
<span class="line">removed directory: `./d<span class="string">'</span>
<span class="line">removed `./df'</span></span>
<span class="line">removed `./dg<span class="string">'</span>
<span class="line">removed directory: `./e'</span></span>
<span class="line">removed `./g<span class="string">'</span>
<span class="line">removed `./saf'</span></span>
<span class="line">removed `./sd<span class="string">'</span>
<span class="line">removed directory: `./sdf'</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>

ln

概要: 创建文件或目录的链接

语法格式: ln [参数] [源文件] [目标文件]

参数:

参数 说明
-s 创建软连接

使用示例:

  • 为/etc/passwd文件创建硬连接到当前目录下
<span class="line">1</span>
2
3
4
<span class="line">[root@ansheng ~]<span class="comment"># ln /etc/passwd ./</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ll</span></span>
<span class="line">total 4</span>
<span class="line">-rw-r--r--. 2 root root 1273 Apr  9 18:05 passwd</span>
  • 为/boot目录创建软连接到当前目录
<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># ln -s /boot ./</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ls</span></span>
<span class="line">boot</span>

find

概要: 用于在文件树种查找文件,并作出相应的处理

语法格式:

find [目录] [option]

参数:

参数 说明
-name 按照文件名称查找文件
-user 按照文件属主查找文件
-group 按照文件属组查找文件
-mtime 按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示最近多少天
-type 按照文件类型查找文件
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-exec 执行命令,语法:-exec ‘command’ {} \;
! -name 排除某个文件或目录

使用示例:

  • 查找/var目录下面后缀为“.log”的所有文件
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<span class="line">[root@ansheng ~]<span class="comment"># find /var/ -type f -name "*.log"</span></span>
<span class="line">/var/<span class="built_in">log</span>/anaconda.program.log</span>
<span class="line">/var/<span class="built_in">log</span>/dracut.log</span>
<span class="line">/var/<span class="built_in">log</span>/prelink/prelink.log</span>
<span class="line">/var/<span class="built_in">log</span>/audit/audit.log</span>
<span class="line">/var/<span class="built_in">log</span>/anaconda.log</span>
<span class="line">/var/<span class="built_in">log</span>/boot.log</span>
<span class="line">/var/<span class="built_in">log</span>/anaconda.yum.log</span>
<span class="line">/var/<span class="built_in">log</span>/anaconda.ifcfg.log</span>
<span class="line">/var/<span class="built_in">log</span>/yum.log</span>
<span class="line">/var/<span class="built_in">log</span>/anaconda.storage.log</span>
<span class="line">``</span>

<span class="line">- 查找/etc/sysconfig/network-scripts目录下包含eth0的所有文件,并且属组和主都是root,然后以长格式显示这个文件的信息</span>

<span class="line">```bash</span>
<span class="line">[root@ansheng ~]<span class="comment"># find /etc/sysconfig/network-scripts/ -type f -name "*eth0*" -user root -group root -exec ls -l {} \;</span></span>
<span class="line">-rw-r--r--. 1 root root 271 Apr  9 17:58 /etc/sysconfig/network-scripts/ifcfg-eth0</span>
  • 已知 apache 服务的访问日志按天记录在服务器本地目录/app/logs 下,由于磁盘空间紧张,现在要求只能保留最近 7 天的访问日志!请问如何解决? 请给出解决办法或配置或处理命令

模拟日志脚本:

<span class="line">1</span>
2
3
4
5
6
<span class="line"><span class="meta">#!/bin/bash</span></span>
<span class="line"><span class="keyword">for</span> n <span class="keyword">in</span> `seq 14`</span>
<span class="line"><span class="keyword">do</span></span>
<span class="line">        date <span class="_">-s</span> <span class="string">"11/0<span class="variable">$n</span>/14"</span></span>
<span class="line">        touch access_www_`(date +%F)`.log</span>
<span class="line"><span class="keyword">done</span></span>

创建的文件:

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="line">[root@ansheng logs]<span class="comment"># ll</span></span>
<span class="line">total 0</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  1 00:00 access_www_2014-12-01.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  2 00:00 access_www_2014-12-02.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  3 00:00 access_www_2014-12-03.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  4 00:00 access_www_2014-12-04.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  5 00:00 access_www_2014-12-05.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  6 00:00 access_www_2014-12-06.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  7 00:00 access_www_2014-12-07.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  8 00:00 access_www_2014-12-08.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  9 00:00 access_www_2014-12-09.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 10 00:00 access_www_2014-12-10.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 11 00:00 access_www_2014-12-11.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 12 00:00 access_www_2014-12-12.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 13 00:00 access_www_2014-12-13.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 14 00:00 access_www_2014-12-14.log</span>

验证

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
<span class="line">[root@ansheng logs]<span class="comment"># sh -c 'find /app/logs/ -type f -mtime +7 -name "*.log"|xargs rm -f' </span></span>
<span class="line">[root@ansheng logs]<span class="comment"># ll</span></span>
<span class="line">total 0</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  7 00:00 access_www_2014-12-07.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  8 00:00 access_www_2014-12-08.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec  9 00:00 access_www_2014-12-09.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 10 00:00 access_www_2014-12-10.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 11 00:00 access_www_2014-12-11.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 12 00:00 access_www_2014-12-12.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 13 00:00 access_www_2014-12-13.log</span>
<span class="line">-rw-r--r--. 1 root root 0 Dec 14 00:00 access_www_2014-12-14.log</span>

文件查看及处理命令

cat

概要: 三大功能:

  • 一次性输出整个文件的内容
  • 创建一个新的文件,只能创建新文件不能编辑已有的文件
  • 将多个文件合并为一个文件

语法格式: cat [OPTION]… [FILE]…

参数:

参数 说明
-n 由1开始对输出内容的行数进行编号
-b 由1开始对输出内容的行进行编号,跳过空行不对其编号
-s 当遇到有连续两行以上的空白行,就代换为一行的空白行

使用示例:

  • 用cat命令将/etc/sysconfig/network-scripts/ifcfg-eth0的内容显示行号并且输入到testfile1里面
<span class="line">1</span>
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
<span class="line">[root@ansheng ~]<span class="comment"># cat /etc/sysconfig/network-scripts/ifcfg-eth0    </span></span>
<span class="line">DEVICE=eth0</span>
<span class="line">TYPE=Ethernet</span>
<span class="line">ONBOOT=yes</span>
<span class="line">NM_CONTROLLED=yes</span>
<span class="line">BOOTPROTO=static</span>
<span class="line">IPADDR=172.16.10.10</span>
<span class="line">NETMASK=255.255.255.0</span>
<span class="line">GATEWAY=172.16.10.2</span>
<span class="line">DNS1=172.16.10.2</span>
<span class="line">DEFROUTE=yes</span>
<span class="line">IPV4_FAILURE_FATAL=yes</span>
<span class="line">IPV6INIT=no</span>
<span class="line">NAME=<span class="string">"System eth0"</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat -n /etc/sysconfig/network-scripts/ifcfg-eth0 > textfile</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat textfile</span></span>
<span class="line">     1  DEVICE=eth0</span>
<span class="line">     2  TYPE=Ethernet</span>
<span class="line">     3  ONBOOT=yes</span>
<span class="line">     4  NM_CONTROLLED=yes</span>
<span class="line">     5  BOOTPROTO=static</span>
<span class="line">     6  IPADDR=172.16.10.10</span>
<span class="line">     7  NETMASK=255.255.255.0</span>
<span class="line">     8  GATEWAY=172.16.10.2</span>
<span class="line">     9  DNS1=172.16.10.2</span>
<span class="line">    10  DEFROUTE=yes</span>
<span class="line">    11  IPV4_FAILURE_FATAL=yes</span>
<span class="line">    12  IPV6INIT=no</span>
<span class="line">    13  NAME=<span class="string">"System eth0"</span></span>

高级应用:

  • 创建一个新的文件名为network并且输入内容”cat Advanced Applications”

方法1:

<span class="line">1</span>
2
3
4
5
<span class="line">[root@ansheng ~]<span class="comment"># cat > network</span></span>
<span class="line">cat Advanced Applications</span>
<span class="line">^C</span>
<span class="line">[root@ansheng ~]<span class="comment"># cat network </span></span>
<span class="line">cat Advanced Applications</span>

方法2

<span class="line">1</span>
2
3
4
5
<span class="line">[root@ansheng ~]<span class="comment"># cat >>network<<EOF</span></span>
<span class="line">> cat Advanced Applications</span>
<span class="line">> EOF</span>
<span class="line">[root@ansheng ~]<span class="comment"># cat network </span></span>
<span class="line">cat Advanced Applications</span>
  • 请将f1、f2、f3文件的内容追加到f4文件中
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
<span class="line">[root@ansheng ~]<span class="comment"># cat f1 f2 f3</span></span>
<span class="line">f1</span>
<span class="line">f2</span>
<span class="line">f3</span>
<span class="line">[root@ansheng ~]<span class="comment"># cat f4</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat f1 f2 f3 >> f4</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat f4</span></span>
<span class="line">f1</span>
<span class="line">f2</span>
<span class="line">f3</span>
  • 清空当前目录下面的networks文件内容
<span class="line">1</span>
2
3
4
<span class="line">[root@ansheng ~]<span class="comment"># cat network </span></span>
<span class="line">cat Advanced Applications</span>
<span class="line">[root@ansheng ~]<span class="comment"># cat /dev/null > network </span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat network</span></span>

概要: 输出文件头部内容,默认显示文件头部头10行

语法格式: head [OPTION]… [FILE]…

参数:

参数 说明
-n 显示文件头几行内容,-n 5也可以写作-5,显示文件头5行内容

使用示例:

  • 输出文件/etc/inittab文件的头5行内容
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
<span class="line">[root@ansheng ~]<span class="comment"># head -n 5 /etc/inittab  </span></span>
<span class="line"><span class="comment"># inittab is only used by upstart for the default runlevel.</span></span>
<span class="line"><span class="comment">#</span></span>
<span class="line"><span class="comment"># ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.</span></span>
<span class="line"><span class="comment">#</span></span>
<span class="line"><span class="comment"># System initialization is started by /etc/init/rcS.conf</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># head -5 /etc/inittab    </span></span>
<span class="line"><span class="comment"># inittab is only used by upstart for the default runlevel.</span></span>
<span class="line"><span class="comment">#</span></span>
<span class="line"><span class="comment"># ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.</span></span>
<span class="line"><span class="comment">#</span></span>
<span class="line"><span class="comment"># System initialization is started by /etc/init/rcS.conf</span></span>

tac

概要: 倒序的查看文件内容

语法格式: tac file

使用示例:

  • 查看文件/etc/issue内容
<span class="line">1</span>
2
3
4
5
6
7
8
<span class="line">[root@ansheng ~]<span class="comment"># cat /etc/issue</span></span>
<span class="line">CentOS release 6.7 (Final)</span>
<span class="line">Kernel \r on an \m</span>

<span class="line">[root@ansheng ~]<span class="comment"># tac /etc/issue</span></span>

<span class="line">Kernel \r on an \m</span>
<span class="line">CentOS release 6.7 (Final)</span>

tail

概要: 输出文件尾部内容,默认显示文件尾部10行

语法格式: tail [OPTION]… [FILE]…

参数:

参数 说明
-n 显示文件尾部几行内容,-n 5也可以写作-5,显示文件尾部5行内容
-f 实时跟踪一个文件内容的变化
-F 实时监控文件状态,当文件被删掉时继续监控

使用示例:

  • 输出文件/etc/sysconfig/network-scripts/ifcfg-eth0文件的尾3行内容
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<span class="line">[root@ansheng ~]<span class="comment"># cat /etc/sysconfig/network-scripts/ifcfg-eth0 </span></span>
<span class="line">DEVICE=eth0</span>
<span class="line">TYPE=Ethernet</span>
<span class="line">ONBOOT=yes</span>
<span class="line">NM_CONTROLLED=yes</span>
<span class="line">BOOTPROTO=static</span>
<span class="line">IPADDR=172.16.10.10</span>
<span class="line">NETMASK=255.255.255.0</span>
<span class="line">GATEWAY=172.16.10.2</span>
<span class="line">DNS1=172.16.10.2</span>
<span class="line">DEFROUTE=yes</span>
<span class="line">IPV4_FAILURE_FATAL=yes</span>
<span class="line">IPV6INIT=no</span>
<span class="line">NAME=<span class="string">"System eth0"</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># tail -n 3 /etc/sysconfig/network-scripts/ifcfg-eth0    </span></span>
<span class="line">IPV4_FAILURE_FATAL=yes</span>
<span class="line">IPV6INIT=no</span>
<span class="line">NAME=<span class="string">"System eth0"</span></span>

cut

概要: 切割,取列,适合简单的内容选取

语法格式: cut [-bn] [file] cut [-c] [file] cut [-df] [file]

参数:

参数 说明
-d 指定分隔符
-c 按照字符查找
-f 取列

使用示例:

  • 获取/etc/passwd文件中的用户名
<span class="line">1</span>
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
<span class="line">[root@ansheng ~]<span class="comment"># cut -d ":" -f1 /etc/passwd</span></span>
<span class="line">root</span>
<span class="line">bin</span>
<span class="line">daemon</span>
<span class="line">adm</span>
<span class="line">lp</span>
<span class="line">sync</span>
<span class="line">shutdown</span>
<span class="line">halt</span>
<span class="line">mail</span>
<span class="line">uucp</span>
<span class="line">operator</span>
<span class="line">games</span>
<span class="line">gopher</span>
<span class="line">ftp</span>
<span class="line">nobody</span>
<span class="line">dbus</span>
<span class="line">vcsa</span>
<span class="line">abrt</span>
<span class="line">haldaemon</span>
<span class="line">ntp</span>
<span class="line">apache</span>
<span class="line">saslauth</span>
<span class="line">postfix</span>
<span class="line">sshd</span>
<span class="line">stap-server</span>
<span class="line">oprofile</span>
<span class="line">tcpdump</span>
  • 获取文件ansheng中5-9数字
<span class="line">1</span>
2
3
4
<span class="line">[root@ansheng ~]<span class="comment"># cat ansheng </span></span>
<span class="line">1 2 3 4 5 6 7 8 9 10</span>
<span class="line">[root@ansheng ~]<span class="comment"># cut -c 9-17 ansheng </span></span>
<span class="line">5 6 7 8 9</span>

wc

概要: 统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

语法格式: wc [选项] 文件

参数:

参数 说明
-l 输出文件的行数
-c 显示一个文件的字节数
-m 显示一个文件的字符数
-L 显示一个文件中的最长行的长度
-w 显示一个文件的字数

使用示例:

  • 列出文件/etc/hosts的行数
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># wc -l /etc/hosts</span></span>
<span class="line">2 /etc/hosts</span>

file

概要: 查看文件类型

语法格式: file [文件]

使用示例:

  • 查看/etc/passwd文件类型
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># file /etc/passwd</span></span>
<span class="line">/etc/passwd: ASCII text</span>

nl

概要: 显示文件内容的行号,默认空行不加行号。

语法格式: nl file

使用示例:

  • 查看文件/etc/sysconfig/network-scripts/ifcfg-eth0的内容对应的行号
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
<span class="line">[root@ansheng ~]<span class="comment"># nl /etc/sysconfig/network-scripts/ifcfg-eth0 </span></span>
<span class="line">     1  DEVICE=eth0</span>
<span class="line">     2  TYPE=Ethernet</span>
<span class="line">     3  ONBOOT=yes</span>
<span class="line">     4  NM_CONTROLLED=yes</span>
<span class="line">     5  BOOTPROTO=static</span>
<span class="line">     6  IPADDR=172.16.10.10</span>
<span class="line">     7  NETMASK=255.255.255.0</span>
<span class="line">     8  GATEWAY=172.16.10.2</span>
<span class="line">     9  DNS1=172.16.10.2</span>
<span class="line">    10  DEFROUTE=yes</span>
<span class="line">    11  IPV4_FAILURE_FATAL=yes</span>
<span class="line">    12  IPV6INIT=no</span>
<span class="line">    13  NAME=<span class="string">"System eth0"</span></span>

概要: 查看链接文件的源地址

语法格式: Readlink linkdir/linkfile

使用示例:

  • 查看链接文件/etc/grub.conf文件的源地址
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># readlink /etc/grub.conf </span></span>
<span class="line">../boot/grub/grub.conf</span>

dos2unix

概要: dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter)。

<span class="line">1</span>
<span class="line">[root@ansheng ~]<span class="comment"># yum -y install dos2unix</span></span>

语法格式: dos2unix file1 file2 file3 …

使用示例:

  • 转换/etc/hosts的文件格式
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># dos2unix /etc/hosts</span></span>
<span class="line">dos2unix: converting file /etc/hosts to UNIX format ...</span>

sort

概要: sort命令是帮我们依据不同的数据类型进行排序

语法格式:

sort [-bcfMnrtk][源文件][-o 输出文件]

参数:

参数 说明
-b 忽略每行前面开始出的空格字符
-c 检查文件是否已经按照顺序排序
-f 排序时,忽略大小写字母
-M 将前面3个字母依照月份的缩写进行排序
-n 依照数值的大小排序
-o <输出文件> 将排序后的结果存入指定的文件
-r 以相反的顺序来排序
-t <分隔字符> 指定排序时所用的栏位分隔字符
-k 选择以哪个区间进行排序

使用示例:

  • sort将文件/文本的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
<span class="line">[root@ansheng ~]<span class="comment"># cat sort.txt </span></span>
<span class="line">aaa:10:1.1</span>
<span class="line">ccc:30:3.3</span>
<span class="line">ddd:40:4.4</span>
<span class="line">bbb:20:2.2</span>
<span class="line">eee:50:5.5</span>
<span class="line">eee:50:5.5</span>
<span class="line">[root@ansheng ~]<span class="comment"># sort sort.txt </span></span>
<span class="line">aaa:10:1.1</span>
<span class="line">bbb:20:2.2</span>
<span class="line">ccc:30:3.3</span>
<span class="line">ddd:40:4.4</span>
<span class="line">eee:50:5.5</span>
<span class="line">eee:50:5.5</span>
  • 忽略相同行使用-u选项或者uniq
<span class="line">1</span>
2
3
4
5
6
<span class="line">[root@ansheng ~]<span class="comment"># sort -u sort.txt </span></span>
<span class="line">aaa:10:1.1</span>
<span class="line">bbb:20:2.2</span>
<span class="line">ccc:30:3.3</span>
<span class="line">ddd:40:4.4</span>
<span class="line">eee:50:5.5</span>
  • sort的-n、-r、-k、-t选项的使用:
<span class="line">1</span>
2
3
4
5
6
7
<span class="line">[root@ansheng ~]<span class="comment"># sort -nk 2 -t: sort.txt </span></span>
<span class="line">aaa:10:1.1</span>
<span class="line">bbb:20:2.2</span>
<span class="line">ccc:30:3.3</span>
<span class="line">ddd:40:4.4</span>
<span class="line">eee:50:5.5</span>
<span class="line">eee:50:5.5</span>

-n是按照数字大小排序,-r是以相反顺序,-k是指定需要爱排序的栏位,-t指定栏位分隔符为冒号

uniq

概要: uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用。也就是说,为了使uniq起作用,所有的重复行必须是相邻的。

语法格式: uniq [OPTION]… [INPUT [OUTPUT]]

参数:

参数 说明
-i 忽略大小写字符的不同;
-c 进行计数
-u 只显示唯一的行
admin
版权声明:本站原创文章,由admin2016-07-12发表,共计17066字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)