天天看點

Linux指令實踐(一)

Linux指令實踐(一)

-h要和-l一起用才看得到效果。這裡說一下Linux裡的指令選項,Linux裡的指令選項分長選項和短選項,長選項就是以--開頭的 通常情況是--一個單詞之類的,選項和選項之間不能合并使用,必須有空格給予區分。短選項是以-開頭的 通常後面接的是一個字元或單詞 ,短選項之間可以合并使用 比如ls -l -h 就可以寫成ls -lh。

1.pwd(printing working directory)列印目前工作目錄路徑

[root@test sysconfig]# pwd
/etc/sysconfig
      

2.ls(list)列出目前目錄下的檔案

  文法:ls [OPTION]... [FILE]... 

  選項:-l 長格式顯示目前目錄下的檔案

[root@test ~]# ls -l
total 48
-rw-r--r-- 1 root root     0 Sep 10 09:42  
-rw-r--r-- 1 root root 20163 Sep 20 18:36 a.txt
-rw-r--r-- 1 root root   177 Sep 12 10:55 mycron
drwxr-xr-x 3 root root  4096 Sep 14 10:52 scripts
drwxr-xr-x 2 root root  4096 Sep 11 15:33 test
-rw-r--r-- 1 root root   177 Oct 12 08:52 xx.xx
-rw-r--r-- 1 root root  9607 Sep 11 15:32 xxx.tar.gz          

    檔案類型:

      -:普通檔案(file)

      d:目錄檔案(directory)

      b:塊裝置檔案(block)

      c:字元裝置檔案(character)

      l:符号連結檔案(symbolic link file)

      p:指令管道檔案(pipe)

      s:套接字檔案(socket)

檔案權限:9位,每3位一組,每一組:rwx(讀寫執行),如果沒有相對應的權限用'-'占位

檔案硬連結的次數

檔案的屬主(owner)

檔案的屬組(group)

檔案的大小(size),預設機關是位元組

時間戳(timestamp):顯示是最近一次修改的時間

通路:access

修改:modify 這個是檔案的内容發生改變

改變:change,metadata,中繼資料 或者說改變屬性

當然除此以上權限,還有其他權限,上面是普通權限位的一個簡要介紹,我們先了解,後面我們會細說Linux檔案普通權限和特殊權限。

-h:以人類可以讀的格式顯示檔案的大小

[root@test ~]# ls -lh
total 48K
-rw-r--r-- 1 root root    0 Sep 10 09:42  
-rw-r--r-- 1 root root  20K Sep 20 18:36 a.txt
-rw-r--r-- 1 root root  177 Sep 12 10:55 mycron
drwxr-xr-x 3 root root 4.0K Sep 14 10:52 scripts
drwxr-xr-x 2 root root 4.0K Sep 11 15:33 test
-rw-r--r-- 1 root root  177 Oct 12 08:52 xx.xx
-rw-r--r-- 1 root root 9.4K Sep 11 15:32 xxx.tar.gz
      

  說明:-h要和-l一起用才看得到效果。這裡說一下Linux裡的指令選項,Linux裡的指令選項分長選項和短選項,長選項就是以--開頭的 通常情況是--一個單詞之類的,選項和選項之間不能合并使用,必須有空格給予區分。短選項是以-開頭的 通常後面接的是一個字元或單詞 ,短選項之間可以合并使用 比如ls -l -h 就可以寫成ls -lh。

    -a:顯示以.開頭的隐藏檔案(要顯示.或..)

[root@test ~]# ls -a
    .bash_history  .bashrc   .mysql_history  .viminfo  scripts  xxx.tar.gz
.   .bash_logout   .cshrc    .ssh            a.txt     test
..  .bash_profile  .lesshst  .tcshrc         mycron    xx.xx
      

    -A:顯示已.開頭的隐藏檔案(不顯示.或..)

[root@test ~]# ls -A
               .bash_logout   .bashrc  .lesshst        .ssh     .viminfo  mycron   test   xxx.tar.gz
.bash_history  .bash_profile  .cshrc   .mysql_history  .tcshrc  a.txt     scripts  xx.xx
      

  說明:.表示目前目錄,..表示目前目錄的父目錄。

    -d:顯示檔案自身屬性

[root@test ~]# ls -ld
dr-xr-x---. 5 root root 4096 Oct 13 20:00 .
      

    -i:顯示檔案的索引節點号(index node 簡稱inode)

[root@test ~]# ls -i
135914    135474 a.txt  135927 mycron  131075 scripts  135455 test  140544 xx.xx  135923 xxx.tar.gz
      

    -r:逆序顯示檔案(reversed)

[root@test ~]# ls
   a.txt  mycron  scripts  test  xx.xx  xxx.tar.gz
[root@test ~]# ls -r
xxx.tar.gz  xx.xx  test  scripts  mycron  a.txt   
      

    -R:遞歸(recursive)顯示檔案

[root@test work]# ls -l
total 40
-rw-r--r-- 1 root root   56 Jul 13 07:49 list.txt
drwxr-xr-x 2 root root 4096 Sep  8 20:20 mysql_log
-rw------- 1 root root    5 Jun 25 11:30 rsync.password
drwxr-xr-x 2 root root 4096 Aug 31 17:40 scripts
drwxr-xr-x 2 root root 4096 Aug 31 17:28 test
-rw-r--r-- 1 root root  221 Jul 13 07:35 test.tar.gz
-rw-r--r-- 1 root root   79 Sep  8 16:46 test.txt
-rw-r--r-- 1 root root 3259 Sep 13 15:08 tomcat_star.sh
-rw-r--r-- 1 root root 2926 Sep 13 15:08 tomcat_star01.sh
-rw-r--r-- 1 root root  177 Jul 13 07:52 xxxx.tar.gz
[root@test work]# ls -R
.:
list.txt   rsync.password  test         test.txt        tomcat_star01.sh
mysql_log  scripts         test.tar.gz  tomcat_star.sh  xxxx.tar.gz

./mysql_log:
mysql.log

./scripts:
auto_bak_log.sh     batch_create_user.sh  clear             rsync_server_config.sh
auto_delete_log.sh  batch_delete_user.sh  nginx_install.sh

./test:
      

3.cd:change directory 切換目錄

cd指令用來切換工作目錄至dirname 其中dirName表示法可為絕對路徑或相對路徑。若目錄名稱省略,則變換至使用者的home directory(也就是剛登入時所在的目錄)。另外,

~

也表示為home directory的意思,

.

則是表示目前所在的目錄,

..

則表示目前目錄位置的上一層目錄。

cd 不帶任何選項和參數 就回到了家目錄

[root@test work]# pwd
/work
[root@test work]# cd
[root@test ~]# pwd
/root
      

cd ~username  切換到指定使用者的家目錄

[root@test ~]# cd ~qiuhom
[root@test qiuhom]# pwd
/home/qiuhom
      

  提示:切換到指定使用者家目錄的前提是root使用者喲

cd - 切換到上一次所在目錄(在目前目錄和前一次所在目錄之間來回切換)

[root@test qiuhom]# cd /etc/init.d
[root@test init.d]# pwd
/etc/init.d
[root@test init.d]# cd /var/log/
[root@test log]# pwd
/var/log
[root@test log]# cd -
/etc/init.d
[root@test init.d]# cd -
/var/log
[root@test log]#        
cd ~  進入使用者主目錄      
[root@test log]# pwd
/var/log
[root@test log]# cd ~
[root@test ~]# pwd
/root       
d ..  傳回上級目錄      
[root@test etc]# cd /etc/init.d/
[root@test init.d]# pwd
/etc/init.d
[root@test init.d]# cd ..
[root@test etc]# pwd
/etc
      

4.type顯示指定指令屬于那種指令類型

首先說下Linux指令類型分為内置指令和外部指令兩種,内置指令指的是shell内置的指令,用type去檢視會傳回shell builtin的字元串,說明我們查詢的指令是内置shell指令,外部指令用type檢視一般在檔案系統的某個路徑下有一個與指令名稱相應的可執行檔案。

[root@test etc]# type cd
cd is a shell builtin
[root@test etc]# type type
type is a shell builtin
[root@test etc]# type tree
tree is /usr/bin/tree
[root@test etc]# type ifconfig
ifconfig is /sbin/ifconfig
      

5.hash:檢視緩存指令和使用的次數顯示了之前指令的操作記錄,hits表示我們使用的次數

[root@test etc]# hash
hits    command
  11    /usr/bin/cal
   2    /bin/date
   7    /usr/bin/man
   2    /sbin/clock
  28    /bin/ls      

  說明:linux系統下會有一個hash表,當你剛開機時這個hash表為空,每當你執行過一條指令時,hash表會記錄下這條指令的路徑,就相當于緩存一樣。第一次執行指令shell解釋器預設的會從PATH路徑下尋找該指令的路徑,當你第二次使用該指令時,shell解釋器首先會檢視hash表有沒有緩存該指令,如果沒有該指令才會去PATH路徑下尋找。

-d:清空指定指令的緩存記錄(後面要指定指令)

[root@test etc]# hash
hits    command
  11    /usr/bin/cal
   2    /bin/date
   7    /usr/bin/man
   2    /sbin/clock
  28    /bin/ls
[root@test etc]# hash -d cal
[root@test etc]# hash
hits    command
   2    /bin/date
   7    /usr/bin/man
   2    /sbin/clock
  28    /bin/ls
      

-p:把指定路徑 對應的指令加入到指令緩存表裡

[root@test etc]# hash -p /usr/bin/cal cal
[root@test etc]# hash
hits    command
   0    /usr/bin/cal
   2    /bin/date
   7    /usr/bin/man
   2    /sbin/clock
  28    /bin/ls
      

-l:檢視hash表指令的路徑和對應的指令

[root@test etc]# hash -l
builtin hash -p /usr/bin/cal cal
builtin hash -p /bin/date date
builtin hash -p /usr/bin/man man
builtin hash -p /sbin/clock clock
builtin hash -p /bin/ls ls
      

-r:清空指令緩存表(清空全部)  

[root@test etc]# hash 
hits    command
   0    /usr/bin/cal
   2    /bin/date
   7    /usr/bin/man
   2    /sbin/clock
  28    /bin/ls
[root@test etc]# hash -r
[root@test etc]# hash
hash: hash table empty
      

6.date列印或設定系統日期和時間(print or set the system date and time)

文法:date(選項)(參數)
選項:      
-d<字元串>:顯示字元串所指的日期與時間。字元串前後必須加上雙引号;
-s<字元串>:根據字元串來設定日期與時間。字元串前後必須加上雙引号;
參數:<+時間日期格式>:指定顯示時使用的日期時間格式。
日期格式字元串表      
%H 小時,24小時制(00~23)
%I 小時,12小時制(01~12)
%k 小時,24小時制(0~23)
%l 小時,12小時制(1~12)
%M 分鐘(00~59)
%p 顯示出AM或PM
%r 顯示時間,12小時制(hh:mm:ss %p)
%s 從1970年1月1日00:00:00到目前經曆的秒數
%S 顯示秒(00~59)
%T 顯示時間,24小時制(hh:mm:ss)
%X 顯示時間的格式(%H:%M:%S)
%Z 顯示時區,日期域(CST)
%a 星期的簡稱(Sun~Sat)
%A 星期的全稱(Sunday~Saturday)
%h,%b 月的簡稱(Jan~Dec)
%B 月的全稱(January~December)
%c 日期和時間(Tue Nov 20 14:12:58 2012)
%d 一個月的第幾天(01~31)
%x,%D 日期(mm/dd/yy)
%j 一年的第幾天(001~366)
%m 月份(01~12)
%w 一個星期的第幾天(0代表星期天)
%W 一年的第幾個星期(00~53,星期一為第一天)
%y 年的最後兩個數字(1999則是99)
      
-d<字元串>:顯示字元串所指的日期與時間。字元串前後必須加上雙引号
顯示目前年月日:      
[root@test etc]# date +%Y%m%d
20181015
      

顯示昨天的日期

[root@test etc]# date -d "-1 day" +%Y%m%d  
20181014        

 顯示明天的日期

[root@test etc]# date -d "+1 day" +%Y%m%d
20181016
      

顯示上一月今天的日期

[root@test etc]# date -d "-1 month" +%Y%m%d
20180915      

顯示下個月今天的日期

[root@test etc]# date -d "+1 month" +%Y%m%d 
20181115      

顯示昨年今天的日期

[root@test etc]# date -d "-1 year" +%Y%m%d
20171015      

顯示明年今天的日期

[root@test etc]# date -d "+1 year" +%Y%m%d 
20191015
      

設定時間:

設定目前時間,隻有root權限才能設定,其他隻能檢視 

[root@test etc]# date -s 20191025
Fri Oct 25 00:00:00 CST 2019
      

  說明:設定成2019年10月25日 這樣設定它會預設把時分秒設定成00:00:00

[root@test etc]# date -s 12:15:30
Fri Oct 25 12:15:30 CST 2019
      

  說明:設定具體時間,不會對日期做更改

[root@test etc]# date -s "13:54:20 2018-10-26"
Fri Oct 26 13:54:20 CST 2018
[root@test etc]# date -s "15:45:25 20181112"
Mon Nov 12 15:45:25 CST 2018
[root@test etc]# date -s "20181113 14:20:20"
Tue Nov 13 14:20:20 CST 2018
      

  說明:以上3中寫法都可以設定完整全部的時間,年月日時分秒。

7.cal:calendar 顯示月曆

[root@test etc]# cal
    November 2018   
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
      

  說明:預設顯示一個月的 當然我們可以指定年月來顯示。

顯示2018一年的月曆 cal 2018

[root@test etc]# cal 2018
                               2018                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6                1  2  3                1  2  3
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    4  5  6  7  8  9 10
14 15 16 17 18 19 20   11 12 13 14 15 16 17   11 12 13 14 15 16 17
21 22 23 24 25 26 27   18 19 20 21 22 23 24   18 19 20 21 22 23 24
28 29 30 31            25 26 27 28            25 26 27 28 29 30 31

        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7          1  2  3  4  5                   1  2
 8  9 10 11 12 13 14    6  7  8  9 10 11 12    3  4  5  6  7  8  9
15 16 17 18 19 20 21   13 14 15 16 17 18 19   10 11 12 13 14 15 16
22 23 24 25 26 27 28   20 21 22 23 24 25 26   17 18 19 20 21 22 23
29 30                  27 28 29 30 31         24 25 26 27 28 29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4                      1
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    2  3  4  5  6  7  8
15 16 17 18 19 20 21   12 13 14 15 16 17 18    9 10 11 12 13 14 15
22 23 24 25 26 27 28   19 20 21 22 23 24 25   16 17 18 19 20 21 22
29 30 31               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6                1  2  3                      1
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    2  3  4  5  6  7  8
14 15 16 17 18 19 20   11 12 13 14 15 16 17    9 10 11 12 13 14 15
21 22 23 24 25 26 27   18 19 20 21 22 23 24   16 17 18 19 20 21 22
28 29 30 31            25 26 27 28 29 30      23 24 25 26 27 28 29
                                              30 31
      

顯示指定某一年的某個月 如: cal 10 2018 表示顯示2018年10月的月曆

[root@test etc]# cal 10 2018
    October 2018    
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
      

8.hwclock查詢并設定硬體時鐘(query and set the hardware clock)

 linux系統時鐘有兩個,一個是硬體時鐘,即BIOS時間,另一個是系統時鐘,是linux系統Kernel時間,程式運作時讀取的時間是系統時間。當Linux啟動時,系統Kernel會去讀取硬體時鐘的設定,然後系統時鐘就會獨立于硬體運作。

硬體時鐘(hwclock或者clock檢視)

[root@test etc]# clock
Tue Oct 16 00:21:11 2018  -0.625710 seconds
[root@test etc]# hwclock
Tue Oct 16 00:21:18 2018  -0.077961 seconds
      

系統時鐘(date檢視) 

[root@test etc]# date
Tue Nov 13 14:48:27 CST 2018       

通常這兩個時間是不同步的,我們hwclock指令去同步兩者的時間。

-w:将系統時鐘同步到硬體時鐘

[root@test etc]# clock
Tue Oct 10 00:00:21 2017  -0.769098 seconds
[root@test etc]# date -s "20181016 00:39:00"
Tue Oct 16 00:39:00 CST 2018
[root@test etc]# hwclock -w
[root@test etc]# clock
Tue Oct 16 00:39:12 2018  -0.205121 seconds
      

-s:将硬體時鐘同步到系統時鐘  

[root@test etc]# hwclock
Tue Oct 16 00:40:26 2018  -0.082141 seconds
[root@test etc]# date
Tue Oct 10 00:00:09 CST 2017
[root@test etc]# hwclock -s
[root@test etc]# date
Tue Oct 16 00:40:40 CST 2018
      

-r:讀取硬體時鐘 

[root@test etc]# clock
Tue Oct 16 00:41:17 2018  -0.587698 seconds
[root@test etc]# hwclock -r
Tue Oct 16 00:41:24 2018  -0.898271 seconds
[root@test etc]# hwclock
Tue Oct 16 00:41:29 2018  -0.498146 seconds
      

  提示:其實不加-r選項也是可以查詢的。

9.獲得指令的使用幫助

内部指令:help command

外部指令:command --help 獲得指令的簡要幫助資訊

指令手冊:man command (manual)

首先我們說下man手冊裡面的章節數字所代表的含義:

1:使用者指令(user command)所有使用者都可以使用的非管理者指令,如(/bin,/usr/bin,/usr/local/bin)下的指令。

2:系統調用(system calls)

3:c 語言庫調用( C Library Functions)

4:特殊檔案(如:裝置檔案)

5:檔案格式(解釋某個指令的配置檔案的文法的,如passwd)

6:遊戲相關的

7:雜項:miscellaneous(不便歸類的指令)

8:系統管理者(超級管理者所用指令,如/sbin,/usr/sbin,/usr/local/sbin)下的指令,一般就是指root使用者可以用的指令。

man手冊裡特殊符号所代表的意思:

[]:表示可選的

<>:表示必選的

...:表示可以重複多次

|:表示多選一

{}:分組,沒有特殊意義

NAME:指令名稱及功能簡要說明

SYNOPSIS:用法說明,包括可以用的選項

DESCRIPTION:指令功能的詳盡說明,包括每一個選項的意義

OPTIONS:說明每一個選項的意義

FILES:此指令相關的配置檔案

BUGE:報告bug郵箱位址

EXAMPLES:使用示例

SEE ALSO:另外參照

翻屏操作:

向後翻一屏:space

向前翻一屏:b

向後翻一行:enter或者向下放向鍵 或者j

向前翻一行:k 或向上方向鍵

查找:

/加要查的内容 按n向下搜尋 N 向上搜尋 預設從上往下搜尋

?加要查的内容 按N向下搜尋 n 向上搜尋 預設從下往上搜尋

q 退出

10.echo:顯示一行文本内容,在shell中常用來列印shell變量的值或者直接輸出指定的字元串

文法:echo(選項)(參數)

-n:不換行顯示内容

-e:對反斜杠轉義的解釋

\\ backslash(反斜線,插入\字元)

\a alert (BEL)(響鈴,發出警告聲)

\b backspace(倒退,相當于backspace鍵的功能,删除前一個字元)

\c produce no further output(最後不加上換行符号)

\f form feed(換行但光标仍舊停留在原來的位置;)

\n new line(換行,且光标移至行首)

\r carriage return(光标移至行首,但不換行)

\t horizontal tab(水準插入tab)

\v vertical tab(垂直插入tab)

[root@test etc]# echo -en "this is test...\b\b\b"
this is test[root@test etc]# echo -e "\\"
\
[root@test etc]# echo -e "this is test\n"
this is test

[root@test etc]# echo -e "this is test\f"
this is test

[root@test etc]# echo -e "this is \ftest"  
this is 
        test
[root@test etc]# echo -e "this is \vtest" 
this is 
        test
[root@test etc]# echo -e "this is \ttest" 
this is         test
[root@test etc]# echo -e "this is \ctest"  
this is [root@test etc]       

作者:Linux-1874

出處:https://www.cnblogs.com/qiuhom-1874/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀