Linux大数据开发基础:第二节:Linux文件系统、目录(一)

2024-06-20 18:32

本文主要是介绍Linux大数据开发基础:第二节:Linux文件系统、目录(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/lovehuangjiaju/article/details/47856591


本节主要内容

  1. 如何获取帮助文档
  2. Linux文件系统简介
  3. 目录操作
  4. 访问权限

1. 如何获取帮助文档

在实际工作过程当中,经常会忘记命令的使用方式,例如ls命令后面可以跟哪些参数,此时可以使用man命令来查看其使用方式,例如

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//man命令获取命令帮助手册</span>
xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:~$ man <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

这里写图片描述

可以使用键盘上的这里写图片描述 来显示下一行或上一行命令,也可以使用这里写图片描述 进行上一页或下一页(屏)命令的查看,另外这里写图片描述 空格鍵也可以用来显示下一屏的命令。想退出命令查看,直接按q鍵退出即可,也可以h鍵显示less命令列表(man命令通过less命令输出结果)

2. Linux文件系统简介

(一) 文件和目录

本节从使用者的角度来介绍Linux文件系统,Linux根据文件形式将文件分为目录和普通文件,如下图: 
这里写图片描述 
目录或文件的名称长度不超过255个字符,文件或目录名可由下列字符构成:

  • Uppercase letters (A–Z)
  • Lowercase letters (a–z)
  • Numbers (0–9)
  • Underscore ( _ )
  • Period(.)
  • Comma(,) 
    文件或目录名区分大小写,属于不同的文件或目录

(二) 文件扩展名与不可见文件名

与Window操作系统有很大不同的是,Linux文件对文件扩展名没有强制要求,例如假设编写了一个c语言源文件,你可以将其命名为complier.c,也可以是其它如complier、complier.ccc等文件名,但不推荐这么做,因为如果能将文件扩展名与特定的文件进行关联的话,有利于理解文件内容,目前约定成俗的linux文件扩展名如下表:

带扩展名的文件名 扩展名的含义
max.c C语言源文件
max.o 编码后的目标代码文件
max max.c对应的可执行文件
memo.txt 文本文件
memo.pdf pdf文件,必须在GUI界面上使用xpdf或kpdf才能查看
memo.ps PostScript文件,必须在GUI界面上使用ghostscript或kpdf才能查看
memo.z 经压缩程序压缩后的文件,可使用uncompress或gunzip解压
memo.gz 经gzip压缩程序压缩后的文件,可使用gunzip解压
memo.tar.gz或memo.tgz 经gzip压缩后的tar归档文件,可使用gunzip解压
memo.bz2 经bzip2压缩后的文件,可使用bunzip2解压
memo.html html文件,使用GUI环境的firefox查看
memo.jpg等 图像文件,使用GUI环境的照片查看器打开

在前一讲中我们看到,linux中还存在大量的隐藏文件,采用ls -a 命令可以显示,想定义隐藏文件,只要文件名或目录以.开始即可 
这里写图片描述

(三) 绝对路径与相对路径

在Linux中绝对路径与相对路径是一个很重要的概念,下图给出了什么是绝对路径 
这里写图片描述 
所有以根目录”/”作为开始的都是绝对路径,其它的均为相对路径

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绝对路径访问</span>
xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:~/Public$ cd /home/
xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home$ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>
xtwy
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相对路径访问</span>
xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home$ cd xtwy/</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

3. 目录操作

(一) 创建目录 mkdir

为演示方便,使用下列目录结构进行演示:

这里写图片描述

1 绝对路径创建方式

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用绝对路径创建</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir /home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>  xtwy
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

2 相对路径创建方式

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用相对路径进行创建</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/names
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/temp
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/literature
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# cd <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span># mkdir demo
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span># <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>
demo  literature  names  temp</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

有时不想层层目录创建,此时可以在mkdir 后面加上参数 -p(parents),将父子目录一起创建

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># mkdir -p literature/promo</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
demo  literature  names  temp
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd literature/</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max/literature</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
promo
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

(二) 更改目录 cd

工作目录与主目录的区别 
用户每次登录后的默认目录就是主目录,与系统会话期间保持不变,主目录用~表示

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/root</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>cd ~
xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>pwd
/home/xtwy
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

工作目录又称当前目录,cd命令执行完成后的目录就是工作目录,它是可以随意改变的。

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//.表示当前目录即工作目录</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//..表示当前目录的上一级目录</span>
xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:~$ cd .
xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:~$ cd ..
xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:/home$ 
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

(三) 删除目录 rmdir

rmdir是remove directory的简称,用于删除目录,它先删除目录下的所有文件,然后再删除该目录,但当目录下还有子目录时,该命令不能执行,需要使用rm命令,例如

<code class="hljs glsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//删除temp目录,先删除目录下的文件</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//再删除temp目录自身</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rmdir temp/</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rmdir literature/</span>
rmdir: failed to remove `literature/': Directory <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">not</span> empty
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rm -r literature/</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
demo  names</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

其中rm -r中的r指的是递归的删除目录及目录中的文件,因此它具有很强的破坏力,要谨慎使用。

(四) 移动目录 mv

<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>将目录demo移到<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy/</span>目录下
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/max# mv demo/</span> <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy/</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/max# cd /home/xtwy/</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
demo     Documents  examples.desktop  Pictures  Templates
Desktop  Downloads  Music             Public    Videos
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rmdir demo</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>原来目录的demo目录已经不存在了
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy# cd /home/max/</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/max<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
names</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

(五) 复制目录 cp

前面用mv命令移动目录,有时候需要对目录进行拷贝,使用方式如下:

<code class="hljs glsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//先创建一个演示目录,用-p,父目录如果不存在将会被创建</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mkdir -p literature/demo</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由于literature还包括子目录,此时拷贝不成功</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cp literature/ /home/xtwy/</span>
cp: omitting directory `literature/'
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果包括子目录的话,则加上-r参数,表示递归地拷贝</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cp -r literature/ /home/xtwy/</span>
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd /homt</span>
bash: cd: /homt: No such file or directory
root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd /home/xtwy/</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
Desktop    Downloads         literature  Pictures  Templates
Documents  examples.desktop  Music       Public    Videos
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd literature/</span>
root@ubuntu:/home/xtwy/literature<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
demo
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

4. 文件操作

(一) 创建文件

直接通过命令行的方式创建文件的方式有多种,常用方式如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">//通过echo命令,将输出的命令重定向到文件
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "hello linux" > hello.txt</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
Desktop    Downloads         hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>   Music     Public     Videos
Documents  examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span>  literature  Pictures  Templates
//touch命令,如何文件不存在,会创建文件
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># touch hell1.txt</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
Desktop    Downloads         hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  literature  Pictures  Templates
Documents  examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span>  hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  Music       Public    Videos
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

(二) 显示文件内容

cate命令可以显示文件内容,它的全称是catenate,意思是将单词一个接一个地连接起来

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat hello.txt </span>
hello linux
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

cat命令会将文件中所有的内容全部一次性显示出现,例如

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat /etc/profile</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># /etc/profile: system-wide .profile file for the Bourne shell (sh(1))</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -d /etc/profile.d ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> i <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /etc/profile.d/*.sh; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -r <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>. <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span>fidoneunset i......
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

有时候我们希望能够分屏查看文件内容,此时可以使用less或more分页程序,less和more的使用方式相差不大,通过空格键显示下一屏信息,它们之间的差别在于less在文件末尾会显示END消息,而more直接返回shell终端,例如: 
less命令 
这里写图片描述

more命令 
这里写图片描述

(三) cp命令复制文件

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
Desktop    Downloads         hell1.txt  literature  Pictures  Templates
Documents  examples.desktop  hello.txt  Music       Public    Videos
//复制文件
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cp hell1.txt literature/demo</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd literature/demo</span>
//cd -返回上一次执行的工作目录
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/literature/demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd -</span>
/home/xtwy
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

需要注意的是cp命令在复制时,如果目标目录中已存在该文件,系统不会给出警告,而是直接覆盖,因此它可能存在销毁文件的风险,为解决这个问题可以使用-i参数让系统给出警告,例如:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@ubuntu:/home/xtwy# cp -i hell1.txt literature/demo
cp: overwrite <span class="hljs-smartquote" style="box-sizing: border-box;">`literature/demo/hell1.txt'</span>? 
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

(三) mv命令移动或重命名文件

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">//在同一目录时,相当于文件重命名,执行完成后hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>不存在
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mv hell1.txt hell2.txt</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
Desktop    Downloads         hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  literature  Pictures  Templates
Documents  examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span>  hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  Music       Public    Videos
//移动hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>到literature/demo
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mv hell2.txt literature/demo</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd literature/demo/</span>
root@ubuntu:/home/xtwy/literature/demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
root@ubuntu:/home/xtwy/literature/demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd -</span>
/home/xtwy
//源目录hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>已不存在
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
Desktop    Downloads         hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>   Music     Public     Videos
Documents  examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span>  literature  Pictures  Templates
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

(四)显示文件头部或尾部

显示文件头部内容用head命令,尾部用tail命令,默认显示行数为10

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@ubuntu:/home/xtwy# head ~/.bashrc 
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ~/.bashrc: executed by bash(1) for non-login shells.</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># for examples</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># If not running interactively, don't do anything</span>
[ -z <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"$PS1"</span> ] && <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># don't put duplicate lines in the history. See bash(1) for more options</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ... or force ignoredups and ignorespace</span>
HISTCONTROL=ignoredups:ignorespace
root@ubuntu:/home/xtwy# tail ~/.bashrc
if [ -f ~/.bash_aliases ]; then. ~/.bash_aliases
fi<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># enable programmable completion features (you don't need to enable</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># this, if it's already enabled in /etc/bash.bashrc and /etc/profile</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># sources /etc/bash.bashrc).</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#    . /etc/bash_completion</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#fi</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>

head及tail的默认行数是可以修改的,例如:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//仅显示前两行</span>
root@ubuntu:/home/xtwy# head -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> ~/.bashrc 
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ~/.bashrc: executed by bash(1) for non-login shells.</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

tail命令在查看日志文件内容增长时可能经常会使用,例如在hadoop启动之后,会产生许多日志,但出现问题时,可以采用tail命令动态地监测日志文件内容的增长,查看问题出在哪个地方。

<code class="hljs ruleslanguage has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始显示情况</span>
root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># tail </span>-f hello.txt 
hello linux<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//向文件中追加内容</span>
root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># echo </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hello linux linux"</span> >> hello.txt<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//追加后的输出情况</span>
root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># tail </span>-f hello.txt 
hello linux
hello linux linux
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

(五)其它常见文件操作命令

下面的命令都不会改变文件内容

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# cp hello.txt hello1.txt
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span>
Desktop    Downloads         hello1.txt  literature  Pictures  Templates
Documents  examples.desktop  hello.txt   Music       Public    Videos
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//根据文件内容排序</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sort</span> hello1.txt
hello linux
hello linux linux
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//逆序输出</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sort</span> -r  hello1.txt
hello linux linux
hello linux
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//diff进行内容比较</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff hello1.txt hello.txt
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//向文件中追加内容</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hello linux linux"</span> >> hello.txt
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//内容比较</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff hello1.txt hello.txt
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>a3
> hello linux linux
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式化输出</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-u参数将文件分成多块</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//比较的两个文件分别用-、+表示</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//本例中 -表示hello1.txt,+表示hello.txt</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff -u hello1.txt hello.txt
--- hello1.txt  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">28</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44.071202558</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0700</span>
+++ hello.txt   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49.131181281</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0700</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//@@xxx@@用于标识行起始编号、行数</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-1,2表示 hello1.txt文件起始编号为1,行数为2</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//+1,3表示 hello.txt文件起始编号为1,行数为3</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@@</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@@</span>hello linuxhello linux linux
+hello linux linux</code>

这篇关于Linux大数据开发基础:第二节:Linux文件系统、目录(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078917

相关文章

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

Linux samba共享慢的原因及解决方案

《Linuxsamba共享慢的原因及解决方案》:本文主要介绍Linuxsamba共享慢的原因及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux samba共享慢原因及解决问题表现原因解决办法总结Linandroidux samba共享慢原因及解决

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T