1. 命令的剖析

Unix 的命令由2部分组成,命令本身和附加的参数。例如 ls 命令,如果直接执行 ls 命令,不带附加参数,那么默认执行的目标即为当前目录,如下

[root@localhost /]$lsbin   dev   etc   lib    media  opt   root  sbin  sys  usrboot  docs  home  lib64  mnt    proc  run   srv   tmp  var

我们可以添加命令参数,来使得它更加灵活, -l 参数可以使得结果集是以长结果显示更多信息,后面的路径可以指定命令执行的目标路径

[root@localhost /]$ls -l /roottotal 181328-rw-------. 1 root root      1264 Aug 15 19:36 anaconda-ks.cfgdrwxr-xr-x. 3 root root        18 Sep  1 22:12 backups-rw-r--r--. 1 root root      2381 Sep  1 21:35 baidu.html-rwxr-xr-x. 1 root root        10 Sep  1 05:57 cat.txtdrwxr-xr-x. 2 root root        25 Sep  1 21:43 docs-r--r--r--. 2 root root        17 Aug 18 12:18 file-r--r--r--. 2 root root        17 Aug 18 12:18 ha_link-rw-r--r--. 1 root root        49 Aug 18 12:14 hard_linkdrwxr-xr-x. 7   10  143       245 Aug 26 11:44 jdk1.8-rw-r--r--. 1 root root 185646832 Aug 26 11:30 jdk-8u181-linux-x64.tar.gz-rw-r--r--. 1 root root         0 Sep  1 22:18 log.file-rw-r--r--. 1 root root         0 Sep  1 23:36 log.fileat-rwxr--r--. 1 root root       114 Sep  2 06:53 purple.shlrwxrwxrwx. 1 root root         4 Aug 18 12:08 soft_link -> file-rw-r--r--. 1 root root      1326 Aug 22 12:01 test.txt
2. 查找命令相关的信息

如果你从未使用过 ls 命令,该如何学习它如何使用呢?除了通过搜索引擎学习,你还可以使用 man 来学习所有你想学习的指令。如下展示了 man ls 命令执行后的输出结果,由于篇幅过长,省略了后面部分,有兴趣的朋友自行尝试

[root@localhost /]$man ls

LS(1)                            User Commands                           LS(1)

NAME       ls - list directory contents

SYNOPSIS       ls [OPTION]... [FILE]...

DESCRIPTION       List  information  about  the FILEs (the current directory by default).       Sort entries alphabetically if none of -cftuvSUX nor --sort  is  speci‐       fied.

       Mandatory  arguments  to  long  options are mandatory for short options       too.

       -a, --all

GNU coreutils 8.22               November 2016                           LS(1) Manual page ls(1) line 219/251 (END) (press h for help or q to quit)

从输出中我们可以了解到该命令的主要功能和支持的所有参数。除了使用 man 来学习命令,我们还可以使用 info 来学习。例如命令行输入 info ls:

[root@localhost /]$info ls

File: coreutils.info,  Node: ls invocation,  Next: dir invocation,  Up: Directory listin\g

10.1 'ls': List directory contents==================================

The 'ls' program lists information about files (of any type, includingdirectories).  Options and file arguments can be intermixed arbitrarily,as usual.

   For non-option command-line arguments that are directories, bydefault 'ls' lists the contents of directories, not recursively, andomitting files with names beginning with '.'.  For other non-optionarguments, by default 'ls' lists just the file name.  If no non-optionargument is specified, 'ls' operates on the current directory, acting asif it had been invoked with a single argument of '.'.  ...

该操作同样会告诉你如何使用 ls 来高效化你的工作。


3. 修改命令

我们可以使用一些工具来增强命令的功能,例如元字符,输入输出重定向,管道,命令置换。


元字符

上面延时的 ls 命令输出了很多文件,但是假如我们只需要列出,文件名末尾是 link 结尾的文件,可以如何操作呢?我们可以使用参数 + 通配符来完成这个功能。

[root@localhost ~]$ls *linkha_link  hard_link  soft_link

* 代表匹配文件名中的一个或多个字符。

? 匹配文件名中的任何一个字符。

[] 匹配包含在 [] 符号内的某个字符即可。

[root@localhost ~]$ls *[link]baidu.html  ha_link  hard_link  soft_link

如上匹配除了结尾最后一个字符是 l,i,n,k 任何一个字符的所有文件。


输出,输出重定向

上述我们操作的命令执行结果都是直接输出到了控制台,但是假如我们的输出结果很长,或者暂时有其他事情要做,结果需要等稍后去分析该怎么办呢?这个时候我们可以将结果输出重定向到到一个文件中,保存起来,稍后查看。

[root@localhost ~]$ls *[link] > links.txt[root@localhost ~]$cat links.txtbaidu.htmlha_linkhard_linksoft_link

如上操作,我们便将文件列表的结果输出到了 links.txt 文件中,以便稍后调查问题。


管道

刚刚我们查看的是用户工作目录下的文件,输出很少,可以直接查看。但是当我们查询一个文件夹下面有很多文件的时候,输出结果很长,此时一个屏幕都无法展示结果,查看并不方便。有没有一种方法,可以让我们先查看一部分结果,查看完毕后,按下一个键继续查看下一页呢?也许聪明的你会想到 more 或者 less 命令。但是这俩个命令是来查看文件的,此时管道可以帮助你。管道可以使得输入输出重定向,将一个命令的输出作为另外一个命令的输入。

[root@localhost ~]$ls /etc | moreadjtimealiasesaliases.dbalternativesanacrontabasound.confat.denyaudispauditbash_completion.dbashrcbinfmt.dcentos-releasecentos-release-upstream--more--

如上,ls 的输出结果,作为了 more 的输入,这样我们就可以优哉游哉的慢慢查看 ls 的结果。当然这里只是用 ls 和 more 来举例,朋友们可以自己去探索其他的命令结合管道来使用,你会爱上它的。


命令置换

同样,命令置换也可以达到像管道一样的操作。它也将命令的输出结果作为另外一个命令输入。

[root@localhost ~]$ls ${pwd}anaconda-ks.cfg  cat.txt  ha_link    jdk-8u181-linux-x64.tar.gz  log.fileat  test.txtbackups          docs     hard_link  links.txt                   purple.shbaidu.html       file     jdk1.8     log.file                    soft_link

如上,我们将 pwd 命令的输出(当前工作目录),作为 ls 命令的输入。当然该命令也可以用管道来实现:pwd | ls,达到同样的效果:

[root@localhost ~]$pwd | lsanaconda-ks.cfg  cat.txt  ha_link    jdk-8u181-linux-x64.tar.gz  log.fileat  test.txtbackups          docs     hard_link  links.txt                   purple.shbaidu.html       file     jdk1.8     log.file                    soft_link

实现的同样的效果,但是实现原理不同。命令置换是通过在子 shell 中执行结果,然后将结果返回到主 shell 中。而管道则一直在主 shell 中执行。

—————END—————

喜欢本文的朋友们,欢迎长按下图订阅,收看更多精彩内容

转载于:https://blog.51cto.com/7779961/2169233

深入理解 Unix / Linux 命令相关推荐

  1. unix/linux命令“ls -l”选项输出结果详解

    from: http://hi.baidu.com/hoxily/item/12e2a02d03f77e0942634a8e unix/linux命令"ls -l"选项输出结果详解 ...

  2. unix/linux系统中文件分为哪些类型?,到底该如何理解 Unix/Linux 的文件系统?看这篇就知道了...

    原标题:到底该如何理解 Unix/Linux 的文件系统?看这篇就知道了 作者:舠

  3. linux调试-v-n区别,为你分享一些超好用的Unix/Linux 命令技巧

    今天小编要跟大家分享的文章是关于一些超好用的Unix/Linux 命令技巧.喜欢Linux系统或者正在从事Linux相关工作的小伙伴快来和小编一起学习一下吧! 1.如何删除一个大文件 当你想要删除一个 ...

  4. linux下启动tlq命令,UNIX/LINUX命令

    1 如何查看磁盘使用情况 查看磁盘空间 df -k 可以看出磁盘空间使用情况.以及分区或者逻辑卷的挂载目录等. $ df -k Filesystem 1024-blocks Free %Used Iu ...

  5. 后台执行UNIX/Linux命令和脚本的五种方法

    http://hankjin.blog.163.com/blog/static/337319372010111492348473/ 2010-12-14 09:35:48|  分类: Unix |   ...

  6. [译]后台执行UNIX/Linux命令和脚本的五种方法

    原文:http://www.thegeekstuff.com/2010/12/5-ways-to-execute-linux-command/ by SathiyaMoorthy on Decembe ...

  7. 理解Unix/Linux系统中的文件描述符

    简介 文件描述符是针对Unix/Linux的每个进程而言的,每个进程都维护了一个文件指针表,指针指向操作系统的文件.这里的文件是指的Unix/Linux系统所说的文件,Unix/Linux下一切皆文件 ...

  8. unix linux 命令参考,Unix/Linux 命令参考

    文件命 令 ls – 列出目录 ls -al – 使用格式化列出隐藏文件 cd dir - 更改目录到 dir cd – 更改到 home 目录 pwd – 显示当前目录 mkdir dir – 创建 ...

  9. linux命令行看图工具,六个鲜为人知的超酷Unix/Linux命令

    师徒对话:kibitz kibitz是一个终端工具,它主要用于让一个"师父(master)"来帮助他/她的"徒弟(apprentice)".从本质上来说,它是通 ...

最新文章

  1. Microsoft Hyper-V Server 2008 R2和SCVMM2012部署XenDesktop 5.6桌面虚拟化系列之二准备虚拟桌面模板...
  2. java singleton inner class_关于java:Singleton设计模式实现
  3. 华为P30系列新配色官宣:9月6日IFA2019上见!
  4. 2017-10-19 NOIP模拟赛
  5. docker 部署 zookeeper+kafka 集群
  6. 学生简单个人博客网页DW模板 简单HTML静态网页设计个人主页制作 大学生个人网站模板下载 网页作业个人主页制作
  7. redis下载安装教程
  8. ps+背景缩放+内容缩放
  9. psd原型图自动转html,psd自动转成html的研究
  10. 不怕被群主踢,安心分享小游戏续命,上分好办法!
  11. css 图片剪切object-fit属性
  12. NandFlash 控制器操作实例:读Flash
  13. Nginx 实现域名访问以及反向代理
  14. android仿钉钉日程日历,Flutter仿钉钉考勤日历的示例代码
  15. LeetCode——706,设计哈希映射
  16. mpvue开发微信小程序踩坑笔记
  17. linux 通过ssh上传文件
  18. 多元回归分析(multiple regression)及其应用
  19. odoo 12 : 附件(ir.attachment)——同一模型多个附件字段
  20. win7 如何设置快速启动栏

热门文章

  1. Linux内存page,【原创】(十四)Linux内存管理之page fault处理
  2. Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
  3. java调用outlook
  4. winCVS 使用方法
  5. spring batch
  6. [Vue 牛刀小试]:第八章 - 组件的基础知识
  7. 开发中关于Fragment异常的两个问题
  8. 《算法导论》中动态规划求解钢条切割问题
  9. linux 没有root登陆
  10. 第二次冲刺 站立会议5