Linux文件操作高頻使用命令

<article class="baidu_pl">
        <!--python安裝手冊開始-->
                <!--python安裝手冊結束-->
                 <div id="article_content" class="article_content clearfix">
                                            <div class="article-copyright">
                <span class="creativecommons">
                    <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
                    </a>
                    <span>
                        版權聲明:本文爲博主原創文章,遵循<a href="http://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener"> CC 4.0 BY </a>版權協議,轉載請附上原文出處鏈接和本聲明。                    </span>
                    <div class="article-source-link2222">
                        本文鏈接:<a href="https://blog.csdn.net/gexiaoyizhimei/article/details/100122368">https://blog.csdn.net/gexiaoyizhimei/article/details/100122368</a>
                    </div>
                </span>
                    
                </div>
                                                    <!--一個博主專欄付費入口-->
             
             <!--一個博主專欄付費入口結束-->
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-4a3473df85.css">
                                        <div id="content_views" class="markdown_views prism-kimbie-light">
                    <!-- flowchart 箭頭圖標 勿刪 -->
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                                            <p></p><div class="toc"><h3>文章目錄</h3><ul><ul><ul><ul><li><a href="#0_1" rel="nofollow" target="_self">0.新建操作:</a></li><li><a href="#1_8" rel="nofollow" target="_self">1.查看操作</a></li><li><a href="#2_82" rel="nofollow" target="_self">2.刪除操作</a></li><li><a href="#3_89" rel="nofollow" target="_self">3.複製操作</a></li><li><a href="#4_136" rel="nofollow" target="_self">4.移動操作:</a></li><li><a href="#5_148" rel="nofollow" target="_self">5.重命名操作:</a></li><li><a href="#6_163" rel="nofollow" target="_self">6.解壓壓縮操作</a></li><li><a href="#7_235" rel="nofollow" target="_self">7.上傳文件工具</a></li><li><a href="#8lnfiletouch_244" rel="nofollow" target="_self">8.ln、file和touch命令</a></li><li><a href="#9_339" rel="nofollow" target="_self">9.查找操作命令:</a></li></ul></ul></ul></ul></div><p></p>
<h4><a id="0_1"></a>0.新建操作:</h4>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">mkdir</span> abc <span class="token comment">#新建一個文件夾</span>
<span class="token function">touch</span> abc.sh <span class="token comment">#新建一個文件</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<h4><a id="1_8"></a>1.查看操作</h4>
<ul>
<li>查看目錄:</li>
</ul>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ll <span class="token comment">#顯示目錄文件詳細信息</span>
<span class="token function">du</span> -h 文件/目錄  <span class="token comment">#查看大小</span>
<span class="token function">pwd</span> <span class="token comment">#顯示路徑</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<ul>
<li>
<p>查看文件內容:</p>
<p>cat|head|tail命令</p>
</li>
</ul>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">cat</span> abc.txt <span class="token comment">#查看abc的內容</span>
<span class="token function">head</span> -5 abc.txt <span class="token comment">#查看abc前5行內容。默認是10行</span>

<span class="token function">tail</span> <span class="token punctuation">[</span>選項<span class="token punctuation">]</span> 文件名
各選項的含義如下:
+num:從第num行以後開始顯示
-num:從距文件尾num行處開始顯示。如果省略num參數,系統默認值爲10.
-f: 循環讀取,例如查看服務器日誌時,可以實時觀察
<span class="token comment">#filename 文件裏的最尾部的內容顯示在屏幕上,並且不斷刷新。</span>
<span class="token function">tail</span> -f filename 
<span class="token comment">#查看最後20行</span>
<span class="token function">tail</span> -f filename



<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre>
<p>​		more命令:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">more命令一次顯示一屏信息,若信息未顯示完屏幕底部將出現“-More-(xx%)”。

此時按Space鍵,可顯示下一屏內容;

按“回車”鍵,顯示下一行內容;

按B鍵,顯示上一屏;

按Q鍵,可退出more命令。
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>​		less命令:和more命令類似,但是比more命令更強大。在很多時候,必須使用less,比如管道。例如:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ll /etc <span class="token operator">|</span> <span class="token function">less</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>stat 命令:<br>
查看文件的詳細信息,比如創建修改時間,大小等</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token punctuation">[</span>root@localhost zx<span class="token punctuation">]</span><span class="token comment"># stat index.html </span>
  文件:<span class="token string">"index.html"</span>
  大小:29006     	塊:64         IO 塊:4096   普通文件
設備:fd00h/64768d	Inode:17589607    硬鏈接:1
權限:<span class="token punctuation">(</span>0644/-rw-r--r--<span class="token punctuation">)</span>  Uid:<span class="token punctuation">(</span>    0/    root<span class="token punctuation">)</span>   Gid:<span class="token punctuation">(</span>    0/    root<span class="token punctuation">)</span>
環境:unconfined_u:object_r:home_root_t:s0
最近訪問:2019-09-02 21:47:41.824053666 +0800
最近更改:2019-09-02 21:44:33.588587500 +0800
最近改動:2019-09-02 21:44:33.588587500 +0800
創建時間:-

<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre>
<p>du 命令:<br>
選項:-h  以合適的單位顯示(會根據文件的大小自動選擇kb或M等單位)</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token punctuation">[</span>root@localhost zx<span class="token punctuation">]</span><span class="token comment"># du -h index.html </span>
32K	index.html
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<h4><a id="2_82"></a>2.刪除操作</h4>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">rm</span> -f  aa.txt <span class="token comment">#強制刪除aa.txt</span>
<span class="token function">rm</span> -rf fileDir <span class="token comment">#強制刪除fileDir文件夾和裏邊的所有文件</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<h4><a id="3_89"></a>3.複製操作</h4>
<ul>
<li>同一機器的複製:</li>
</ul>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">cp:複製文件或目錄
語法:
<span class="token function">cp</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token function">source</span> dest

-a:此選項通常在複製目錄時使用,它保留鏈接、文件屬性,並複製目錄下的所有內容。其作用等於dpR參數組合。
-d:複製時保留鏈接。這裏所說的鏈接相當於Windows系統中的快捷方式。
-f:覆蓋已經存在的目標文件而不給出提示。
-i:與-f選項相反,在覆蓋目標文件之前給出提示,要求用戶確認是否覆蓋,回答<span class="token string">"y"</span>時目標文件將被覆蓋。
-p:除複製文件的內容外,還把修改時間和訪問權限也複製到新文件中。
-r:若給出的源文件是一個目錄文件,此時將複製該目錄下所有的子目錄和文件。
-l:不復制文件,只是生成鏈接文件。

舉例:
<span class="token comment">#將../html/index.html 複製到當前目錄</span>
<span class="token function">cp</span> <span class="token punctuation">..</span>/html/index.html <span class="token keyword">.</span>    
<span class="token comment">#將../html/ 目錄下的文件及子目錄複製到當前的tt目錄下,如果tt不存在,會自動創建</span>
<span class="token function">cp</span> -r <span class="token punctuation">..</span>/html/  tt/       
<span class="token comment">#將文件file複製到目錄/usr/men/tmp下,並改名爲file1</span>
<span class="token function">cp</span> <span class="token function">file</span> /usr/men/tmp/file1  
    
<span class="token comment">#如果dir2目錄已存在,則需要使用</span>
<span class="token function">cp</span> -r dir1/. dir2
<span class="token comment">#如果這時使用cp -r dir1 dir2,則也會將dir1目錄複製到dir2中,明顯不符合要求。</span>
ps:dir1、dir2改成對應的目錄路徑即可。
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li></ul></pre>
<ul>
<li>遠程複製</li>
</ul>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#將當前目錄下的test.txt複製到遠程111.12機器的/zx目錄下</span>
<span class="token function">scp</span> test.txt [email protected]:/zx

<span class="token comment">#將test.txt複製到遠程用戶的根目錄,並命名爲textA.txt</span>
<span class="token function">scp</span> test.txt [email protected]:testA.txt

<span class="token comment">#也可以不指定用戶,在後續提示中再輸入,如下:</span>
<span class="token function">scp</span> test.txt 192.168.111.12:/zx

<span class="token comment">#從遠程複製到本地: -r用於遞歸整個目錄</span>
<span class="token function">scp</span> -r remote_user@remote_ip:remote_folder local_path
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre>
<h4><a id="4_136"></a>4.移動操作:</h4>
<p>移動操作可以理解成複製文件後,刪除原文件。</p>
<p>eg1:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">mv</span> /zx/soft/* <span class="token keyword">.</span> <span class="token comment">#複製/zx/soft目錄中的所有文件到當前目錄</span>
<span class="token function">mv</span> a.txt ./test/a.txt  <span class="token comment">#複製當前目錄a.txt到當前的test目錄下。</span>
<span class="token function">mv</span> /zx/soft/  /tmp/soft  <span class="token comment">#複製文件夾到/tmp/下,必須保證tmp是存在的文件夾</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<h4><a id="5_148"></a>5.重命名操作:</h4>
<p>重命名還是用的移動操作命令,比如:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#將目錄(文件)A重命名爲B</span>
<span class="token function">mv</span> A B
<span class="token comment">#將/a目錄(文件)移動到/b下,並重命名爲c。要保證b目錄存在。</span>
<span class="token function">mv</span> /a /b/c
<span class="token comment">#將當前test1目錄移動到當前的test目錄並命名爲b</span>
<span class="token function">mv</span> ./test1 ./test/b 
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<h4><a id="6_163"></a>6.解壓壓縮操作</h4>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">tar</span>
-c: 建立壓縮檔案
-x:解壓
-t:查看內容
-r:向壓縮歸檔文件末尾追加文件
-u:更新原壓縮包中的文件
這五個是獨立的命令,壓縮解壓都要用到其中一個,可以和別的命令連用但只能用其中一個。下面的參數是根據需要在壓縮或解壓檔案時可選的。

-z:有gzip屬性的
-j:有bz2屬性的
-Z:有compress屬性的
-v:顯示所有過程
-O:將文件解開到標準輸出

下面的參數-f是必須的
-f: 使用檔案名字,切記,這個參數是最後一個參數,後面只能接檔案名。

三、舉例說明:
<span class="token function">tar</span> -cf all.tar *.jpg
這條命令是將所有.jpg的文件打成一個名爲all.tar的包。-c是表示產生新的包,-f指定包的文件名。
<span class="token function">tar</span> -tf all.tar
這條命令是列出all.tar包中所有文件,-t是列出文件的意思
<span class="token function">tar</span> -xf all.tar
這條命令是解出all.tar包中所有文件,-x是解開的意思
壓縮
<span class="token function">tar</span> –cvf jpg.tar *.jpg //將目錄裏所有jpg文件打包成jpg.tar
eg2:
    <span class="token function">tar</span> -xzf nginx-1.14.0.tar.gz //解壓到當前目錄
    <span class="token function">tar</span> -zxf nginx-1.14.0.tar.gz -C /usr/local/nginx  <span class="token comment">#解壓到對應目錄</span>
 eg3:
 	<span class="token function">tar</span> -zxvf nginx<span class="token punctuation">..</span>.tar.gz <span class="token comment">#解壓並顯示過程</span>
 	
注意:有些壓縮程序提示命令找不到,需要進行安裝,例如:
yum <span class="token function">install</span> unzip
或在ubuntu上:
<span class="token function">apt-get</span> <span class="token function">install</span> unzip

四、總結
1、*.tar 用 <span class="token function">tar</span> –xvf 解壓
2、*.gz 用 <span class="token function">gzip</span> -d或者gunzip 解壓
3、*.tar.gz和*.tgz 用 <span class="token function">tar</span> –xzf 解壓
4、*.bz2 用 <span class="token function">bzip2</span> -d或者用bunzip2 解壓
5、*.tar.bz2用tar –xjf 解壓
6、*.Z 用 uncompress 解壓
7、*.tar.Z 用tar –xZf 解壓
8、*.rar 用 <span class="token function">unrar</span> e解壓
9、*.zip 用 unzip 解壓  

解壓的時候,有時候不想覆蓋已經存在的文件,那麼可以加上-n參數
unzip -n test.zip
unzip -n -d /temp test.zip
只看一下zip壓縮包中包含哪些文件,不進行解壓縮
unzip -l test.zip
查看顯示的文件列表還包含壓縮比率
unzip -v test.zip
檢查zip文件是否損壞
unzip -t test.zip
如果已有相同的文件存在,要求unzip命令覆蓋原先的文件
unzip -o test.zip -d /tmp/
示例:
 eg1:  unzip mydata.zip -d mydatabak <span class="token comment">#解壓到mydatabak目錄</span>
 
10. xz
	這是兩層壓縮,外面是xz壓縮方式,裏層是tar壓縮,所以可以分兩步實現解壓
	$ xz -d node-v6.10.1-linux-x64.tar.xz
    $ <span class="token function">tar</span> -xvf node-v6.10.1-linux-x64.tar
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li></ul></pre>
<h4><a id="7_235"></a>7.上傳文件工具</h4>
<p>從本地windows上傳一些文件到遠程Linux服務器可以通過xshell的xftp也可以通過下面這個小工具lrzsz,使用更加方便。</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">yum <span class="token function">install</span> lrzsz <span class="token comment">#安裝工具</span>

常用命令:
sz dist.zip <span class="token comment">#下載文件dist.zip到本地</span>
rz <span class="token comment">#會打開窗口,上傳文件到遠程服務器</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<h4><a id="8lnfiletouch_244"></a>8.ln、file和touch命令</h4>
<ul>
<li>
<p><strong>ln命令</strong>:名用於創建鏈接文件,包括硬鏈接(Hard Link)和符號鏈接(Symbolic Link) 。我們常用的是符號鏈接,也稱軟連接。軟連接就類似windows裏的快捷方式。</p>
<p>示例:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#在當前目錄創建一個軟連接,指向/etc/fastab,名稱也是fastab</span>
<span class="token function">ln</span> -s /etc/fastab

<span class="token comment">#在當前目錄創建一個指向/boot/grub的軟連接,命名爲gb</span>
<span class="token function">ln</span> -s /boot/grub gb
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre>
<p>注意:刪除軟連接 正確方式是:</p>
<pre class="prettyprint"><code class="prism language-shll has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">rm -rf ./gb
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>錯誤方式:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">rm</span> -rf ./gb/
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>這樣會刪除了原有grub下的內容。特別是針對系統文件的軟連接,刪除一定要慎重。</p>
</li>
<li>
<p><strong>file命令</strong> 用於識別文件的類型</p>
</li>
</ul>
<p>Linux中文件後綴只是方便使用者識別,沒有實質的約束作用。file命令可以查看文件的實質類型:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">file</span> <span class="token punctuation">[</span>-bcLz<span class="token punctuation">]</span> 文件<span class="token operator">|</span>目錄
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>選項說明:</p>
<ul>
<li>文件|目錄:需要識別的文件或目錄</li>
<li>-b: 顯示識別結果時,不顯示文件名</li>
<li>-c: 顯示執行過程</li>
<li>-L: 直接顯示符號鏈接文件指向的文件類型</li>
<li>-z: 嘗試去解讀壓縮文件的內容</li>
</ul>
<p>示例: 可以看出,index.mp4本質是一個HTML而非一個mp4文件</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token punctuation">[</span>root@VM_0_13_centos soft<span class="token punctuation">]</span><span class="token comment"># file index.mp4 </span>
index.mp4: HTML document, UTF-8 Unicode text, with very long lines
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<ul>
<li>
<p><strong>touch命令:</strong> 用於改變文件或目錄的訪問時間和修改時間。</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">touch</span> <span class="token punctuation">[</span>-am<span class="token punctuation">]</span> <span class="token punctuation">[</span>-t<span class="token operator">&lt;</span>日期時間<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>目錄<span class="token operator">|</span>文件<span class="token punctuation">]</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>如果指定目錄文件不存在,則會直接創建一個空文件,所以touch也常用來創建一個空白文件</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#創建一個新文件aa.txt</span>
<span class="token function">touch</span> aa.txt
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<p>選項說明:</p>
<ul>
<li>-a:  只修改訪問時間</li>
<li>-m : 只修改 修改時間</li>
<li>-t : 使用指定日期時間,而非系統時間 。例如要修改爲2019年10月20日16:38分13秒。參數就是:‘20191020163813’</li>
</ul>
<p>示例:</p>
<p>修改之前可以先查看文件的時間戳: 用stat 命令查看</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token punctuation">[</span>root@VM_0_13_centos soft<span class="token punctuation">]</span><span class="token comment"># stat index.html </span>
  File: ‘index.html’
  Size: 17215     	Blocks: 40         IO Block: 4096   regular <span class="token function">file</span>
Device: fd01h/64769d	Inode: 529352      Links: 1
Access: <span class="token punctuation">(</span>0644/-rw-r--r--<span class="token punctuation">)</span>  Uid: <span class="token punctuation">(</span>    0/    root<span class="token punctuation">)</span>   Gid: <span class="token punctuation">(</span>    0/    root<span class="token punctuation">)</span>
Access: 2019-10-23 15:15:37.280616254 +0800
Modify: 2019-10-23 15:15:37.280616254 +0800
Change: 2019-10-23 15:15:37.290616257 +0800
 Birth: -

<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>
<p>開始修改:將index.html文件的訪問和修改時間修改成當前系統的時間。</p>
<pre class="prettyprint"><code class="has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">touch index.html
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
</li>
</ul>
<h4><a id="9_339"></a>9.查找操作命令:</h4>
<p>對於要用到的文件,目錄等,經常有忘記的時候,所以查找命令就顯得極爲必要:</p>
<ul>
<li>
<p>find: 查找文件或目錄  (<strong>常用</strong>)</p>
<p>語法如下:</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">find</span> <span class="token punctuation">[</span>目錄…<span class="token punctuation">]</span> <span class="token punctuation">[</span>-amin <span class="token operator">&lt;</span>分鐘<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-atime <span class="token operator">&lt;</span>24小時數<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-cmin <span class="token operator">&lt;</span>分鐘<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-ctime<span class="token operator">&lt;</span>24小時數<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">[</span>-empty<span class="token punctuation">]</span><span class="token punctuation">[</span>-exec<span class="token operator">&lt;</span>執行命令<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">[</span>-fls<span class="token operator">&lt;</span>列表文件<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">[</span>-follow<span class="token punctuation">]</span>
    <span class="token punctuation">[</span>-fstype <span class="token operator">&lt;</span>系統文件類型<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-gid <span class="token operator">&lt;</span>組編號<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-group <span class="token operator">&lt;</span>組名稱<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-nogroup<span class="token punctuation">]</span> <span class="token punctuation">[</span>-mmin <span class="token operator">&lt;</span>分鐘<span class="token operator">&gt;</span><span class="token punctuation">]</span>  <span class="token punctuation">[</span>-mtime
    <span class="token operator">&lt;</span>24小時數<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-name <span class="token operator">&lt;</span>查找內容<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-nogroup<span class="token punctuation">]</span> <span class="token punctuation">[</span>-nouser<span class="token punctuation">]</span> <span class="token punctuation">[</span>-perm <span class="token operator">&lt;</span>權限數值<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-size <span class="token operator">&lt;</span>文件大小<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-uid
    <span class="token operator">&lt;</span>用戶編號<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-user <span class="token operator">&lt;</span>用戶名稱<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-nouser<span class="token punctuation">]</span>


<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<p><strong>幾個常用選項說明:</strong></p>
<ul>
<li>-size &lt;文件大小&gt;:查找符合指定大小的文件。文件大小單位可以是“c”表示Byte;“k”表示KB。如配置爲“100k”,find命令會查找文件大小正好100KB的文件;配置爲“+100k”,find命令會查找文件大小大於100KB的文件;配置爲“-100k”,find命令會查找文件大小小於100KB的文件。</li>
<li>-user&lt;用戶名稱&gt;:查找所有者是指定用戶的文件或目錄,也能以用戶編號指定</li>
<li>-name &lt;查找內容&gt;:查找指定的內容,在查找內容中使用“*”  表示任意個字符;使用“?”表示任何一個字符</li>
<li>-mtime &lt;24小時數&gt;:查找在指定時間<strong>曾更改過內容</strong>的文件或目錄,單位以24小時計算。如配置爲2,find命令會查找剛好在48小時之前更改過內容的文件;配置爲+2,find命令會查找超過在48小時之前更改過內容的文件;配置爲-2,find命令會查找在48小時之內更改過內容的文件。</li>
<li>-mmin &lt;分鐘&gt;:查找在指定時間曾被<strong>更改過內容</strong>的文件或目錄,單位以分鐘計算。</li>
<li>cmin &lt;分鐘&gt;:查找在指定時間曾被<strong>更改過權限</strong>屬性的文件或目錄,單位以分鐘計算。-ctime對應小時。</li>
<li>-amin &lt;分鐘&gt;:查找的是指定時間<strong>訪問過</strong>的文件或目錄。-atim對應小時。</li>
<li>-perm &lt;權限數值&gt;:查找符合指定權限數值(有關權限數值見第6章)的文件或目錄。如配置爲“0700”,find命令會查找權限數值正好是“0700”的文件或目錄;配置爲“+0700”,find命令會查找權限數值大於 “0700”的文件或目錄;配置爲“-0700”,find</li>
<li></li>
</ul>
<p>選項大概有以下幾類:</p>
<p>1.按時間範圍查找</p>
<p>2.按文件大小查找</p>
<p>3.按文件名稱查找</p>
<p>4.按其他:比如權限、用戶組、類型等</p>
<p><strong>示例:</strong></p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#從根目開始,查找名稱以nginx開頭的目錄和文件</span>
<span class="token function">find</span> / -name nginx*  

<span class="token comment">#查找文件大小超過100M的文件</span>
<span class="token function">find</span> / -size +100M

<span class="token comment">#查找/home/zx目錄下,10分鐘內被修改過的文件和目錄</span>
<span class="token function">find</span> /home/zx/  -mmin -10
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>
</li>
<li>
<p>locate: 查找文件或目錄(<strong>不常用</strong>)</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">locate</span> 查找內容
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>例如:locate   nginx  會將所有包含nginx的目錄和文件都列出來。可以用* 或?等匹配符。</p>
<p>locate的查找速度非常快,因爲該命令查找的是數據庫,所以有些剛修改的文件和目錄,可能無法找到。可以採用:updatedb  命令更新數據庫。</p>
</li>
<li>
<p>which: 查找文件(<strong>不常用</strong>)</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token function">which</span> <span class="token punctuation">[</span>文件<span class="token punctuation">]</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>which命令只會在PATH環境變量定義的路徑及命令別名中查找,所以範圍有限。</p>
</li>
<li>
<p>whereis : 查找文件(<strong>不常用</strong>)</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">whichis <span class="token punctuation">[</span>-bu<span class="token punctuation">]</span> <span class="token punctuation">[</span>-B<span class="token operator">&lt;</span>目錄<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-M<span class="token operator">&lt;</span>目錄<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>-S<span class="token operator">&lt;</span>目錄<span class="token operator">&gt;</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>文件<span class="token punctuation">]</span>
<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>常用選項:</p>
<p>文件:要查找的命令</p>
<p>-b: 只查找二進制文件</p>
<p>-u: 查找不包含指定類型的文件</p>
<p>-B&lt;目錄&gt;: 只在指定目錄下查找二進制文件</p>
<p>-M&lt;目錄&gt;:只在指定目錄查找幫助文件</p>
<p>-S&lt;目錄&gt;:只在指定目錄查找源碼目錄</p>
<p>例如: 默認只會在指定目錄查找(/bin ,/etc  ,/usr)</p>
<pre class="prettyprint"><code class="prism language-shell has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token punctuation">[</span>root@VM_0_13_centos soft<span class="token punctuation">]</span><span class="token comment"># whereis nginx</span>
nginx: /usr/local/nginx /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
</li>
</ul>

                                    </div>
                <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                    </div>
    </article>

 

發佈了91 篇原創文章 · 獲贊 52 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章