1. 程式人生 > >Linux系統的基礎學習

Linux系統的基礎學習

egrep 字符 module centos tor ica love ted 固定

Linux系統學習筆記


以下所有操作為centos6.7環境
一、創建目錄:mkdir
命令:mkdir data
創建遞歸目錄參數[-p],如可創建 a/b/c
命令:mkdir -p a/b/c
[root@localhost ~]# mkdir data #創建目錄
[root@localhost ~]# ll
drwxr-xr-x. 2 root root 6 4月 24 15:49 data
[root@localhost ~]# mkdir -p /data/test1/ # “-p”遞歸

二、創建空文件:touch
命令:touch /data/test.txt (在data目前下創建test.txt文件)

或直接在data目錄下創建:touch test.txt或echo >test.txt
同時建多個文件:touch 1.txt 2.txt 3.txt或touch {a,b,c,}.txt
批量刪除:rm -rf [1-9,a-z].txt
[root@localhost ~]# touch test.txt #創建空文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 0 4月 24 15:52 test.txt

三、編輯文件內容:vi
命令:vi [file] ,然後按i ,編輯完後按[:wq]強制保存並退出
方法一:
[root@localhost ~]# vi test.txt #編輯文件

或echo “i am stduying linux” >test.txt
[root@localhost ~]# echo "i am stduying linux" >test.txt #將"i am stduying linux"重定向到test.txt
[root@localhost ~]# cat test.txt #查看文件
i am stduying linux
[root@localhost ~]#
說明:“>” 為重定向,箭頭指向的文件原內容覆蓋
方法二:
[root@localhost ~]# echo "I love you" >>test.txt #說明:“>>”直接在文件後面追加
[root@localhost ~]# cat test.txt
i am stduying linux
I love you
[root@localhost ~]#
方法三:
[root@localhost ~]# cat >>test.txt <<EOF #“EOF”可為任何字符,但語句結束時需一致

I am stduying linux
I love you
3Q

EOF #“EOF”可為任何字符,但需與語句開頭的一致
[root@localhost ~]# cat test.txt
I am stduying linux
I love you
3Q

[root@localhost ~]#

四、拷貝文件:cp [源文件或目錄] [目的目錄]
將test.txt文件拷貝到tmp目錄下
[root@localhost ~]# cp test.txt /tmp/
[root@localhost ~]# ls /tmp/
test.txt
[root@localhost ~]#
說明:拷貝目錄時加參數“ -a、-p”例如:cd -ap /data /tmp/
cmd.run "cp -r /opt/se/modules/ /opt/se/modules.bak20180514"
[root@0 salt]# scp /opt/salt/history.py [email protected]:~ //192.169.4.67:為目的IP

五、文件或目錄的移動:mv
命令:mv test.txt /root/
[root@localhost ~]# mv test.txt /root/data/
[root@localhost ~]# ls /root/data/
test.txt
[root@localhost ~]#
刪除文件或目錄
命令:rm -rf test.txt
[root@localhost ~]# rm -rf test.txt

六、過慮查看文件內容:grep、awk、sed
問題:查看test.txt文件裏的除了oldboy內容
例1:
[root@localhost ~]# cat test.txt | grep -v “oldboy”
test
ll
[root@localhost ~]#
說明:cat查看;“|”相當管道作用;“-v”排除後面內容
例2:
[root@localhost ~]# grep -v oldboy test.txt -----“grep”過慮
test
ll
[root@localhost ~]#
例3:
[root@localhost ~]# head -2 test.txt #“head”顯示,“-2”顯示2行,默認是顯示10行
test
ll
[root@localhost ~]#

七、查看系統的命令別名:alias “unalias”取消別名
[root@localhost data]# alias
alias cp=‘cp -i‘
alias egrep=‘egrep --color=auto‘
alias fgrep=‘fgrep --color=auto‘
alias grep=‘grep --color=auto‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@localhost data]#

八、查看test.txt文件的第20至30行內容 “seq”生成行數;“tail”查看最後多少行;“head”查看最前多少行
[root@localhost ~]# seq 100 >test.txt
例1:
[root@localhost ~]# head -30 test.txt | tail -11
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#
例2:
[root@localhost ~]# sed -n ‘20,30p‘ test.txt #“p”打印,“-n”取消默認輸出
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#
例3:
[root@localhost ~]# awk ‘{if(NR>19 && NR <31) print $1}‘ test.txt
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#

九、查找命令:find
[root@test ~]# find / -name wxli
/root/wxli
[root@test ~]

十、創建用戶
[root@localhost ~]# useradd lt #創建lt用戶
[root@localhost ~]# passwd lt #給lt用戶建立密碼
Changing password for user lt.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
方法2: “--stdin”為passwd的參數
[root@localhost ~]# echo "zaq1.;[="| passwd --stdin lt
Changing password for user lt.
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
[root@localhost ~]# useradd libin -u 560 #創建libin用戶加固定ID:560
[root@localhost ~]# tail -1 /etc/group #查看用戶
libin:x:560:
[root@localhost ~]#


Linux系統的基礎學習