引言
? ? ? ? 簡單介紹一些Linux的基本指令,快速上手Linux操作系統。
一、ls指令
語法:ls [選項] [目錄或文件]
功能::對于目錄,該命令列出該目錄下的所有子目錄與文件。
???????????????對于文件件,將列出文件名以及其他信息
常用選項:
- -a 列出目錄下的所以文件,包含以 . 開頭的隱含文件
- -l 列出文件的詳細信息
- -d 將目錄像文件一樣顯示,而不是顯示其下的文件。ls -d [指定目錄]
- 等等等等,后面遇到了再詳細介紹。
[root@hcss-ecs-f571 ~]# ls
learn01
[root@hcss-ecs-f571 ~]# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .cshrc .history learn01 .pki .ssh .tcshrc
[root@hcss-ecs-f571 ~]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Jul 29 22:12 learn01
[root@hcss-ecs-f571 ~]# ls -a -l
total 48
dr-xr-x---. 6 root root 4096 Jul 29 22:12 .
dr-xr-xr-x. 19 root root 4096 May 25 23:07 ..
-rw-r--r-- 1 root root 475 Jul 29 22:12 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwx------ 3 root root 4096 Jul 26 2024 .cache
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw------- 1 root root 0 Jul 26 2024 .history
drwxr-xr-x 2 root root 4096 Jul 29 22:12 learn01
drwxr----- 3 root root 4096 Jul 26 2024 .pki
drwx------ 2 root root 4096 May 25 23:07 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
二、pwd命令
語法:pwd
功能:顯示用戶當前所在的目錄
常用選項:無
[root@hcss-ecs-f571 ~]# pwd
/root
?Linux理論知識:路徑的認識
??Linux系統中,磁盤上的文件和目錄被組成一棵目錄樹,每個節點都是目錄或文件
??其中普通文件?定是目錄樹的葉子節點
? 目錄可能是葉子(空目錄),也可能是路上節點
??理解路徑存在的意義:樹狀組織方式,保證快速定位查找到指定的文件,而定位文件就需要具有唯?性的方案來進行定位文件。其中任何?個節點,都只有?個父節點,所以,從根目錄開始,定位指定文件,路徑具有唯?性
??絕對路徑:?般從/開始,不依賴其他?錄的定位文件的方式
??相對路徑:相對于當前用戶所處?錄,定位?件的路徑方式
??絕對路徑?般不會隨著用戶的路徑變化而喪失唯一性,一般在特定服務的配置文件中經常? 被使用
??相對路徑因為它的便捷性,?般在命令行中使用較多
三、cd指令
語法:cd 目錄名
功能:改變工作目錄。將當前工作目錄改變到指定的目錄下
常用選項:
- cd ..? 返回上級目錄
- cd ~ 快速進入自己的家目錄
- cd / 進入/目錄
- cd - 退回到上一次的目錄中
- cd 跟目錄? ? ??
[root@hcss-ecs-f571 learn01]# cd ~
[root@hcss-ecs-f571 ~]# pwd
/root
[root@hcss-ecs-f571 ~]# cd /
[root@hcss-ecs-f571 /]# pwd
/
[root@hcss-ecs-f571 /]# cd /root/learn01
[root@hcss-ecs-f571 learn01]# pwd
/root/learn01
[root@hcss-ecs-f571 learn01]# cd -
/
[root@hcss-ecs-f571 /]# pwd
/
[root@hcss-ecs-f571 /]# cd ~
[root@hcss-ecs-f571 ~]# pwd
/root
[root@hcss-ecs-f571 ~]#
四、touch指令
語法:touch [選項]..[文件]
功能:touch命令參數可更改文檔或目錄的日期時間,包括存取時間和更改時間,或者新建?個不存在的文件。
常用選項:
- -a:change only the access time
- -c:change only the modification time
[root@hcss-ecs-f571 ~]# ls
learn01
[root@hcss-ecs-f571 ~]# cd learn01
[root@hcss-ecs-f571 learn01]# touch my.txt
[root@hcss-ecs-f571 learn01]# ls
my.txt
[root@hcss-ecs-f571 learn01]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]#
五、mkdir指令
語法:mkdir [選項] dirname
功能:在當前目錄下創建一個名為“dirname”的目錄
常用選項:-p/--parents: 可以是一個路徑名稱。此時若路徑中的某些目錄尚不存在,加上此選項后,系統將自動建好那些尚不存在的目錄,即一次可以建立多個目錄
[root@hcss-ecs-f571 learn01]# ll
total 8
drwxr-xr-x 2 root root 4096 Jul 29 23:40 dir1
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]# mkdir dir3
[root@hcss-ecs-f571 learn01]# ll
total 12
drwxr-xr-x 2 root root 4096 Jul 29 23:40 dir1
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]# mkdir -p dir4/dir5/dir6
[root@hcss-ecs-f571 learn01]# ll
total 16
drwxr-xr-x 2 root root 4096 Jul 29 23:40 dir1
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
drwxr-xr-x 3 root root 4096 Jul 29 23:42 dir4
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]# tree dir4
dir4
└── dir5└── dir62 directories, 0 files
[root@hcss-ecs-f571 learn01]#
六、rmdir指令
語法:rmdir [-p] [dirname]
適用對象:具有當前目錄操作權限的所有使用者
功能:刪除空目錄
drwxr-xr-x 6 root root 4096 Jul 29 23:42 learn01
[root@hcss-ecs-f571 ~]# tree learn01
learn01
├── dir1
├── dir2
│?? └── dir3
│?? └── dir4
│?? └── dir5
├── dir3
├── dir4
│?? └── dir5
│?? └── dir6
└── my.txt
[root@hcss-ecs-f571 learn01]# ll
total 16
drwxr-xr-x 2 root root 4096 Jul 29 23:40 dir1
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
drwxr-xr-x 3 root root 4096 Jul 29 23:42 dir4
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]# rmdir dir1
# 直接刪除目錄dir1
[root@hcss-ecs-f571 learn01]# ll
total 12
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
drwxr-xr-x 3 root root 4096 Jul 29 23:42 dir4
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
[root@hcss-ecs-f571 learn01]# rmidr dir2
-bash: rmidr: command not found
[root@hcss-ecs-f571 learn01]# ll
total 12
drwxr-xr-x 3 root root 4096 Jul 29 23:41 dir2
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
drwxr-xr-x 3 root root 4096 Jul 29 23:42 dir4
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
# 指定路徑中有不為空的路徑,便?法刪除
[root@hcss-ecs-f571 learn01]# rmdir -p dir2/dir3/dir4
rmdir: failed to remove ‘dir2/dir3/dir4’: Directory not empty
[root@hcss-ecs-f571 learn01]# rmdir -p dir2/dir3/dir4/dir5
[root@hcss-ecs-f571 learn01]# ll
total 8
drwxr-xr-x 2 root root 4096 Jul 29 23:42 dir3
drwxr-xr-x 3 root root 4096 Jul 29 23:42 dir4
-rw-r--r-- 1 root root 0 Jul 29 23:28 my.txt
七、rm指令
語法: rm [-f-i-r-v] [dirName/dir]
適用對象:所有使用者
功能:刪除文件或目錄
常用選項:
- -f 即使文件屬性為只讀(即寫保護),亦直接刪除
- -i 刪除前逐一詢問確認
- -r 刪除目錄及其下所有文件
[root@hcss-ecs-f571 learn01]# pwd
/root/learn01
[root@hcss-ecs-f571 learn01]# tree
.
├── dir3
├── dir4
│?? └── dir5
│?? └── dir6
├── my.txt
└── test.txt4 directories, 2 files
[root@hcss-ecs-f571 learn01]# rm dir3
rm: cannot remove ‘dir3’: Is a directory
[root@hcss-ecs-f571 learn01]# rm -f dir3
rm: cannot remove ‘dir3’: Is a directory
[root@hcss-ecs-f571 learn01]# rm -r dir3
rm: remove directory ‘dir3’? y[root@hcss-ecs-f571 learn01]# rm -r -i dir4
rm: descend into directory ‘dir4’? y
rm: descend into directory ‘dir4/dir5’? y
rm: remove directory ‘dir4/dir5/dir6’? y
rm: remove directory ‘dir4/dir5’? y
rm: remove directory ‘dir4’? y
[root@hcss-ecs-f571 learn01]# tree
.
├── my.txt
└── test.txt0 directories, 2 files
[root@hcss-ecs-f571 learn01]# rm my.txt
rm: remove regular empty file ‘my.txt’? y
[root@hcss-ecs-f571 learn01]# tree
.
└── test.txt0 directories, 1 file
八、man指令
Linux的命令有很多參數,我們不可能全記住,可以通過查看聯機手冊獲取幫助
語法:man [選項] 命令
常用選項
- -k 根據關鍵字搜索聯機幫助
- num 只在第num章節查找
- -a 將所有章節都顯示出來
man手冊分為9章(不同系統可能會有差別)
- 1是普通的命令?
- 2是系統調用,如open,write之類的(通過這個,至少可以很方便的查到調用這個函數,需要加什么頭文件)
- 3是庫函數,如printf,fread4是特殊文件,也就是/dev下的各種設備文件
- 4略
- 5是指文件的格式,比如passwd,就會說明這個文件中各個字段的含義
- 6是給游戲留的,由各個游戲??定義
- 7是附件還有?些變量,比如像environ這種全局變量在這里就有說明
- 8是系統管理用的命令,這些命令只能由root使用,如ifconfig
- 9略
按q退出手冊
九、cp指令
語法:cp [選項] 源文件或目錄 目標文件或目錄
功能:復制文件或目錄
解釋:
- cp指令用于復制文件或目錄
- 如同時指定兩個以上的文件或目錄,且最后的目的地地是?個已經存在的?錄,則它會把前面指定的所有文件或目錄復制到此目錄中
常用選項:
- -f或--force強行復制文件或目錄,不論目的文件或目錄是否已經存在
- -i或--interactive 覆蓋文件之前先詢問用戶
- -r遞歸處理,將指定目錄下的文件與子目錄?并處理。若源文件或目錄的形態,不屬于目錄或符號鏈接,則?律視為普通文件處理
# cp普通文件
[root@hcss-ecs-f571 learn01]# echo "你好,世界">test.txt
[root@hcss-ecs-f571 learn01]# cat test.txt
你好,世界
[root@hcss-ecs-f571 learn01]# cp test.txt test2.txt
[root@hcss-ecs-f571 learn01]# ll
total 8
-rw-r--r-- 1 root root 16 Jul 30 15:29 test2.txt
-rw-r--r-- 1 root root 16 Jul 30 15:28 test.txt
[root@hcss-ecs-f571 learn01]# cat test2.txt
你好,世界# 將多個文件拷貝到指定路徑下
[root@hcss-ecs-f571 learn01]# mkdir dir1
[root@hcss-ecs-f571 learn01]# cp *.txt dir1
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── test2.txt
└── test.txt1 directory, 4 files
[root@hcss-ecs-f571 learn01]#
# cp目標文件存在,直接覆蓋
[root@hcss-ecs-f571 learn01]# echo "hello word" > test.txt
[root@hcss-ecs-f571 learn01]# cat test.txt
hello word
[root@hcss-ecs-f571 learn01]# cp test.txt test2.txt
cp: overwrite ‘test2.txt’? y
[root@hcss-ecs-f571 learn01]# cat test2.txt
hello word
[root@hcss-ecs-f571 learn01]#
# 遞歸強制拷貝整個目錄
[root@hcss-ecs-f571 learn01]# cp -rf dir1 dir2
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── test2.txt
└── test.txt2 directories, 6 files
[root@hcss-ecs-f571 learn01]#
十、mv指令
????????mv命令是move的縮寫,可以?來移動文件或者將文件改名(move(rename)files,經常?來備份文件或者目錄(相當于剪切功能)
語法:?mv [ 選項 ] 源文件或目錄 目標文件或目錄
功能:
- 視mv命令中第?個參數類型的不同(是目標文件還是目標目錄),mv命令將文件重命名或將其移至?個新的目錄中。
常用選項:
- -f:force強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋?
- -i:若目標文件(destination)已經存在時,就會詢問是否覆蓋!
# 更改名稱
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── dir3
├── test2.txt
└── test.txt3 directories, 6 files
[root@hcss-ecs-f571 learn01]# mv test.txt test1.txt
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── dir3
├── test1.txt
└── test2.txt3 directories, 6 files# 如果當前路徑存在同名?件,改名即覆蓋
[root@hcss-ecs-f571 learn01]# touch test3.txt
[root@hcss-ecs-f571 learn01]# mv test3.txt test2.txt
mv: overwrite ‘test2.txt’? y
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── dir3
├── test1.txt
└── test2.txt3 directories, 6 files
[root@hcss-ecs-f571 learn01]# # mv整個目錄
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── dir3
├── test1.txt
└── test2.txt3 directories, 6 files
[root@hcss-ecs-f571 learn01]# mv dir3 dir1
[root@hcss-ecs-f571 learn01]# tree
.
├── dir1
│?? ├── dir3
│?? ├── test2.txt
│?? └── test.txt
├── dir2
│?? ├── test2.txt
│?? └── test.txt
├── test1.txt
└── test2.txt3 directories, 6 files
[root@hcss-ecs-f571 learn01]# [root@hcss-ecs-f571 learn01]# mv dir1 ..
[root@hcss-ecs-f571 learn01]# cd ..
[root@hcss-ecs-f571 ~]# tree
.
├── dir1
│?? ├── dir3
│?? ├── test2.txt
│?? └── test.txt
└── learn01├── dir2│?? ├── test2.txt│?? └── test.txt├── test1.txt└── test2.txt4 directories, 6 files
十一、cat指令
語法:cat [選項] [文件]
功能:查看目標文件的內容
常用選項:
- -b對非空輸出行編號
- -n對輸出的所有行編號
- -s不輸出多行空行
# 命令?構建多??本
[root@hcss-ecs-f571 ~]# cnt=0; while [ $cnt -le 20 ]; do echo "hello word";let cnt++; done > temp.txt
[root@hcss-ecs-f571 ~]# cat temp.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word# cat 輸出攜帶行號
[root@hcss-ecs-f571 ~]# cat -b temp.txt1 hello word2 hello word3 hello word4 hello word5 hello word6 hello word7 hello word8 hello word9 hello word10 hello word11 hello word12 hello word13 hello word14 hello word15 hello word16 hello word17 hello word18 hello word19 hello word20 hello word21 hello word# 修改temp.txt,使其攜帶多行空行
[root@hcss-ecs-f571 ~]# vim temp.txt# -b 對?空輸出?編號
[root@hcss-ecs-f571 ~]# cat -b temp.txt1 hello word2 hello word3 hello word4 hello word5 hello word6 hello word7 hello word8 hello word9 hello word10 hello word11 hello word12 hello word13 hello word14 hello word15 hello word16 hello word17 hello word18 hello word19 hello word20 hello word21 hello word
# -n 對輸出的所有?編號
[root@hcss-ecs-f571 ~]# cat -n temp.txt1 hello word2 hello word3 hello word4 hello word5 hello word6 hello word7 hello word8 hello word9 hello word10 hello word11 hello word12 hello word13 hello word14 hello word15 hello word16 hello word17 hello word18 hello word19 20 21 22 23 hello word24 hello word25 26 27 hello word
# -s 不輸出多?空?,多?空?壓縮成為??
[root@hcss-ecs-f571 ~]# cat -s temp.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello wordhello word
hello wordhello word
[root@hcss-ecs-f571 ~]#
十二、more指令
語法:more [選項]
功能:more命令,功能類似cat
常用選項:
- -n指定輸出行數
- q退出more
# 命令行輸出2000行"hello word"
[root@hcss-ecs-f571 ~]# cnt=0; while [ $cnt -le 2000 ]; do echo "hello word";let cnt++; done > temp.txt
# -n 指定輸出的行數
[root@hcss-ecs-f571 ~]# more -10 temp.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
hello word
--More--(0%)
十三、less指令
語法:less [參數] 文件
功能:less與more類似,但使用less可以隨意瀏覽文件,而more僅能向前移動,卻不能向后移動,而且less在查看之前不會加載整個文件。
- less?具也是對文件或其它輸出進行分頁顯示的?具,應該說是linux正統查看文件內容的工具, 功能極其強?
- ?less的用法比起more更加的有彈性,在more的時候,我們并沒有辦法向前面翻,只能往后面看
- 若使?了less,就可以使用[pageup][pagedown]等按鍵的功能來往前往后翻看文件,更 容易用來查看?個文件的內容
- ?除此之外,在less里頭可以擁有更多的搜索功能,不止可以向下搜,也可以向上搜。
選項:?
- -i 忽略搜索時的大小寫
- -N 顯示每行的行號
- ?/字符串:向下搜索“字符串”的功能
- ??字符串:向上搜索“字符串”的功能
- ?n:重復前一個搜索(與/或?有關)
- ?N:反向重復前?個搜索(與/或?有關)
- ?q:quit
[root@hcss-ecs-f571 ~]# less -N temp.txt 1 hello 02 hello 13 hello 24 hello 35 hello 46 hello 57 hello 68 hello 79 hello 810 hello 911 hello 1012 hello 1113 hello 1214 hello 1315 hello 1416 hello 1517 hello 1618 hello 1719 hello 1820 hello 1921 hello 2022 hello 2123 hello 2224 hello 2325 hello 2426 hello 2527 hello 2628 hello 2729 hello 2830 hello 2931 hello 3032 hello 31
...
十四、head指令和tail指令
????????head與tail就像它的名字?樣的淺顯易懂,它是用來顯示開頭或結尾某個數量的文字區塊,head用來顯示檔案的開頭至標準輸出中,而tail就是看檔案的結尾。
1.head指令
語法:head [參數] [文件]
功能:head用來顯示檔案的開頭至標準輸出中,默認head命令打印其相應?件的開頭10行。
選項:
- ?-n<行數>顯示的行數
[root@hcss-ecs-f571 ~]# head temp.txt
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9[root@hcss-ecs-f571 ~]# head -5 temp.txt
hello 0
hello 1
hello 2
hello 3
hello 4
[root@hcss-ecs-f571 ~]#
2.tail指令
????????tail 命令從指定點開始將文件寫到標準輸出.使用tail命令的-f選項可以方便的查閱正在改變的日志文件,tail-f filename會把filename里最尾部的內容顯示在屏幕上,并且不斷刷新,使你看到最新的文件內容。
語法:tail 必要參數 [文件]
功能:用于顯示指定文件末尾內容,不指定文件時,作為輸入信息進行處理。常用查看日志文件。
選項:?
- -f 循環讀取
- -n<行數> 顯示行數
[root@hcss-ecs-f571 ~]# tail temp.txt
hello 1991
hello 1992
hello 1993
hello 1994
hello 1995
hello 1996
hello 1997
hello 1998
hello 1999
hello 2000
[root@hcss-ecs-f571 ~]#
[root@hcss-ecs-f571 ~]# tail -4 temp.txt
hello 1997
hello 1998
hello 1999
hello 2000
[root@hcss-ecs-f571 ~]#
用管道對數據進行操作:顯示文件[250, 300]
[root@hcss-ecs-f571 ~]# head -250 temp.txt | tail -50
hello 200
hello 201
hello 202
hello 203
hello 204
hello 205
hello 206
hello 207
hello 208
hello 209
hello 210
hello 211
hello 212
hello 213
hello 214
hello 215
hello 216
hello 217
hello 218
hello 219
hello 220
hello 221
hello 222
hello 223
hello 224
hello 225
hello 226
hello 227
hello 228
hello 229
hello 230
hello 231
hello 232
hello 233
hello 234
hello 235
hello 236
hello 237
hello 238
hello 239
hello 240
hello 241
hello 242
hello 243
hello 244
hello 245
hello 246
hello 247
hello 248
hello 249
十五、data指令
1.在顯示方面:可以設定欲顯示的格式,格式設定為?個加號后接數個標記,其中常用的標記列表如下:
- %H:小時(00..23)
- ?%M:分鐘(00..59)
- ?%S:秒(00..61)
- ?%X:相當于%H:%M:%S
- ?%d:日(01..31)
- ?%m:月份(01..12)
- ?%Y:完整年份(0000..9999)
- ?%F:相當于%Y-%m-%d
2.在設定時間方面:
- date-s//設置當前時間,只有root權限才能設置,其他只能查看。
- ?date-s20080523//設置成20080523,這樣會把具體時間設置成空00:00:00
- ?date-s01:01:01//設置具體時間,不會對日期做更改
- ?date-s“01:01:012008-05-23″//這樣可以設置全部時間
- ?date-s“01:01:0120080523″//這樣可以設置全部時間
- ?date-s“2008-05-2301:01:01″//這樣可以設置全部時間
- ?date-s“2008052301:01:01″//這樣可以設置全部時間
3. 時間戳
- 時間->時間戳:date+%s
- ?時間戳->時間:date-d@1508749502
- ?Unix時間戳(英?為Unixepoch,Unixtime,POSIXtime或Unixtimestamp)是從1970年1?1 ?(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒
[root@hcss-ecs-f571 ~]# date
Wed Jul 30 17:53:13 CST 2025
[root@hcss-ecs-f571 ~]# date %Y
date: invalid date ‘%Y’
[root@hcss-ecs-f571 ~]# date +%Y
2025
[root@hcss-ecs-f571 ~]# date %Y
date: invalid date ‘%Y’
[root@hcss-ecs-f571 ~]# y
-bash: y: command not found
[root@hcss-ecs-f571 ~]# date +%Y/%m/%d-%H:%M:%S
2025/07/30-17:54:41
[root@hcss-ecs-f571 ~]# date +%Y/%m/%d-%H:%M:%S -d @0
1970/01/01-08:00:00
[root@hcss-ecs-f571 ~]#
十六、cal指令
????????cal命令可以用來顯示公歷(陽歷)日歷。
公歷是現在國際通用的歷法,又稱格列歷,通稱陽歷。
“陽 歷”又名“太陽歷”,系以地球繞行太陽一周為一年,為西方各國所通用,故又名“西歷”。
命令格式:cal 參數 [年份]
功能:用于查看日歷等時間信息,如只有一個參數,則表示年份(1-9999),如有兩個參數,則表示月份和年份
常用選項:
- -3顯示系統前?個月,當前月,下一個月的月歷
- -j 顯示在當年中的第幾天(?年日期按天算,從1月1號算起,默認顯示當前月在?年中的天數)
- -y 顯示當前年份的日歷
[root@hcss-ecs-f571 ~]# cal -y2025 January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 1 15 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 2930 31April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 5 1 2 3 1 2 3 4 5 6 76 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 5 1 2 1 2 3 4 5 66 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 3031October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 1 1 2 3 4 5 65 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 3130[root@hcss-ecs-f571 ~]# cal -jJuly 2025
Sun Mon Tue Wed Thu Fri Sat182 183 184 185 186
187 188 189 190 191 192 193
194 195 196 197 198 199 200
201 202 203 204 205 206 207
208 209 210 211 212[root@hcss-ecs-f571 ~]#