1.Linux基礎
[root@VM_0_9_centos ~]#
root:當前登錄主機的用戶名
VM_0_9_centos:當前登錄主機的主機名
~:當前所處的工作目錄(這裏表示root用戶的家目錄)
$:普通用戶類型
#:根用戶類型
小試牛刀
查看登錄用戶名:whoami
[root@VM_0_9_centos ~]# whoami
root
whoami --help
[root@VM_0_9_centos ~]# whoami --help
Usage: whoami [OPTION]...
Print the user name associated with the current effective user ID.
Same as id -un.
--help display this help and exit
--version output version information and exit
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'whoami invocation'
輸出:echo
[root@VM_0_9_centos ~]# echo “hello”
“hello”
文本編輯器:nano
進入nano界面
ctrl + O :保存
ctrl + G :查看幫助文檔
ctrl + X :退出
歷史命令:按上下箭頭(↑,↓)查看歷史命令
命令補齊:輸入命令(不全)按Tab進行自動補全,按兩下Tab會輸出所有可能的命令
使用幫助文檔
man幫助文檔
man who
WHO(1) User Commands WHO(1)
NAME
who - show who is logged on
SYNOPSIS
who [OPTION]... [ FILE | ARG1 ARG2 ]
DESCRIPTION
Print information about users who are currently logged in.
-a, --all
same as -b -d --login -p -r -t -T -u
-b, --boot
time of last system boot
-d, --dead
print dead processes
-H, --heading
print line of column headings
-l, --login
print system login processes
--lookup
attempt to canonicalize hostnames via DNS
-m only hostname and user associated with stdin
-p, --process
print active processes spawned by init
-q, --count
all login names and number of users logged on
-r, --runlevel
print current runlevel
-s, --short
print only name, line, and time (default)
-t, --time
print last system clock change
-T, -w, --mesg
add user's message status as +, - or ?
-u, --users
list users logged in
--message
same as -T
Manual page who(1) line 1 (press h for help or q to quit)
在man命令中:
上下箭頭:向上向下滾動
pageup和pagedown:上下翻頁
/strings:從光標處開始搜索,n下一個匹配處,N上一個匹配處
q:退出
現在時間
date
[root@VM_0_9_centos tmp]# date
Wed Jul 11 15:11:14 CST 2018
2.管好文件
2.1創建和刪除文件夾
樹形目錄
pwd:顯示當前完整的工作目錄
[root@VM_0_9_centos ~]# pwd
/root
cd:切換目錄
[root@VM_0_9_centos ~]# cd /tmp
[root@VM_0_9_centos tmp]#
“.”:當前目錄
“..”:當前目錄的上層目錄
“-”:表示前一個工作目錄
“~”:表示當前用戶的家目錄
mkdir:創建目錄
[root@VM_0_9_centos tmp]# mkdir /tmp/demo01
-p 創建嵌套目錄
[root@VM_0_9_centos tmp]# mkdir -p /tmp/demo02/pages/main
rmdir:刪除目錄
[root@VM_0_9_centos tmp]# rmdir demo01
只能刪除空目錄
[root@VM_0_9_centos tmp]# rmdir demo02
rmdir: failed to remove ‘demo02’: Directory not empty
2.2列出文件
ls
[root@VM_0_9_centos tmp]# ls
demo02 systemd-private-85c529a253624597917be650012790f3-ntpd.service-SV1ZH2
-a:列出所有文件
-l:列出詳細屬性
-i:列出Inodeid值
[root@VM_0_9_centos tmp]# ls -al
total 36
drwxrwxrwt. 9 root root 4096 Jul 11 12:38 .
dr-xr-xr-x. 20 root root 4096 Jul 11 12:39 ..
drwxr-xr-x 3 root root 4096 Jul 11 12:06 demo02
drwxrwxrwt. 2 root root 4096 Jan 9 2018 .font-unix
drwxrwxrwt. 2 root root 4096 Jan 9 2018 .ICE-unix
drwx------ 3 root root 4096 Mar 26 00:08 systemd-private-85c529a253624597917be650012790f3-ntpd.service-SV1ZH2
drwxrwxrwt. 2 root root 4096 Jan 9 2018 .Test-unix
drwxrwxrwt. 2 root root 4096 Jan 9 2018 .X11-unix
drwxrwxrwt. 2 root root 4096 Jan 9 2018 .XIM-unix
1、3、4字段決定了文件的訪問屬性
1:類型和權限
2:文件的擁有者
3:文件從屬用戶組
第一個字段詳解:
文件的類型,文件擁有者,文件從屬用戶組,其他用戶
文件的類型:
d:目錄文件
-:普通文件
c:字符設備文件
b:塊設備文件
l:符號鏈接文件
權限:
r:讀
w:寫
x:執行
切換用戶
su - nboocer01
修改文件權限
chmod 700 hello.sh
修改文件的擁有者和從屬用戶組
chowm nbcc:stu hello.sh
chowm nbcc hello.sh
chowm :stu hello.sh
2.3創建和刪除文件
拷貝文件
cp [要拷貝的文件|文件列表][目的文件名|目的目錄]
[root@VM_0_9_centos ~]# cp hello.txt /tmp
[root@VM_0_9_centos ~]# cd /tmp/
[root@VM_0_9_centos tmp]# ls
demo01 demo02 hello.txt
拷貝並重命名
[root@VM_0_9_centos ~]# cp hello.txt /tmp/hello1.txt
同事拷貝多個文件
[root@VM_0_9_centos tmp]# cp hello.txt hello1.txt /tmp/demo01
拷貝目錄
-r:拷貝目錄
注意:拷貝文件時,目的文件夾有同名的文件,一個同名文件會詢問是否覆蓋,多個就麻煩了
\cp -f /tmp/hello.txt /tmp/hello1.txt /tmp/demo01
在文件的拷貝過程中,文件的所有者,從屬用戶組可能發生改變,如果要完完全全拷貝文件
-p:原原本本拷貝文件(登錄用戶,對文件有寫權限)
移動文件
mv [要拷貝的文件|文件列表][目的文件名|目的目錄]
重命名文件,路徑不變,改名字
[root@VM_0_9_centos tmp]# mv hello1.txt hello2.txt
刪除文件
rm
-R:刪除文件夾以及文件夾下文件
-f:不詢問直接刪除
[root@VM_0_9_centos tmp]# rm demo01/hello.txt
rm: remove regular file ‘demo01/hello.txt’? y
刪除文件夾以及文件夾下面的文件
[root@VM_0_9_centos tmp]# rm -R demo01
rm: descend into directory ‘demo01’? y
rm: remove regular file ‘demo01/hello1.txt’? y
rm: remove directory ‘demo01’?
不詢問直接刪除
[root@VM_0_9_centos tmp]# rm -Rf demo02
創建文件
touch hello1.txt
如果創建的文件已經存在,touch會改變這個文件的文件時間戳屬性
2.4創建硬鏈接和符號鏈接
符號鏈接文件:和windows中的快捷方式一樣
創建符號鏈接
ln -s [鏈接指向的文件][鏈接名]
ln -s /tmp/hello.txt /tmp/hello_slink
查看兩個文件
lrwxrwxrwx 1 root root 14 Jul 11 13:57 hello_slink -> /tmp/hello.txt
-rw-r--r-- 1 root root 10 Jul 11 13:04 hello.txt
通過比較,我們可以發現,雖然符號鏈接文件能夠讀寫執行鏈接的文件。但是兩個文件並不是一樣的。
如果符號鏈接的目標文件被刪除了,那麼符號鏈接就會失效。
創建硬鏈
ln /tmp/hello.txt /tmp/hello_hl
刪除hello.txt
rm /tmp/hello.txt
注意:不要給目錄創建硬鏈接,不要跨硬盤創建硬鏈接
2.5打包壓縮文件
2.5.1打包
創建打包文件
tar -cf [打包文件名][要打包的文件/列表]
-c:創建打包文件
-f:指定要創建的打包文件名(.tar結尾)
tar -cf total.tar hello.txt hello1.txt hello2.txt
[root@VM_0_9_centos demo1]# tar -cf total.tar hello.txt hello1.txt hello2.txt
[root@VM_0_9_centos demo1]# ls
hello1.txt hello2.txt hello.txt total.tar
打包後的文件將比之前的文件之和還要大,比較tar不壓縮文件
tar -cf demo1.tar demo1
[root@VM_0_9_centos tmp]# tar -cf demo1.tar demo1
[root@VM_0_9_centos tmp]# ls
demo1 hello1.txt hello_hl
demo1.tar hello2.txt systemd-private-85c529a253624597917be650012790f3-ntpd.service-SV1ZH2
列出打包文件下的所有文件
tar -tf demo1.tar
[root@VM_0_9_centos tmp]# tar -tf demo1.tar
demo1/
demo1/hello1.txt
demo1/hello2.txt
demo1/total.tar
demo1/hello.txt
刪除打包文件下的指定文件
tar -f demo1.tar --delete demo1/total.tar
[root@VM_0_9_centos tmp]# tar -f demo1.tar --delete demo1/total.tar
[root@VM_0_9_centos tmp]# tar -tf demo1.tar
demo1/
demo1/hello1.txt
demo1/hello2.txt
demo1/hello.txt
注意:delete是一個長選項,所以前面有兩個連字符
合併兩個打包文件
tar -f demo1.tar -A demo2.tar
[root@VM_0_9_centos tmp]# tar -f demo1.tar -A demo2.tar
[root@VM_0_9_centos tmp]# ls
demo1 demo2 hello1.txt hello_hl
demo1.tar demo2.tar hello2.txt
[root@VM_0_9_centos tmp]# tar -tf demo1.tar
demo1/
demo1/hello1.txt
demo1/hello2.txt
demo1/hello.txt
demo2/
demo2/hello1.txt
demo2/hello2.txt
demo2/hello.txt
demo2/hello3.txt
向打包文件中添加文件
tar -f demo2.tar -r add.txt
[root@VM_0_9_centos tmp]# tar -f demo2.tar -r add.txt
[root@VM_0_9_centos tmp]# tar -tf demo2.tar
demo2/
demo2/hello1.txt
demo2/hello2.txt
demo2/hello.txt
demo2/hello3.txt
add.txt
解包
tar -xf demo2.tar -C demo2
-C:要解包的目錄,默認是當前目錄
[root@VM_0_9_centos tmp]# ls -Rl demo2
demo2:
total 4
-rw-r--r-- 1 root root 0 Jul 11 14:32 add.txt
drwxr-xr-x 2 root root 4096 Jul 11 14:29 demo2
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello1.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello2.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello3.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello.txt
demo2/demo2:
total 0
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello1.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello2.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello3.txt
-rw-r--r-- 1 root root 0 Jul 11 14:29 hello.txt
2.5.2壓縮
創建壓縮包
gzip 文件名
[root@VM_0_9_centos tmp]# gzip add.txt
[root@VM_0_9_centos tmp]# ls
add.txt.gz demo2 hello2.txt
demo1 demo2.tar hello_hl
demo1.tar hello1.txt systemd-private-85c529a253624597917be650012790f3-ntpd.service-SV1ZH2
gzip默認會替換到源文件,並且自動添加後綴.gz
解壓
gzip -d 文件名
[root@VM_0_9_centos tmp]# gzip -d add.txt.gz
[root@VM_0_9_centos tmp]# ls
add.txt demo1.tar demo2.tar hello2.txt systemd-private-85c529a253624597917be650012790f3-ntpd.service-SV1ZH2
demo1 demo2 hello1.txt hello_hl
調節壓縮比
1~9:默認爲6
壓縮的文件大小跟壓縮比成反比
gzip -1 add.txt
[root@VM_0_9_centos tmp]# gzip -1 add.txt
[root@VM_0_9_centos tmp]# gzip -6 add1.txt
[root@VM_0_9_centos tmp]# gzip -9 add2.txt
[root@VM_0_9_centos tmp]# ls -al
total 92
drwxrwxrwt. 10 root root 4096 Jul 11 14:49 .
dr-xr-xr-x. 20 root root 4096 Jul 11 14:49 ..
-rw-r--r-- 1 root root 52 Jul 11 14:48 add1.txt.gz
-rw-r--r-- 1 root root 52 Jul 11 14:48 add2.txt.gz
-rw-r--r-- 1 root root 54 Jul 11 14:48 add.txt.gz
gzip只能壓縮單個文件,不能壓縮文件夾和同時壓縮多個文件
tar中提供-z選項,調用gzip命令
打包壓縮
tar -czf demo2.tar.gz demo2 demo2/add.txt demo2/hello.txt demo2/hello1.txt
tar -xzf demo2.tar.gz
注意-z參數的位置不能錯
2.6查找文件
locate、find
locate的用法
查找所有以.tar結尾的文件
locate .tar
/var/lib/mlocate數據庫
這個數據庫不會實時更新,使用updatedb更新這個數據庫。
find命令
find [查找範圍][查找條件][目錄]
查詢名字爲hello.txt文件(模糊查找用通配符)
find / -name hello.txt
查詢符號鏈接文件
find / -type l
查詢3天內改變的文件,並列出
find /tmp -mtime -3 -ls
查詢3天前內容發生改變的文件
find /tmp -mtime +3 -ls
查詢在3天前的那一天發生改變的文件
find /tmp -mtime 3 -ls
-amin -atime:文件讀取和執行時間相關
-cmin -ctime:文件屬性修改時間相關
-mmin -mtime:文件修改時間相關
find /tmp -size -3k
size中的單位:
c:字節
k:1024字節
M:1024k
G:1024M
查詢root用戶文件
find /tmp -user root -ls
查詢uid爲500的文件
find / -uid 500 -ls
查詢從屬用戶組爲root的文件
find /tmp -group root -ls
查詢屬於gid爲0的用戶組文件
find /tmp -gid 0 -ls
-user
-uid
-group
-gid
查詢用戶權限爲777的文件
find / -perm 777
聯合條件
-a:與
-o:或
!:非
使用聯合查詢時候,要用()括起來,並且需要轉移動作參數
-exec
find /tmp \( -size +1k -a -size -10M -a -mmin -30 -a -type f \) -exec rm -rf {} \;
-ok替換-exec
兩種都是動作參數,-ok有提示
2.7通配符
*:代表任意長度字符串
?:單個任意字符
[c1-c2]:匹配c1到c2中有序字符任意的字符
[c1,c2,c3...cn]:匹配單個字符
{c1,c2,...cn}:匹配c1到cn中無序字符集中的任意字符串
!:取反
將文件的訪問權限改爲777
chmod 777 /tmp/demo2/{hello,hello1}.txt
2.8查看文本文件
cat、
cat
將文本內容輸出在命令行(不提供翻頁),適用於查看較小文件
cat /tmp/demo1/hello.txt
less/more
less /var/log/messages
pageup,pagedown:上下翻頁
↑,↓:上下翻
/關鍵字:在文本中查找關鍵字文本(n/N)
q:退出
head/tail
查看文件的開頭或末尾的內容,一般用於查看日誌文件,默認查看10行
head /var/log/messages
3用好vi編輯器
vi是linux中標配,vim是vi的改進版
vim
一般模式 : 動作指令 (左下角無顯示)
插入模式 : 輸入字符 (左下角有插入字樣)
命令行模式 : 命令 (左下角就是冒號)
一般模式===>i===>輸入模式===>Esc===>一般模式===>:===>命令模式一般模式:
h、j、k、l:左下上右(箭頭)
x:刪除光標所在文字
dd/D:刪除一整行
J:刪除換行符
u:撤銷最後一次操作
ctrl + r:撤銷過頭恢復
:wq test.txt:保存退出
:q!:強制退出
設定行號
set 環境參數
set no環境參數
set all 查看所有參數
:set nu
:set nonu
gg:移動到文件首行
G:移動到文件末行
0:移動到當前行首
$:移動到當前行末
yy:複製光標所在行
nyy:向下複製n行
p:粘貼
可視塊
ctrl + v