歡迎您光臨本站 註冊首頁

linux01-初識

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

桌面環境有 gnome 和 kde 這兩種

ctrL alt F1~F6 文字終端界面 tty

ctrl alt f7 圖形終端


虛擬機 多加一個shift鍵


alt F2 可以用命令打開程序
<---gnome-terminal
<---konsole


ctrl shift 字母t鍵 是在一個終端上再多打開一個終端 pts
alt 1 alt 2 alt 3 是分別在一號,二號,三號終端上快速切換


pts 和 tty的區別

------------------------------------------------------


用戶


bash 終端相當於是bash的介面和工具 cat /etc/shells
| bash-3.2-24.el5.i386.rpm
|
內核kernel 1,系統介面調用 www.kernel.org
| 2,內存管理
| 3,文件系統管理
| 4,進程管理
| 5,硬體驅動
|
物理硬體

內部命令: bash 自帶的命令


man cd
bash, :, ., [, alias, bg, bind, break,
builtin, cd, command, compgen, complete,
continue, declare, dirs, disown, echo,
enable, eval, exec, exit, export, fc, fg,
getopts, hash, help, history, jobs, kill,
let, local, logout, popd, printf, pushd,
pwd, read, readonly, return, set, shift,
shopt, source, suspend, test, times,
trap, type, typeset, ulimit, umask, una-
lias, unset, wait - bash built-in com-
mands, see bash(1)


外部命令: 安裝外部軟體所帶的命令


man --注重於應用

info --注重於介紹

command -h
command --help

help command


[root@localhost etc]# ls /usr/share/man/


man1 普通用戶可執行的命令和二進位文件

Empire CMS,phome.net


man2 系統核心調用的函數工具
man3 常用的一些函數與函數庫
man4 設備文件的說明
man5 設置文件與一些其它格式的文件
man6 遊戲相關
man7 協議有關,網路協議等
man8 管理員可操作的命令
man9 系統內核相關的文件


[root@li ~]# which ls
alias ls='ls --color=tty'
/bin/ls
[root@li ~]# rpm -qf /bin/ls
coreutils-5.97-23.el5
[root@li ~]# rpm -ql coreutils-5.97-23.el5


================================


文件命令:

ls 列出目錄的內容 同dos里的dir
ls -a 列出隱藏文件(就是以.開頭的文件,包括.(當前目錄)和..(上一層目錄))
ls -l 詳細列表
ls -l -a
ls -la
ls -ld /etc


ls / --有顏色
/bin/ls / --沒顏色
dir / --沒顏色

--原因是ls 被做了別名
[root@li ~]# which ls
alias ls='ls --color=tty'
/bin/ls

想自己做一個別名
# alias dir='dir --color'
# dir / --就有顏色了

ls -ld /root/
drwxr-x--- 27 root root 4096 Mar 4 10:17 /root/

文件類型 許可權 鏈接數 擁有者 擁有組 大小 最后修改時間 文件名


文件的分類:
- 普通文件
d 目錄 directory
l 鏈接文件 link
p 管道文件 pipe
b block文件,塊設備
c 字元 character
s 套接字文件socket


用ls對一個目錄大小由大到小進行排序列表 -S
按時間由最新時間到最老時間排序列表 -t


mkdir /test
cd /test

touch 1

touch {1,2,3}aa{4,5,6}bb{7,8,9} --創建27個空文件
touch “{1,2,3}aa{4,5,6}bb{7,8,9}“ --創建1個空文件

Empire CMS,phome.net


---------------------

stat 1
Access: 2011-06-27 14:11:39.000000000 0800
Modify: 2011-06-27 14:11:39.000000000 0800
Change: 2011-06-27 14:11:39.000000000 0800

touch 1 --再創建1,不會報“文件已經存在的錯誤"只是改變了文件的時間相關的屬性

stat 1
Access: 2011-06-27 14:12:03.000000000 0800
Modify: 2011-06-27 14:12:03.000000000 0800
Change: 2011-06-27 14:12:03.000000000 0800


touch {2..10} --創建2到10一共9個文件

touch {a..z}
touch {A..Z}

========================================================


目錄命令:
pwd cd mkdir rmdir


[root@li /]# cd /var/mail/
[root@li mail]# pwd -P
/var/spool/mail
[root@li mail]# pwd
/var/mail


[root@li mail]# ll -di /var/mail/
196459 drwxrwxr-x 2 root mail 4096 Jun 27 11:05 /var/mail/
[root@li mail]# ll -di /var/spool/mail/
196459 drwxrwxr-x 2 root mail 4096 Jun 27 11:05 /var/spool/mail/
--這兩個目錄在物理上屬於同一個位置


cd

絕對路徑 --相對於根目錄的路徑
相對路徑 --相對於當前目錄的路徑

區分方法 --就看路徑最左邊是否有/

[root@li doc]# pwd
/usr/share/doc
[root@li doc]# cd /usr/share/doc/man-pages-2.39/ --絕對
[root@li doc]# cd man-pages-2.39/ --相對

cd ~ --cd到當前登錄用戶的家目錄


mkdir 建一個空目錄


鏈接數,目錄默認為2(指的是.和..),文件默認為1;
一個目錄的鏈接數可以看成2+子目錄數(但只有一層,也就是說子目錄下的子目錄不算)

mkdir aaa/aaa/aaa/aaa/aaa/aaa/aaa -p

rmdir 刪除空目錄(非空目錄不能刪除)


#!/bin/bash

Empire CMS,phome.net

mkdir {1..10000}
while true
do
cd 1
mkdir {1..10000}
done

=================================


閱讀命令:

cat more less head tail

cat -n /etc/inittab --空行也算一行
nl /etc/inittab --空行不算


more 翻頁查看內容
--回車向下翻行,空格向下翻頁,q鍵退出

less 比more更靈活
--方向鍵和page up,page down 都可以實現上翻下翻


head 默認顯示文件的前十行

tail 默認顯示文件的后十行


tail -50 /var/log/messages --查看文件的最后50行
tail -f /var/log/messages --動態監控文件的最后十行

watch 接任何運行的程序,默認每兩秒執行一次
watch -n 5 --指定每5秒監控一次

-------------------------------------

操作命令

cp mv rm


cp 源 目的

[root@localhost ~]# cp /share/soft/iso/rhel-5.4-server-i386-dvd.iso /root/Desktop

-r 拷目錄
-a 保留屬性(擁有者,擁有組)


mv 移動,或者重命名
mv 目錄不需要加-r參數,同時它會自動保留屬性


--把所有的.html結尾的文件批量重命名為.php結尾的文件
touch {1..100}.html

man rename --查看幫助

# rename .html .php *.html

注意: 通配符 *
把/etc/skel/目錄下所有的文件目錄(不包括skel這個目錄)拷貝到一個空目錄

錯誤一:目錄下都是有.開頭的隱藏文件
cp /etc/skel/* /test4 -r
cp: cannot stat `/etc/skel/*': No such file or directory


錯誤二:.*包含.. 也就是上一級目錄,這樣拷的話會把/etc/下的所有東西給拷過去

Empire CMS,phome.net


cp /etc/skel/.* /test4/ -r

rm 刪除文件或目錄

rm /test4/* -rf

[root@li test3]# rm $ORACLE_HOME/ -rf
--以root用戶去刪除的話,就相當於是刪除/

轉義符

rm \{1\,2\}aa\{3\,4\}bb\{5\,6\}
rm 123\ --空格也需要轉義


[root@localhost ~]# touch /test/1 23
[root@localhost ~]# ls /test/
1 --23在當前目錄
[root@localhost ~]# touch /test/1\ 23
[root@localhost ~]# ls /test/
1 1 23


--在目錄內,把本目錄刪除的情況
[root@localhost test]# rm /test/ -rf
[root@localhost test]# ls /test
ls: /test: No such file or directory
[root@localhost test]# touch 1
touch: cannot touch `1': No such file or directory


===============================


管道 --一條命令完成不了的任務或者要多條複雜的命令來實現的任務,可以用管道來實現
cat /etc/rc.sysinit |more 等於 more /etc/rc.sysinit
cat /etc/rc.sysinit |grep fi |wc -l
grep root /etc/passwd |wc -l

重定向
cat /etc/rc.sysinit |grep fi |wc -l|cat >> /root/test/1 --追加
echo aa > /root/test/1 --覆蓋


用一條命令實現建立一個/root/test文件,並且寫上內容為haha

[root@li /]# echo haha > /root/test
[root@li /]# cat /root/test
haha
[root@li /]# echo hehe > /root/test
[root@li /]# cat /root/test
hehe
[root@li /]# echo hoho >> /root/test
[root@li /]# cat /root/test
hehe
hoho
[root@li /]# sed -i '2s/hoho/hohohaha/' /root/test
[root@li /]# cat /root/test
hehe
hohohaha

把/etc/passwd里最后三行的內容給追加到/root/test

Empire CMS,phome.net


# tail -3 /etc/passwd >> /root/test

=============================================


# ps -ef |grep httpd | awk '{print $2}' |xargs kill -9


[root@localhost test]# ls /test/
1 1 23
嘗試一:
[root@localhost test]# ls /test/* | rm -rf --刪除不了

嘗試二:
[root@localhost ~]# ls /test/ | xargs rm -rf
[root@localhost ~]# ls /test/
1 1 23
--刪除不了,前面的結果里沒有絕對路徑,如果這條命令在cd /test目錄之後再使用,結果就不一樣了

嘗試三:
[root@localhost ~]# ls /test/*
/test/1 /test/1 23
[root@localhost ~]# ls /test/* |xargs rm -rf
[root@localhost ~]# ls /test/
1 23
--發現1\ 23這個文件沒有刪除掉,xargs不會幫你去轉義

================================

一條命令把/etc下的所有.conf結尾的文件cp一份到/test目錄下
cp /etc/*.conf /test/ --這樣只能拷貝/etc/目錄下一層的*.conf

find /etc -name "*.conf" -exec cp {} /test \;
--這樣是把/etc/目錄包括子目錄下所有的*.conf拷到/test下


一條命令刪除/tmp目錄下有root關鍵字的文件
cd /tmp
rm *root* -rf

ls /tmp/* |grep root |xargs rm -rf --在/tmp目錄下不適用,ls /tmp/*得到的結果里有:符號


[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls |grep root |xargs rm -rf


=======================

開機時 卡在啟動sendmail服務時,一般就是主機名的問題.這種情況就只有等待,進去后把主機名對應好;
或者是輸入用戶名和密碼登錄系統慢
或者是進入系統后,打開終端或者firefox都很慢

Empire CMS,phome.net


--一般來說都是主機名的問題

hostname li.cluster.com (xx.xx.com是FQDN,full qualified domain name)
vim /etc/hosts --按I鍵進入插入模式
172.19.1.44 li.cluster.com --寫上IP和主機名的對應
--寫完后,按ESC鍵退出插入模式.按大寫Z鍵兩次 (如果沒開大寫燈,就shift zz)就是保存退出

vim /etc/sysconfig/network
HOSTNAME=li.cluster.com --這個一樣得改


--如果主機名對應好之後,還是慢,那麼檢查一下/tmp目錄裡面是否有相關的文件
--仍然慢的話,就建議做下面的步驟:
rm /root/.gnome -rf
rm /root/.gnome2 -rf

cd /tmp
rm *root* -rf

--做完上面的步驟后,reboot命令重啟你的電腦

=============================================


一條命令把/etc下(不包含子目錄)的所有.conf結尾的文件名保存到/root/Desktop/name文件里,要求使用一行輸出


ls /etc/*.conf|xargs echo > /root/Desktop/name

一條命令把/etc下(不包含子目錄)的所有.conf結尾的文件名保存到/root/Desktop/name2文件里,但要求名字的順序是以文件從大到小排列的,要求一個名字一行

ls -S /etc/*.conf > /root/Desktop/name2

============================================================

vi
vim


--由兩個不同的軟體包所提供
[root@li /]# rpm -qf `which vi`
vim-minimal-7.0.109-6.el5
[root@li /]# rpm -qf `which vim`
vim-enhanced-7.0.109-6.el5

vi vim (增強版vi,現在基本各種linux版本都帶有vim)

cp /etc/passwd /tmp/
vim /tmp/passwd --現在打開,沒有顏色,passwd不是在它應該在的路徑,vim是可以區分出來的


vi 有三種模式:

1,一般普通模式:

Empire CMS,phome.net


2,編輯模式: i I o O a A r R
3,命令行模式: : / ?


移動相關:
--ctrl b 向上翻頁 = page up
--ctrl f 向下翻頁 = page down
ctrl d 向下移動半頁
ctrl u 向上移動半頁
--G 移動到頁末 =shift g
--gg 移動到頁頭
--0 移動到行頭 = home
--$ 移動到行末 = end
n(代表數字)+回車 向下移動N行

刪除,複製,粘貼
x 向後刪除一個字元 = delete
X 往前刪除一個字元 = backspace
--dd 直接刪除游標所在行 ndd (n代表數字,刪除n行)
--yy 複製游標所在行 nyy (n代表數字,複製n行)
--p 粘貼
--u 回退上一次操作 按一次u只能回退一次
ctrl r 重做上一次操作


進入編輯模式:
--i 當前insert
I 行頭insert
a 向後一個字元insert
A 行末insert
--o 下一行insert
O 上一行insert
r 替換游標所在的字元
R 從游標處向後一直替換
--ESC 從編輯模式回退到一般模式

查找與替換:
-- /word 查找單詞 n 向下查找 N 向上查找
-- :1,$s/old/new/g 從第一行到最后一行都用new替換old,g表示行里所有匹配的都替換
-- :%s/old/new/g 從第一行到最后一行都用new替換old

-- :n1,n2s/old/new/g 從n1行到n2行用new替換old
-- :1,$s/old/new/gc 從第一行到最后一行都用new替換old,多加了一個c就可以要求用戶確認哪些換哪些不換


--:set number 或者 : set nu 把每一行都加上行號
--:set nonumber 或者 : set nonu 去掉每一行前的行號

http://10.1.1.35/li@user1~/abc
http://10.1.1.35/li@user1~/abc
http://10.1.1.35/li@user1~/abc

Empire CMS,phome.net


http://10.1.1.35/li@user1~/abc
把上面四行替換成和下面這行內容
file:///172.19.1.120/abc

:1,$s/http\:\/\/10.1.1.35\/li@user1\~\/abc/file\:\/\/\
/172.19.1.120\/abc/g


:n(代表數字)+回車 直接把游標定位到n行

--:w 保存 (write)
--:q 退出 (quit)
--:wq 保存並退出 = shift zz = :x
-- +! 表示強制保存或者退出

:split 橫分佈局
:vsplit 豎分佈局


--當兩個終端同時打開的話,會產生一個.swap文件
如:你打開/tmp/passwd文件,會產生/tmp/.passwd.swap
解決方法是:直接刪除掉它就可以了
rm /tmp/.passwd.swp

==============================================================


bash 的一些技巧補充

history
!748 --直接執行histroy里顯示的第748條命令

ctrl shift r
(reverse-i-search)`': 匹配模式

!$ --代表上一條命令的後面部分


history -c --清除歷史記錄


多用TAB鍵

本文出自 「linuxart」 博客,請務必保留此出處http://linuxart.blog.51cto.com/686203/843845



[火星人 ] linux01-初識已經有617次圍觀

http://coctec.com/docs/linux/show-post-45740.html