LINUX下的21個特殊符号
1 > 重定向輸出符号
用法:指令 >檔案名
特性:覆寫(當輸入檔案和輸出檔案是同一檔案,文
件内容被清空;不适合連續重定向)
eg: [root@Centos home]# ls -l /home/ > 1.txt
root@Centos home]# cat 1.txt
total 32
-rw-r--r--. 1 root root 0 Jan 22 02:16 1.txt
-rw-r--r--. 1 root root 29 Jan 22 00:55 aa.txt
-rw-r--r--. 1 root root 22 Jan 22 02:15 bb.txt
-rw-r--r--. 1 root root 31 Jan 21 05:19 home.txt
drwxrwxrwx. 2 root root 4096 Jan 19 23:18 iso
drwx------. 2 root root 16384 Jan 19 21:02 lost+found
[root@Centos home]# date >2
[root@Centos home]# cat 2
Tue Jan 22 02:17:20 CST 2013
2,>> 重定向輸出符号
用法:指令 >>檔案名(沒有就建立,有就追加)
特性:追加
eg:[root@Centos home]# date >>2
Tue Jan 22 02:17:20 CST 2013
Tue Jan 22 02:19:00 CST 2013
3,2> 錯誤重定向輸出符号
用法:指令 2>檔案名
特性:覆寫
eg:[root@Centos home]# vim a.txt
[root@Centos home]# cat a.txt
qwe
asd
zxc
123
4,2>> 錯誤重定向輸出符号
用法:指令 2>>檔案名
特性:錯誤資訊的追加
典型應用:指令 >檔案名 指令 2>檔案名
指令 >/dev/null 2>/dev/null==指令 >
/dev/null
5,| 管道符号
用法:指令1 | 指令2
機制:上一個的指令輸出作為下一個指令的輸入
eg:
[root@Centos etc]#eg:ls -l | more
分頁顯示(隻能向下翻頁,按空格space鍵盤,向下翻一頁,按enter鍵向下翻一行,直接一
直按空格直到結束)
[root@Centos etc]#ls -l | less
分頁顯示:(按空格space鍵盤向下一頁一頁的翻,按箭頭向上的鍵則向上一行一行的翻,按
箭頭向下的鍵則向下一行一行的翻,按enter鍵向下翻一行,到最後顯示end時,
敲一下q鍵退出。或者在中途按q鍵也是退出。)
[root@Centos ~]# ls -a | grep bash
.bash_history
.bash_logout
.bash_profile
.bashrc
grep用來從一個檔案中找出比對指定關鍵字的那一行,并送到标準輸出,結合管道,我們
通常用它來過濾搜尋結果.
6, * 比對任意字元
eg: home]# touch abc*def.pdf
touch q.pdf
touch 1*1.pdf
touch *1.pdf (先建立四個檔案)
(1)find /home/*.pdf (可通過後面的字尾名來查找,但是linux的字尾名不一定就是你看 見的)
[root@Centos /]# find /home/*.pdf
/home/1*1.pdf
/home/*1.pdf
/home/abc*def.pdf
/home/q.pdf
(2)find /home/*\**.pdf(這個比對中間有一個*,*的前後随便有幾個字元也包括0個都可以)
[root@Centos /]# find /home/*\**.pdf
(3) find /home/\**.pdf(這個是比對第一個字元為*,後面随便有幾個字元)
[root@Centos /]# find /home/\**.pdf
(4)find /home/*\*1.pdf(這個是比對有一個*,*前面随便幾個字元,*的後面是1的)
[root@Centos /]# find /home/*\*1.pdf
7,? 比對任意一個字元
eg:
[root@Centos home]# ls
ab.txt a.txt ba.txt b.txt iso lost+found
[root@Centos home]# find /home/?.txt
/home/a.txt
/home/b.txt
8,& 背景程序符
用法:指令(程式) &
9,&& 邏輯與
用法:指令1 && 指令2
機制:如果指令1執行成功,繼續執行指令2;否則,
不執行指令2
eg:
[root@Centos ~]# date > 2.txt
[root@Centos ~]# date > 2.txt && cat 2.txt
Wed Jan 23 07:49:36 CST 2013
10,|| 邏輯或
用法:指令1 || 指令2
機制:如果指令1執行成功,不執行指令2;否則,
才執行指令2
11,! 邏輯非
機制:排除指定範圍
12,[x-y] 指定範圍
13,# 注釋
eg:當你打開一個shell腳本的時候,第一句基本是#!/bin/bash
井号也常出現在一行的開頭,不會被執行。
當某條指令不想被執行的時候,隻需在該行的前面加上#即可。
#ls(則表示ls指令不會執行)
echo "aaa" >>liu;#cat liu(則僅此cat指令不會執行,前面的指令還是會執行)
14,"" 雙引号
機制:把它所包含的内容作為普通字元,但‘’\
$ `` 除外
[root@Centos /]# echo -e "your system is init done.\nplease reboot"
your system is init done.
please reboot
[root@Centos ~]# echo "`date`"
Wed Jan 23 07:51:49 CST 2013
[root@Centos ~]# echo "$LANG"
en_US.UTF-8
15‘’ 單引号
機制:把它所包含的内容作為普通字元,無例外
16,`` 倒引号(在tab鍵的上面)
機制:執行它所包含的内容,他包含的是指令
注意,需要結合引号一起用。
[root@Centos home]# `date`
-bash: Tue: command not found
[root@Centos home]# date
Tue Jan 22 10:20:44 CST 2013
[root@Centos home]# echo "now,it's `date`"
now,it's Tue Jan 22 10:21:17 CST 2013
[root@Centos home]# echo "now,it's 'date'"
now,it's 'date'
[root@Centos home]# echo "now,it's date"
now,it's date
17,\ 轉義字元
用法; \符号
機制:把符号的特定含義去掉,使其變成普通标點
符号
18,$ 變量調用符号
用法: $變量
機制:調用變量,進而得到‘變量的值’
[root@Centos ~]# LI=date(先定義變量,定義變量必須是大寫)
[root@Centos ~]# $LI(調用變量)
Wed Jan 23 08:08:11 CST 2013
19,; 指令分隔符
用法:指令1 ; 指令2
機制;一行語句中,順次執行各指令
eg:date >>myfile;cat myfile(分号前面有空格和沒有空格都一樣)
20() 整體執行
[root@Centos home]# (cd ~;VC=`pwd`;echo $VC)
/root
21,{ } 變量分離
22,< 重定向輸入符号
用法:指令 <檔案名
輸入重定向
輸入重定向是指把指令(或可執行程式)的标準輸入重定向到指定的檔案中。也就是說,輸入可以不來自鍵盤,而來自一個指定的檔案。是以說,輸入重定向主要用于改變一個指令的輸入源,特别是改變那些需要大量輸入的輸入源。
例如,指令wc統計指定檔案包含的行數、單詞數和字元數。如果僅在指令行上鍵入:
wc
wc将等待使用者告訴它統計什麼,這時shell就好象死了一樣,從鍵盤鍵入的所有文本都出現在螢幕上,但并沒有什麼結果,直至按下<ctrl+d>,wc才将指令結果寫在螢幕上。
eg:1
[root@Centos home]# wc(自己随便輸入)
ziji suibian shuru
123 122
2233 (輸入完之後按住crtl+d)
3 6 32(告訴你有幾行,幾個單詞,幾個字元)
eg:2
[root@Centos /]# wc /etc/passwd(wc指令後面可以接檔案,意思就是統計檔案内容的個數)
29 45 1394 /etc/passwd
[root@Centos /]# wc /home/aa.txt
1 6 29 /home/aa.txt
本文轉自 jie783213507 51CTO部落格,原文連結:http://blog.51cto.com/litaotao/1187983,如需轉載請自行聯系原作者