前言:
前面學了了用戶和用戶組的管理,文件權限的管理以及查找文件的三種方式,接下來將學習怎樣配置環境變量,常見的壓縮格式與怎么解壓,管道,標準的輸入輸出以及重定向
echo命令:
linux的echo命令, 在shell編程中極為常用, 在終端下打印變量value的時候也是常常用到:
[plain]?view plain
?copy [user1@xianzan?dir]$?echo?"hello?world"?? hello?world?? [user1@xianzan?dir]$?echo?-n?"hello?world"?? hello?world[user1@xianzan?dir]$?echo?$PATH?? /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin??
此外。echo可以直接將你要打印的字符串寫到你的文件中:
[plain]?view plain
?copy [user1@xianzan?dir]$?echo?"hello?world">echotest.txt?? [user1@xianzan?dir]$?cat?echotest.txt??? hello?world??
export命令:
用于聲明一個環境變量:
[plain]?view plain
?copy [user1@xianzan?dir]$?export?PATH=$PATH:/usr/local/bin?? [user1@xianzan?dir]$?echo?$PATH?? /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/usr/local/bin?? 類似于 export ?變量 = 變量值 ? 的方式聲明一個環境變量只在本進程或者子進程器作用,關閉進程重新查看PATH變量的值:<img src="https://img-blog.csdn.net/20160114184427901?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border: none; max-width: 100%;" />
[plain]?view plain
?copy [xianzan@xianzan?dir]$?echo?$PATH?? /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin?? 說明:
如果在某次運行中,手動export一個變量,則在這次shell使用中,才能訪問這個變量,或者在該shell啟動的其他程序中,也可以訪問這個變量,因為它們是這個shell的子進程。
</pre><pre id="best-content-1144911805" class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px;">要想在linux啟動時,環境變量對這個系統的所有用戶都有效則修改 <span style="color: rgb(255, 0, 0);">/etc/profile</span>文件<div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 700.906px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/xiaofengwu123/article/details/50518084#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/xiaofengwu123/article/details/50518084#" class="CopyToClipboard" title="copy" target="_blank" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></span><div style="position: absolute; left: 390px; top: 1782px; width: 18px; height: 18px; z-index: 99;"></div><span class="tracking-ad" data-mod="popu_169"> </span></div></div><ol start="1" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">[xianzan@xianzan?dir]$?vim?/etc/profile??</span></span></li></ol></div>
環境變量生效:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?source?/etc/profile?? shell配置:
Linux系統配置文件有四個:
/etc/profile: 此文件為系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行.并從/etc/profile.d目錄的配置文件中搜集shell的設置;
/etc/bashrc: 為每一個運行bash shell的用戶執行此文件.當bash shell被打開時,該文件被讀取;
~/.bashrc: 該文件包含專用于你的bash shell的bash信息,當登錄時以及每次打開新的shell時,該文件被讀取;此文件類似于/etc/bashrc,不需要重啟生效,重新打開一個bash即可生效;
~/.bash_profile 是交互式、login 方式進入bash 運行的。
文件(夾)的壓縮:
說明:
經常遇到的壓縮格式為 .tar.gz,tar:只是打包,沒有壓縮后面gz為壓縮方式。
gzip .gz GNU壓縮工具,用Lempel-Ziv編碼;
bzip2 .bz2 采用Burrows-Wheeler塊排序文本壓縮算法和霍夫曼編碼;?
zip .zip Windows上PKZIP工具的Unix實現;?
tar命令:
tar function [options] object1 object2?
function參數定義了tar命令應該做什么
-A --concatenate 將一個已有tar歸檔文件追加到另一個已有tar歸檔文件;?
-c --create 創建一個新的tar歸檔文件;?
-d --diff 檢查歸檔文件和文件系統的不同之處;?
--delete 從已有tar歸檔文件中刪除;
-r --append 追加文件到已有tar歸檔文件末尾;?
-t --list 列出已有tar歸檔文件的內容;?
-u --update 將比tar歸檔文件中已有的同名文件新的文件追加到該tar歸檔文件中;?
-x --extract 從已有tar歸檔文件中提取文件;?
-C dir 切換到指定目錄;?
-f file 輸出結果到文件或設備file;?
-j 將輸出重定向給bzip2命令來壓縮;?
-p 保留所有文件權限;?
-v 在處理文件時顯示文件;?
-z 將輸出重定向給gzip命令來壓縮;
通常這些選項會組合在一起使用。
[plain]?view plain
?copy [xianzan@xianzan?dir]$?tar?-czvf?test.tar.gz?? [xianzan@xianzan?dir]$?tar?-xzvf?test.tar.gz??? ./?? ./test3/?? ./test1/?? ./test2/?? [xianzan@xianzan?dir]$?ls?? test1??test2??test3??test.tar.gz??
file命令:
查看文件類型:ASCII、data、bin等:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?file?/etc/passwd?? /etc/passwd:?ASCII?text?? [xianzan@xianzan?dir]$?file?/usr/sbin/atrun??? /usr/sbin/atrun:?POSIX?shell?script?text?executable?? [xianzan@xianzan?dir]$?file?/dev/fb0?? /dev/fb0:?character?special?? [xianzan@xianzan?dir]$?file?/dev/sda1?? /dev/sda1:?block?special?? od命令:
查看二進制文件,格式: od [ -t TYPE ] 文件名
a: 利用默認的字符來輸出
c: 使用ASCII輸出
d: 十進制輸出
f: 浮點數輸出
o: 八進制輸出
x: 十六機制輸出
[plain]?view plain
?copy [xianzan@xianzan?dir]$?od?-t?d?/bin/ls?? 0344420???????????1???????????0???????????0?????????0?? [xianzan@xianzan?dir]$?od?-t?o?/bin/ls?? 0344420?00000000001?00000000000?00000000000?00000000000?? 管道:
管道可以把一系列命令連接起來,這意味著第一個命令的輸出會作為第二個命令的輸入,通過管道傳給第二個命令,第二個命令的輸出又會作為第三個命令的輸入,以此類推。顯示在屏幕上的是管道行中最后一個命令的輸出。通過使用管道符“|”來建立一個管道行。
[plain]?view plain
?copy [xianzan@xianzan?dir]$?cat?/etc/passwd?|grep?xianzan?? xianzan:x:500:500:xianzan:/home/xianzan:/bin/bash?? [xianzan@xianzan?dir]$?cat?/etc/passwd?|grep?xianzan?|cut?-d:?-f1?? xianzan?? 說明:
cut是一個選取命令,就是將一段數據經過分析,取出我們想要的。一般來說,選取信息通常是針對“行”來進行分析的,并不是整篇信息分析的。
-d :自定義分隔符,默認為制表符,-d后面跟的就是截取的標志上面是以 :為截取的標志;
-f ?:與-d一起使用,指定顯示哪個區域,f1代表顯示第一個字段。
[plain]?view plain
?copy [xianzan@xianzan?dir]$?cat?/etc/passwd?|grep?xianzan?|cut?-d:?-f2?? x?? [xianzan@xianzan?dir]$?cat?/etc/passwd?|grep?xianzan?|cut?-d:?-f3?? 500?? 命令置換符:
命令置換是將一個命令的輸出作為另外一個命令的參數部分。常用命令格式為:command1 `command2`上面這條命令表示,命令command2的輸出將作為command1的參數,需要注意的是,命令置換符“`”為ESC鍵下方的“~”鍵不按shift的輸出。
[plain]?view plain
?copy [xianzan@xianzan?dir]$?ls?`pwd`?? 1.c?? [xianzan@xianzan?dir]$?ls?/home/`cat?/etc/passwd?|grep?xianzan?|cut?-d:?-f1`?? dir??公共的??模板??視頻??圖片??文檔??下載??音樂??桌面?? 標準輸入、輸出、出錯:
標準輸入<1>(Standard Input, stdin)是系統命令或程序獲取輸入的地方:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?cat?? hello?? hello?? 標準輸出<2>(Standard Output, stdout)是系統命令或程序正確執行信息的輸出地方:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?rm?-rf?*?? [xianzan@xianzan?dir]$?ls?? [xianzan@xianzan?dir]$?touch?hello.txt?? [xianzan@xianzan?dir]$?ls?? hello.txt?? 而標準出錯<2>(Standard error, stderr)是系統命令或程序執行出錯信息的輸出地方:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?ls?ls?? ls:?無法訪問ls:?沒有那個文件或目錄?? 說明:
Linux系統中默認的標準輸入為鍵盤,標準輸出和標準出錯為終端屏幕。
輸入、輸出重定向、輸出追加重定向符:
輸入重定向符 < :
[plain]?view plain
?copy [xianzan@xianzan?dir]$?wc?<?/etc/passwd?? ??39?????67?????1883?? 行數??單詞數???字節數?? 輸出重定向>:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?tail?/etc/passwd?>?stdout.txt?? [xianzan@xianzan?dir]$?tail?/etc/passwd?>?stdout.txt?? [xianzan@xianzan?dir]$?cat?stdout.txt??? gdm:x:42:42::/var/lib/gdm:/sbin/nologin?? ntp:x:38:38::/etc/ntp:/sbin/nologin?? apache:x:48:48:Apache:/var/www:/sbin/nologin?? 輸出追加重定向>>:
[plain]?view plain
?copy [xianzan@xianzan?dir]$?tail?/etc/passwd?>>?stdout.txt?? [xianzan@xianzan?dir]$?cat?stdout.txt??? gdm:x:42:42::/var/lib/gdm:/sbin/nologin?? ntp:x:38:38::/etc/ntp:/sbin/nologin?? apache:x:48:48:Apache:/var/www:/sbin/nologin?? gdm:x:42:42::/var/lib/gdm:/sbin/nologin?? ntp:x:38:38::/etc/ntp:/sbin/nologin?? apache:x:48:48:Apache:/var/www:/sbin/nologin??
總結
以上是生活随笔為你收集整理的linux文件操作学习3的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。