自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux基礎(chǔ)命令,用戶管理和文件系統(tǒng)總結(jié)

系統(tǒng) Linux
本篇給大家介紹Linux基礎(chǔ)命令,用戶管理和文件系統(tǒng)總結(jié)。希望對你有所幫助。

 [[353010]]

 基礎(chǔ)命令

Linux系統(tǒng)的命令通常都是如下所示的格式:

  1. 命令名稱 [命名參數(shù)] [命令對象] 

獲取登錄信息

獲取登錄信息 - 「w」 / 「who」 / 「last」/ 「lastb」。

  1. maoli@ubuntu:~$ w 
  2. 08:07:38 up 2 min,  1 user,  load average: 0.83, 0.64, 0.27 
  3. USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT 
  4. maoli    tty7     :0               08:06    2:38   2.29s  0.31s /sbin/upstart - 
  5. maoli@ubuntu:~$ who 
  6. maoli    tty7         2020-05-28 08:06 (:0) 
  7. maoli@ubuntu:~$ last 
  8. maoli    tty7         :0               Thu May 28 08:06    gone - no logout 
  9. reboot   system boot  4.15.0-99-generi Thu May 28 08:04   still running 
  10. maoli    tty7         :0               Fri May  1 15:20 - down   (01:26) 
  11. reboot   system boot  4.15.0-99-generi Fri May  1 15:20 - 16:47  (01:26) 
  12. maoli    tty7         :0               Fri May  1 10:12 - crash  (05:08) 
  13. wtmp begins Fri May  1 10:12:22 2020 
  14. maoli@ubuntu:~$ sudo lastb 
  15. [sudo] maoli 的密碼:  
  16.  
  17. btmp begins Fri May  1 15:20:29 2020 

查看自己使用的Shell

查看自己使用的Shell - 「ps」。

Shell也被稱為“殼”或“殼程序”,它是用戶與操作系統(tǒng)內(nèi)核交流的翻譯官,簡單的說就是人與計(jì)算機(jī)交互的界面和接口。目前很多Linux系統(tǒng)默認(rèn)的Shell都是bash(Bourne Again SHell),因?yàn)樗梢允褂胻ab鍵進(jìn)行命令和路徑補(bǔ)全、可以保存歷史命令、可以方便的配置環(huán)境變量以及執(zhí)行批處理操作。

  1. maoli@ubuntu:~$ ps 
  2.   PID TTY          TIME CMD 
  3.  3621 pts/1    00:00:00 bash 
  4.  5082 pts/1    00:00:00 ps 

查看命令的說明和位置

查看命令的說明和位置 - 「whatis」 / 「which」 / 「whereis」。

  1. maoli@ubuntu:~$ whatis ps 
  2. ps (1)               - report a snapshot of the current processes. 
  3. maoli@ubuntu:~$ whatis python 
  4. python (1)           - an interpreted, interactive, object-oriented programmi... 
  5. maoli@ubuntu:~$ whereis ps 
  6. ps: /bin/ps /usr/share/man/man1/ps.1.gz 
  7. maoli@ubuntu:~$ whereis python 
  8. python: /usr/bin/python3.5m-config /usr/bin/python /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python2.7 /usr/bin/python3.5-config /usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz 
  9. maoli@ubuntu:~$ which ps 
  10. /bin/ps 
  11. maoli@ubuntu:~$ which python 
  12. /usr/bin/python 

清除屏幕上顯示的內(nèi)容

清除屏幕上顯示的內(nèi)容 - 「clear」。

查看幫助文檔查看幫助文檔 - 「man」 / 「info」 / 「help」 / 「apropos」。

  1. maoli@ubuntu:~$ ps --help 
  2.  
  3. Usage: 
  4. ps [options] 
  5.  
  6. Try 'ps --help <simple|list|output|threads|misc|all>' 
  7.  or 'ps --help <s|l|o|t|m|a>' 
  8. for additional help text. 
  9.  
  10. For more details see ps(1). 
  11. maoli@ubuntu:~$ man ps 
  12.  
  13. PS(1)                                User Commands                                PS(1) 
  14. NAME 
  15.        ps - report a snapshot of the current processes. 
  16. SYNOPSIS 
  17.        ps [options] 
  18. DESCRIPTION 
  19. ... 

查看系統(tǒng)和主機(jī)名

查看系統(tǒng)和主機(jī)名 - 「uname」 / 「hostname」。

  1. maoli@ubuntu:~$ uname 
  2. Linux 
  3. maoli@ubuntu:~$ hostname 
  4. ubuntu 

時間和日期

時間和日期 - 「date」 / 「cal」。

  1. maoli@ubuntu:~$ date 
  2. 2020年 05月 28日 星期四 08:13:25 CST 
  3. maoli@ubuntu:~$ cal 
  4.      五月 2020          
  5. 日 一 二 三 四 五 六   
  6.                1  2   
  7. 3  4  5  6  7  8  9   
  8. 10 11 12 13 14 15 16   
  9. 17 18 19 20 21 22 23   
  10. 24 25 26 27 28 29 30   
  11. 31                     
  12. maoli@ubuntu:~$ cal 5 2020 
  13.      五月 2020          
  14. 日 一 二 三 四 五 六   
  15.                1  2   
  16. 3  4  5  6  7  8  9   
  17. 10 11 12 13 14 15 16   
  18. 17 18 19 20 21 22 23   
  19. 24 25 26 27 28 29 30   
  20. 31         

重啟和關(guān)機(jī)

重啟和關(guān)機(jī) - 「reboot」 / 「shutdown」。

  1. maoli@ubuntu:~$  shutdown -h +5   #五分鐘關(guān)機(jī) 
  2. Shutdown scheduled for 四 2020-05-28 08:19:24 CST, use 'shutdown -c' to cancel. [root ~]#  
  3. maoli@ubuntu:~$ shutdown -c 
  4. maoli@ubuntu:~$ shutdown -r 12:00 
  5. Shutdown scheduled for 四 2020-05-28 12:00:00 CST, use 'shutdown -c' to cancel 
  6. maoli@ubuntu:~$ shutdown -c 

說明:在執(zhí)行shutdown命令時會向登錄系統(tǒng)的用戶發(fā)出警告,可以在命令后面跟上警告消息來替換默認(rèn)的警告消息,也可以在-h參數(shù)后通過now來表示立刻關(guān)機(jī)。

退出登錄

退出登錄 - 「exit」 / 「logout」。

查看歷史命令

查看歷史命令 - 「history」。

  1. maoli@ubuntu:~$ history 
  2. ... 
  3.   625  date 
  4.   626  cal 
  5.   627  cal 5 2020 
  6.   628  shutdown -c 
  7.   629  shutdown -r 12:00 
  8.   630  shutdown -c 
  9.   631  history 
  10.  
  11. maoli@ubuntu:~$ !631   

說明:查看到歷史命令之后,可以用!歷史命令編號來重新執(zhí)行該命令;通過history -c可以清除歷史命令。

實(shí)用程序

文件和文件夾操作

創(chuàng)建/刪除空目錄

創(chuàng)建/刪除空目錄 - 「mkdir」 / 「rmdir」。

  1. [root ~]# mkdir runsen 
  2. [root ~]# mkdir -p&nbsp;abc/runsen 
  3. [root ~]# rmdir runsen 

創(chuàng)建/刪除文件

創(chuàng)建/刪除文件 - 「touch」 / 「rm」。

  1. maoli@ubuntu:~$ touch readme.txt 
  2. maoli@ubuntu:~$  rm readme.txt  
  3. rm: remove regular empty file ‘readme.txt ’? y 
  4. [root ~]# rm -rf ;xyz&amp;amp;nbsp;

touch命令用于創(chuàng)建空白文件或修改文件時間。在Linux系統(tǒng)中一個文件有三種時間:

  • 更改內(nèi)容的時間 - mtime。
  • 更改權(quán)限的時間 - ctime。
  • 最后訪問時間 - atime。

rm的幾個重要參數(shù):

  • -i:交互式刪除,每個刪除項(xiàng)都會進(jìn)行詢問。
  • -r:刪除目錄并遞歸的刪除目錄中的文件和目錄。
  • -f:強(qiáng)制刪除,忽略不存在的文件,沒有任何提示。

切換和查看當(dāng)前工作目錄

切換和查看當(dāng)前工作目錄 - 「cd」 / 「pwd」。

說明:cd命令后面可以跟相對路徑(以當(dāng)前路徑作為參照)或絕對路徑(以/開頭)來切換到指定的目錄,也可以用cd ..來返回上一級目錄。返回到上上一級目錄應(yīng)該給cd ../../命令。

查看目錄內(nèi)容

查看目錄內(nèi)容 - 「ls」。

  • -l:以長格式查看文件和目錄。
  • -a:顯示以點(diǎn)開頭的文件和目錄(隱藏文件)。
  • -R:遇到目錄要進(jìn)行遞歸展開(繼續(xù)列出目錄下面的文件和目錄)。
  • -d:只列出目錄,不列出其他內(nèi)容。
  • -S / -t:按大小/時間排序。

查看文件內(nèi)容

查看文件內(nèi)容 - 「cat」 / 「tac」 / 「head」 / 「tail」 / 「more」 / 「less」 / 「rev」 / 「od」。

  1. maoli@ubuntu:~$  wget https://www.csdn.net/ 
  2. --2020-05-28 08:25:01--  https://www.csdn.net/ 
  3. 正在解析主機(jī) www.csdn.net (www.csdn.net)... 47.95.164.112 
  4. 正在連接 www.csdn.net (www.csdn.net)|47.95.164.112|:443... 已連接。 
  5. 已發(fā)出 HTTP 請求,正在等待回應(yīng)... 200 OK 
  6. 長度: 未指定 [text/html] 
  7. 正在保存至: “index.html” 
  8.  
  9. index.html              [  <=>               ] 420.39K  1.53MB/s    in 0.3s     
  10.  
  11. 2020-05-28 08:25:02 (1.53 MB/s) - “index.html” 已保存 [430482] 
  12. maoli@ubuntu:~$ cat index.html 
  13. <!DOCTYPE html> 
  14. ... 
  15. maoli@ubuntu:~$ head -10 index.html 
  16. <!DOCTYPE html> 
  17. <html> 
  18. <head> 
  19.    <meta charset="utf-8"
  20.    <meta http-equiv="X-UA-Compatible" content="IE=Edge"
  21.    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
  22.    <meta name="apple-mobile-web-app-status-bar-style" content="black"
  23.    <meta name="referrer"content="always"
  24.    <meta name="msvalidate.01" content="3189512127C34C46BC74BED5852D45E4" /> 
  25.    <title>CSDN-專業(yè)IT技術(shù)社區(qū)</title> 
  26. maoli@ubuntu:~$ tail -2 index.html  
  27. <script src="https://g.csdnimg.cn/side-toolbar/2.0/side-toolbar.js"></script> 
  28. </html> 
  29. maoli@ubuntu:~$ less index.html  #相當(dāng)于cat 
  30. maoli@ubuntu:~$ cat -n index.html |more 
  31. ... 

說明:上面用到了一個名為wget的命令,它是一個網(wǎng)絡(luò)下載器程序,可以從指定的URL下載資源。

拷貝/移動文件

拷貝/移動文件 - 「cp」 / 「mv」。

  1. maoli@ubuntu:~$ mkdir backup 
  2. maoli@ubuntu:~$ cp index.html backup/ 
  3. maoli@ubuntu:~$ cd backup/ 
  4. maoli@ubuntu:~/backup$ ls 
  5. index.html 
  6. maoli@ubuntu:~/backup$ mv index.html csdn.html 
  7. maoli@ubuntu:~/backup$ ls 
  8. csdn.html 

文件重命名

文件重命名 - 「rename」。

查找文件和查找內(nèi)容

查找文件和查找內(nèi)容 - 「find」 / 「grep」。

  1. maoli@ubuntu:~/backup$ find ./ -name "*.html" 
  2. ./csdn.html 
  3. maoli@ubuntu:~/backup$ find / -name "*.html" 
  4. /usr/local/java/jdk-11.0.6/README.html 
  5. /usr/local/python3/lib/python3.6/idlelib/help.html 
  6. /usr/local/python3/lib/python3.6/test/sgml_input.html 
  7. /usr/local/python3/lib/python3.6/test/test_difflib_expect.html 
  8. ... 
  9. maoli@ubuntu:~/backup$  find . -type f -size +2k 
  10. ./csdn.html 
  11. maoli@ubuntu:~/backup$ find . -type f -name "*.swp" -delete 
  12. maoli@ubuntu:~/backup$ grep "<script>" csdn.html -n 
  13. 3192:        <script> 
  14. maoli@ubuntu:~/backup$ grep -E \<\/?script.*\> csdn.html -n 
  15. 12:    <script src='//g.csdnimg.cn/tingyun/1.8.3/www.js' type='text/javascript'></script> 
  16. 18:    <script src="//csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type="text/javascript"></script> 
  17. 19:    <script src="//g.csdnimg.cn/??login-box/1.0.7/login-box.js,login-box/1.0.7/login-auto.js" type="text/javascript"></script> 
  18. ... 

說明:grep在搜索字符串時可以使用正則表達(dá)式,如果需要使用正則表達(dá)式可以用grep -E或者直接使用egrep。

創(chuàng)建鏈接和查看鏈接

創(chuàng)建鏈接和查看鏈接 - 「ln」 / 「readlink」。

  1. maoli@ubuntu:~/backup$ ls -l csdn.html 
  2. -rw-rw-r-- 1 maoli maoli 430482 5月  28 08:30 csdn.html 
  3. maoli@ubuntu:~/backup$ ln /home/maoli/backup/csdn.html /home/maoli/csdn 
  4. maoli@ubuntu:~/backup$ ls -l csdn.html 
  5. -rw-rw-r-- 2 maoli maoli 430482 5月  28 08:30 csdn.html 

說明:鏈接可以分為硬鏈接和軟鏈接(符號鏈接)。硬鏈接可以認(rèn)為是一個指向文件數(shù)據(jù)的指針,就像Python中對象的引用計(jì)數(shù),每添加一個硬鏈接,文件的對應(yīng)鏈接數(shù)就增加1,只有當(dāng)文件的鏈接數(shù)為0時,文件所對應(yīng)的存儲空間才有可能被其他文件覆蓋。我們平常刪除文件時其實(shí)并沒有刪除硬盤上的數(shù)據(jù),我們刪除的只是一個指針,或者說是數(shù)據(jù)的一條使用記錄,所以類似于“文件粉碎機(jī)”之類的軟件在“粉碎”文件時除了刪除文件指針,還會在文件對應(yīng)的存儲區(qū)域填入數(shù)據(jù)來保證文件無法再恢復(fù)。軟鏈接類似于Windows系統(tǒng)下的快捷方式,當(dāng)軟鏈接鏈接的文件被刪除時,軟鏈接也就失效了。

壓縮/解壓縮和歸檔/解歸檔

.壓縮/解壓縮和歸檔/解歸檔 - 「gzip」 / 「gunzip」 / 「xz」。

  1. maoli@ubuntu:~$ ls 
  2.  redis-4.0.10.tar.tar.gz 
  3. maoli@ubuntu:~$ gunzip  redis-4.0.10.tar.tar.gz 
  4. maoli@ubuntu:~$ ls 
  5.  redis-4.0.10.tar.tar 

歸檔和解歸檔

歸檔和解歸檔 - 「tar」。

  1. maoli@ubuntu:~$  tar -xvf redis-4.0.10.tar 
  2. redis-4.0.10/ 
  3. redis-4.0.10/.gitignore 
  4. redis-4.0.10/00-RELEASENOTES 
  5. redis-4.0.10/BUGS 
  6. redis-4.0.10/CONTRIBUTING 
  7. redis-4.0.10/COPYING 
  8. redis-4.0.10/INSTALL 
  9. redis-4.0.10/MANIFESTO 
  10. redis-4.0.10/Makefile 
  11. redis-4.0.10/README.md 
  12. redis-4.0.10/deps/ 
  13. redis-4.0.10/deps/Makefile 
  14. redis-4.0.10/deps/README.md 
  15. ... 

說明:歸檔(也稱為創(chuàng)建歸檔)和解歸檔都使用tar命令,通常創(chuàng)建歸檔需要-cvf三個參數(shù),其中c表示創(chuàng)建(create),v表示顯示創(chuàng)建歸檔詳情(verbose),f表示指定歸檔的文件(file);解歸檔需要加上-xvf參數(shù),其中x表示抽取(extract),其他兩個參數(shù)跟創(chuàng)建歸檔相同。

將標(biāo)準(zhǔn)輸入轉(zhuǎn)成命令行參數(shù)

將標(biāo)準(zhǔn)輸入轉(zhuǎn)成命令行參數(shù) - 「xargs」。

下面的命令會將查找當(dāng)前路徑下的html文件,然后通過xargs將這些文件作為參數(shù)傳給rm命令,實(shí)現(xiàn)查找并刪除文件的操作。

  1. maoli@ubuntu:~$   find . -type f -name "*.html" | xargs rm -f 

下面的命令將a.txt文件中的多行內(nèi)容變成一行輸出到b.txt文件中,其中<表示從a.txt中讀取輸入,>表示將命令的執(zhí)行結(jié)果輸出到b.txt中。

  1. maoli@ubuntu:~$ xargs < a.txt > b.txt 

顯示文件或目錄

顯示文件或目錄 - 「basename」 / 「dirname」。

其他相關(guān)工具

其他相關(guān)工具。

  • sort」 - 對內(nèi)容排序
  • uniq」 - 去掉相鄰重復(fù)內(nèi)容
  • tr」 - 替換指定內(nèi)容為新內(nèi)容
  • cut」 / 「paste」 - 剪切/黏貼內(nèi)容
  • split」 - 拆分文件
  • file」 - 判斷文件類型
  • wc」 - 統(tǒng)計(jì)文件行數(shù)、單詞數(shù)、字節(jié)數(shù)
  • iconv」 - 編碼轉(zhuǎn)換
  1. maoli@ubuntu:~$ cat foo.txt 
  2. grape 
  3. apple 
  4. pitaya 
  5. maoli@ubuntu:~$ cat bar.txt 
  6. 100 
  7. 200 
  8. 300 
  9. 400 
  10. maoli@ubuntu:~$ paste foo.txt bar.txt 
  11. grape   100 
  12. apple   200 
  13. pitaya  300 
  14.         400 
  15. maoli@ubuntu:~$ paste foo.txt bar.txt > hello.txt 
  16. maoli@ubuntu:~$ cut -b 4-8 hello.txt 
  17. pe      10 
  18. le      20 
  19. aya     3 
  20. maoli@ubuntu:~$ cat hello.txt | tr '\t' ',' 
  21. grape,100 
  22. apple,200 
  23. pitaya,300 
  24. ,400 
  25. maoli@ubuntu:~$ split -l 100 sohu.html hello 
  26. maoli@ubuntu:~$ wget https://www.baidu.com/img/bd_logo1.png 
  27. maoli@ubuntu:~$ file bd_logo1.png 
  28. bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced 
  29. maoli@ubuntu:~$ wc index.html  
  30.  3820  18696 430482 index.html 
  31. maoli@ubuntu wget http://www.qq.com -O qq.html 
  32. maoli@ubuntu iconv -f gb2312 -t utf-8 qq.html 

管道和重定向管道的使用

管道的使用 - 「|」。

例子:查找當(dāng)前目錄下文件個數(shù)。

  1. maoli@ubuntu:~$ find ./ | wc -l 
  2. 80801 

例子:列出當(dāng)前路徑下的文件和文件夾,給每一項(xiàng)加一個編號。

  1. maoli@ubuntu:~$ ls | cat -n 1 abc 2 backup 

例子:查找record.log中包含AAA,但不包含BBB的記錄的總數(shù)

  1. maoli@ubuntu:~$ cat record.log | grep AAA | grep -v BBB | wc -l 

輸出重定向

輸出重定向和錯誤重定向 - 「>」 / 「>>」 / 「2>」。

  1. maoli@ubuntu:~$ cat readme.txt 
  2. banana 
  3. apple 
  4. grape 
  5. apple 
  6. grape 
  7. watermelon 
  8. pear 
  9. pitaya 
  10. maoli@ubuntu:~$ cat readme.txt | sort | uniq > result.txt 
  11. maoli@ubuntu:~$ cat result.txt 
  12. apple 
  13. banana 
  14. grape 
  15. pear 
  16. pitaya 
  17. watermelon 

輸入重定向

輸入重定向 - 「<」。

  1. maoli@ubuntu:~$ echo 'hello, world!' > hello.txt 
  2. maoli@ubuntu:~$ echo 'I will show you some code.' >> hello.txt 
  3. maoli@ubuntu:~$ cat hello.txt 
  4. hello, world! 
  5. I will show you some code. 

多重定向

多重定向 - 「tee」。

下面的命令除了在終端顯示命令ls的結(jié)果之外,還會追加輸出到ls.txt文件中。

  1. maoli@ubuntu:~$  ls | tee -a ls.txt 
  2. maoli@ubuntu:~$ cat ls.txt  
  3. abc 
  4. backup 

別名

alias」創(chuàng)建別名

  1. maoli@ubuntu:~$ alias ll='ls -l' 
  2. maoli@ubuntu:~$ alias frm='rm -rf' 
  3. maoli@ubuntu:~$ ll 
  4. 量 144920 
  5. drwxrwxr-x  3 maoli maoli      4096 5月  28 08:20 abc 
  6. maoli@ubuntu:~$frm abc 

unalias」刪除別名

  1. maoli@ubuntu:~$ funalias frm 
  2. maoli@ubuntu:~$ frm index.html 
  3. -bash: frm: command not found 

文本處理字符流編輯器

字符流編輯器 - 「sed」。

sed是操作、過濾和轉(zhuǎn)換文本內(nèi)容的工具。假設(shè)有一個名為fruit.txt的文件,內(nèi)容如下所示。

  1. maoli@ubuntu:~$ cat -n fruit.txt  
  2.      1  banana 
  3.      2  grape 
  4.      3  apple 
  5.      4  watermelon 
  6.      5  orange 

接下來,我們在第2行后面添加一個pitaya。

  1. maoli@ubuntu:~$  sed '2a pitaya' fruit.txt  
  2. banana 
  3. grape 
  4. pitaya 
  5. apple 
  6. watermelon 
  7. orange 

在第2行前面插入一個waxberry。

  1. maoli@ubuntu:~$ sed '2i waxberry' fruit.txt 
  2. banana 
  3. waxberry 
  4. grape 
  5. apple 
  6. watermelon 
  7. orange 

刪除第3行。 

  1. maoli@ubuntu:~$ sed '3d' fruit.txt 
  2. banana 
  3. grape 
  4. watermelon 
  5. orange 

刪除第2行到第4行。

  1. maoli@ubuntu:~$ sed '2,4d' fruit.txt 
  2. banana 
  3. orange 

將文本中的字符a替換為@。

  1. maoli@ubuntu:~$ sed 's#a#@#' fruit.txt  
  2. b@nana 
  3. gr@pe 
  4. @pple 
  5. w@termelon 
  6. or@nge 

將文本中的字符a替換為@,使用全局模式。

  1. maoli@ubuntu:~$ sed 's#a#@#g' fruit.txt  
  2. b@n@n@ 
  3. gr@pe 
  4. @pple 
  5. w@termelon 
  6. or@nge 

模式匹配和處理語言

模式匹配和處理語言 - 「awk」。

awk是一種編程語言,也是Linux系統(tǒng)中處理文本最為強(qiáng)大的工具,它的作者之一和現(xiàn)在的維護(hù)者就是之前提到過的Brian Kernighan(ken和dmr最親密的伙伴)。通過該命令可以從文本中提取出指定的列、用正則表達(dá)式從文本中取出我們想要的內(nèi)容、顯示指定的行以及進(jìn)行統(tǒng)計(jì)和運(yùn)算,總之它非常強(qiáng)大。

假設(shè)有一個名為fruit2.txt的文件,內(nèi)容如下所示。

  1. \maoli@ubuntu:~$ cat fruit2.txt  
  2. 1       banana      120 
  3. 2       grape       500 
  4. 3       apple       1230 
  5. 4       watermelon  80 
  6. 5       orange      400 

顯示文件的第3行。

  1. maoli@ubuntu:~$ awk 'NR==3' fruit2.txt  
  2. 3       apple       1230 

顯示文件的第2列。

  1. maoli@ubuntu:~$awk '{print $2}' fruit2.txt  
  2. banana 
  3. grape 
  4. apple 
  5. watermelon 
  6. orange 

顯示文件的最后一列。

  1. maoli@ubuntu:~$ awk '{print $NF}' fruit2.txt  
  2. 120 
  3. 500 
  4. 1230 
  5. 80 
  6. 400 

輸出末尾數(shù)字大于等于300的行。

  1. maoli@ubuntu:~$ awk '{if($3 >= 300) {print $0}}' fruit2.txt  
  2. 2       grape       500 
  3. 3       apple       1230 
  4. 5       orange      400 

用戶管理

創(chuàng)建和刪除用戶

創(chuàng)建和刪除用戶 - 「useradd」 / 「userdel」。需要用root賬號創(chuàng)建

  1. maoli@ubuntu:~$ su root 
  2. 密碼:  
  3. root@ubuntu:/home/maoli# useradd Runsen 
  4. root@ubuntu:/home/maoli#  userdel Runsen 
  • -d - 創(chuàng)建用戶時為用戶指定用戶主目錄
  • -g - 創(chuàng)建用戶時指定用戶所屬的用戶組

創(chuàng)建和刪除用戶組

創(chuàng)建和刪除用戶組 - 「groupadd」 / 「groupdel」。

用戶組主要是為了方便對一個組里面所有用戶的管理。

修改密碼

修改密碼 - 「passwd」。

  1. root@ubuntu:/home/maoli# passwd maoli 
  2. New password:  
  3. Retype new password:  
  4. passwd: all authentication tokens updated successfully. 

如果使用passwd命令時沒有指定命令作用的對象,則表示要修改當(dāng)前用戶的密碼。如果想批量修改用戶密碼,可以使用chpasswd命令。

  • -l / -u - 鎖定/解鎖用戶。
  • -d - 清除用戶密碼。
  • -e - 設(shè)置密碼立即過期,用戶登錄時會強(qiáng)制要求修改密碼。
  • -i - 設(shè)置密碼過期多少天以后禁用該用戶。

查看和修改密碼有效期查看和修改密碼有效期 - 「chage」。

設(shè)置maoli用戶100天后必須修改密碼,過期前15天通知該用戶,過期后15天禁用該用戶。

  1. root@ubuntu:/home/maoli# chage -M 100 -W 15 -I 15 maoli 

切換用戶 - 「su」。

  1. root@ubuntu:/home/maoli#  su maoli 
  2. maoli@ubuntu:~$  

以管理員身份執(zhí)行命令

以管理員身份執(zhí)行命令 - 「sudo」。

  1. maoli@ubuntu:~$  ls /root 
  2. ls: cannot open directory /root: Permission denied 
  3. maoli@ubuntu:~$  
  4. sudo ls /root 
  5. [sudo] password for maoli: 

「說明」:如果希望用戶能夠以管理員身份執(zhí)行命令,用戶必須要出現(xiàn)在sudoers名單中,sudoers文件在 /etc目錄下,如果希望直接編輯該文件也可以使用下面的命令。

編輯sudoers文件

編輯sudoers文件 - 「visudo」。

這里使用的編輯器是vim,關(guān)于vim的知識在前面有講解。

五十二、ViM的使用該文件的部分內(nèi)容如下所示:

  1. ## Allow root to run any commands anywhere  
  2. root    ALL=(ALL)   ALL 
  3.  
  4. ## Allows members of the 'sys' group to run networking, software,  
  5. ## service management apps and more. 
  6. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS 
  7. ## Allows people in group wheel to run all commands 
  8. %wheel  ALL=(ALL)   ALL 
  9.  
  10. ## Same thing without a password 
  11. # %wheel    ALL=(ALL)   NOPASSWD: ALL 
  12.  
  13. ## Allows members of the users group to mount and unmount the 
  14. ## cdrom as root 
  15. # %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom 
  16.  
  17. ## Allows members of the users group to shutdown this system 
  18. # %users  localhost=/sbin/shutdown -h now 

顯示用戶與用戶組的信息

顯示用戶與用戶組的信息 - 「id」。

  1. root@ubuntu:/home/maoli# id 
  2. uid=0(root) gid=0(root) 組=0(root) 

給其他用戶發(fā)消息

給其他用戶發(fā)消息 -「write」 / 「wall」。

發(fā)送方:

  1. root@ubuntu# write maoli 
  2. Hello Maoli 
  3. EOF 

鍵入EOF表示信息結(jié)束,用Crtl+D組合鍵發(fā)送信息。輸入內(nèi)容會出現(xiàn)在用戶的屏幕上,同時通信中止。接收方:

  1. maoli@ubuntu:~$   
  2. Message from root on pts/0 at 9:41 ... 
  3. Hello Maoli 
  4. EOF 

查看/設(shè)置是否接收其他用戶發(fā)送的消息 - 「mesg」。

  1. maoli@ubuntu:~$   mesg 
  2. is y  
  3. maoli@ubuntu:~$   mesg n 
  4. maoli@ubuntu:~$ mesg 
  5. is n 

如果想要發(fā)送一條信息給系統(tǒng)中所有用戶,可以使用wall命令,wall表示:write all。輸入wall,然后編輯信息,如果shell支持可以使用中文。然后使用Crtl+D組合鍵發(fā)送信息。這樣系統(tǒng)所有登錄用戶的桌面會收到信息。如 果在網(wǎng)絡(luò)上,可以使用rwall命令把信息發(fā)送到局域網(wǎng)上所有的用戶。

文件系統(tǒng)

  1. 文件和路徑命名規(guī)則:文件名的最大長度與文件系統(tǒng)類型有關(guān),一般情況下,文件名不應(yīng)該超過255個字符,雖然絕大多數(shù)的字符都可以用于文件名,但是最好使用英文大小寫字母、數(shù)字、下劃線、點(diǎn)這樣的符號。文件名中雖然可以使用空格,但應(yīng)該盡可能避免使用空格,否則在輸入文件名時需要用將文件名放在雙引號中或者通過\對空格進(jìn)行轉(zhuǎn)義。
  2. 擴(kuò)展名:在Linux系統(tǒng)下文件的擴(kuò)展名是可選的,但是使用擴(kuò)展名有助于對文件內(nèi)容的理解。有些應(yīng)用程序要通過擴(kuò)展名來識別文件,但是更多的應(yīng)用程序并不依賴文件的擴(kuò)展名,就像file命令在識別文件時并不是依據(jù)擴(kuò)展名來判定文件的類型。
  3. 隱藏文件:以點(diǎn)開頭的文件在Linux系統(tǒng)中是隱藏文件(不可見文件)。

目錄結(jié)構(gòu)

  1. /bin - 基本命令的二進(jìn)制文件。
  2. /boot - 引導(dǎo)加載程序的靜態(tài)文件。
  3. /dev - 設(shè)備文件。
  4. 「/etc」 - 配置文件。
  5. /home - 普通用戶主目錄的父目錄。
  6. /lib - 共享庫文件。
  7. /lib64 - 共享64位庫文件。
  8. /lost+found - 存放未鏈接文件。
  9. /media - 自動識別設(shè)備的掛載目錄。
  10. /mnt - 臨時掛載文件系統(tǒng)的掛載點(diǎn)。
  11. /opt - 可選插件軟件包安裝位置。
  12. /proc - 內(nèi)核和進(jìn)程信息。
  13. 「/root」 - 超級管理員用戶主目錄。
  14. /run - 存放系統(tǒng)運(yùn)行時需要的東西。
  15. /sbin - 超級用戶的二進(jìn)制文件。
  16. /sys - 設(shè)備的偽文件系統(tǒng)。
  17. /tmp - 臨時文件夾。
  18. 「/usr」 - 用戶應(yīng)用目錄。
  19. /var - 變量數(shù)據(jù)目錄。

訪問權(quán)限

改變文件模式

1.「chmod」 - 改變文件模式比特。

  1. maoli@ubuntu:~/backup$ ls -l 
  2. 總用量 424 
  3. -rw-rw-r-- 2 maoli maoli 430482 5月  28 08:30 csdn.html 
  4. maoli@ubuntu:~/backup$ chmod g+w,o+w csdn.html 
  5. maoli@ubuntu:~/backup$ ls -l 
  6. 總用量 424 
  7. -rw-rw-rw- 2 maoli maoli 430482 5月  28 08:30 csdn.html 
  8. maoli@ubuntu:~/backup$ chmod 644 csdn.html  
  9. maoli@ubuntu:~/backup$ ls -l 
  10. 總用量 424 
  11. -rw-r--r-- 2 maoli maoli 430482 5月  28 08:30 csdn.html 

說明:通過上面的例子可以看出,用chmod改變文件模式比特有兩種方式:一種是字符設(shè)定法,另一種是數(shù)字設(shè)定法。除了chmod之外,可以通過umask來設(shè)定哪些權(quán)限將在新文件的默認(rèn)權(quán)限中被刪除。


改變文件所有者

2.「chown」 - 改變文件所有者。

  1. maoli@ubuntu:~/backup$ ls -l 
  2. 總用量 424 
  3. -rw-r--r-- 2 maoli maoli 430482 5月  28 08:30 csdn.html 
  4. maoli@ubuntu:~/backup$ sudo chown root csdn.html 
  5. [sudo] maoli 的密碼:  
  6. maoli@ubuntu:~/backup$ ls -l 
  7. 總用量 424 
  8. -rw-r--r-- 2 root maoli 430482 5月  28 08:30 csdn.html 

3.「chgrp」 - 改變用戶組。

本文已收錄 GitHub,傳送門~[1] ,里面更有大廠面試完整考點(diǎn),歡迎 Star。

Reference

[1]傳送門~https://github.com/MaoliRUNsen/runsenlearnpy100

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2009-10-12 11:14:51

LinuxLinux磁盤文件系統(tǒng)管理

2009-10-13 14:31:26

:Linux系統(tǒng)磁盤系統(tǒng)管理

2018-01-10 12:42:09

Linux磁盤文件系統(tǒng)

2010-08-05 11:03:55

創(chuàng)建分區(qū)文件系統(tǒng)

2010-08-02 16:15:20

ibmdwLinux

2016-12-27 10:48:59

Linux命令磁盤與文件系統(tǒng)

2017-12-04 13:30:12

Linux文件系統(tǒng)鏈接

2023-09-27 23:19:04

Linuxmount

2023-09-03 17:09:58

LinuxSAN LUN磁盤

2023-09-05 15:17:48

LinuxLUN磁盤

2011-01-13 14:10:30

Linux文件系統(tǒng)

2020-07-22 14:53:06

Linux系統(tǒng)虛擬文件

2010-04-26 17:34:37

Unix文件

2010-04-13 13:55:36

Unix文件系統(tǒng)

2010-04-07 18:42:42

Unix命令

2021-06-06 16:55:22

Linux文件系統(tǒng)

2021-04-12 05:44:44

Linux文件系統(tǒng)

2023-12-06 09:32:35

Linux系統(tǒng)

2010-04-15 16:24:10

Unix操作系統(tǒng)

2012-03-09 17:31:50

ibmdw
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號