失落的紀錄

2011-05-25

Linux語法使用說明

分類: Linux Scrip,Llinux — 番茄 @ 16:54

# 顯示日期

date

# 顯示今天的年月日

date +%Y%m%d

# 建立以年月日為檔名的資料夾

mkdir $(date +%Y%m%d)

# 將 date +%Y%m%d 這個字串輸入到檔案1裡面

# 因為$(date +%Y%m%d)是顯示今天的年月日,所以要加入挑脫符號

echo "$(date +%Y%m%d)">1

———————————————————————-

# 找出 syslog 的 PID

ps aux | grep ‘syslog’ | grep -v ‘grep’| awk ‘{print $2}’

# 刪除 syslog 的執行緒

kill -SIGHUP $(ps aux|grep ‘syslog’|grep -v ‘grep’|awk ‘{print $2}’)

———————————————————————-

# 將變數all清空

unset all

# 尋找etc目錄下的*.bak檔,並且將找到的檔案列表存在變數all內

all=$(find /etc -name *.bak)

# 尋找etc目錄下的*.bak檔,並且將找到的檔案,複製到tmp資料夾內

cp $(find /etc -name *.bak) /tmp

———————————————————————-

# -i:直接修改讀取的檔案內容,而不是由螢幕輸出。

# -e:直接在指令列模式上進行 sed 的動作編輯。

# 1C:取代,c 的後面接字串,這些字串可以取代 n1,n2 之間的行!

# ’1c #!/usr/bin/perl’:表示把第一行的內容取代成 #!/usr/bin/perl

sed -i -e ’1c #!/usr/bin/perl’ openwebmail*.pl

# 尋找檔案 /etc/logrotate.d/ttpd內容為 size=5M

# 將此字串,改為 size=1M

sed -i ‘s/size=5M/size=1M/’ /etc/logrotate.d/httpd

# 在檔案 123.txt 內,找到 FEATURE 該行後,

# 再下一行開始插入123456的資料,

# /a:開始插入資料

# n:表示換行

sed -i ‘/FEATURE/a123456n123′ 123.txt

在檔案 /tmp/sendmail-slackware-tls-sasl.mc 內,

找到 FEATURE(`blacklist_recipients’)dnl 該行,

然後在下一行,插入下列字串

dnl# ——-ADD-START——-

FEATURE(`enhdnsbl’, `relays.ordb.org’, `’, `t’, `127.0.0.2′)dnl

FEATURE(`dnsbl’,`dnsbl.sorbs.net’,`"554 Rejected spam as" $&{client_addr} " found in dnsbl.sorbs.net"‘)dnl

FEATURE(`enhdnsbl’, `zen.spamhaus.org’, `"Spam blocked see: http://www.abuse.net/sbl.phtml?IP="$&{client_addr}’, `t’)dnl

FEATURE(`enhdnsbl’, `bl.spamcop.net’, `"Spam blocked see: http://spamcop.net/bl.shtml?"$&{client_addr}’, `t’)dnl

define(`confCONNECTION_RATE_THROTTLE’, `50′)

define(`confMAX_DAEMON_CHILDREN’,`100′)

dnl# define(`confTLS_SRV_OPTIONS’, `V’)

dnl# ——-ADD-END———

語法如下:

(因為要便於閱讀,所以在段行符號,做斷行處理,實際執行該指令時必須要在同一行)

sed -i ‘/FEATURE(`blacklist_reci.*/adnl# ——-ADD-START——-n

FEATURE(`enhdnsbl’", `relays.ordb.org’", `’", `t’", `127.0.0.2’")dnln

FEATURE(`dnsbl’",`dnsbl.sorbs.net’",`"554 Rejected spam as" $&{client_addr} " found in dnsbl.sorbs.net"’")dnln

FEATURE(`enhdnsbl’", `zen.spamhaus.org’", `"Spam blocked see: http://www.abuse.net/sbl.phtml?IP="$&{client_addr}’", `t’")dnln

FEATURE(`enhdnsbl’", `bl.spamcop.net’", `"Spam blocked see: http://spamcop.net/bl.shtml?"$&{client_addr}’", `t’")dnln

define(`confCONNECTION_RATE_THROTTLE’", `50’")ndefine(`confMAX_DAEMON_CHILDREN’",`100’")ndnl# define(`confTLS_SRV_OPTIONS’", `V’")n

dnl# ——-ADD-END———’ /tmp/sendmail-slackware-tls-sasl.mc

# 列出 /date/backups/ 資料夾內的所有目錄

all=$(ls /date/backups/)

#將出廠值資料夾,同步資料夾,訓練資料夾刪除

echo ${all%Factory*}

#計算有幾的資料夾

echo $all|awk ‘{printer NF}’

———————————————-

a 發出BB聲

echo -e "a" 發出BB聲

———————————————-

while ["$dir" != "no" -o "$dir" != "NO"]

do

all=$(ls /date/backups/)

all=${all%Factory*}

echo $all

echo "Please input your Deltree date (YYYYMMDD ex>20090401):"

echo "Please input NO/no to exit "

read dir

dir_1=$(/data/backups/$dir)

# 1. 先看看這個目錄是否存在啊?

if [ "$dir_1" == "" -o ! -d "$dir_1" ]; then

echo "The $dir_1 is NOT exist in your system."

exit 1

fi

———————————————-

linux find 指令

ex1. 尋找此目錄下,屬性為目錄的名稱

$ find . -type d

ex2. 刪除此目錄下開頭為 "m-" 的檔案

$ find . -type f -name m-* -exec rm -f {} ;

ex3. 刪除 images/ 下面的目錄及此目錄下的檔案

$ find images/ -type d -exec rm -rf {} ;

ex4. 找出大於 6k 的檔案

$ find images/ -size +6k

$ find images/ -size +6k -exec ls -al {} ;

ex4. 找出大於 6k 檔案的數目

$ find images/ -size +6k > ~/imgCount.txt

$ wc -l ~/imgCount.txt

ex.5 找出檔案不是 m- 與 s- 開頭的檔案

$ find /home/www/htdocs/images -type f -not -name ‘m-*’ -not -name ‘s-*’

ex.6 找出 *.txt 的檔案但是不包括隱藏檔隱藏的 *.txt 檔

$ find . -type f ( -name ‘*.txt’ ! -name ‘.*’ )

find 在 man 還有其他的 operator:

Listed in order of decreasing precedence:

( expr )

Force precedence.

! expr True if expr is false.

-not expr

Same as ! expr.

expr1 expr2

And (implied); expr2 is not evaluated if expr1 is false. 隱含式的 and,先比對 expr1 再比對 expr2,比對過程中,其中一個 expr 如果回傳 false,就不再比對下去。

expr1 -a expr2

Same as expr1 expr2.

expr1 -and expr2

Same as expr1 expr2.

expr1 -o expr2

Or; expr2 is not evaluated if expr1 is true.

expr1 -or expr2

Same as expr1 -o expr2.

expr1 , expr2

List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.

所以範例五也可以寫成:

$ find /home/www/htdocs/images -type f ( ! -iname ‘m-*’ ! -iname ‘s-*’ )

SYNOPSIS

-perm mode 搜尋檔案的權限 "剛好等於 mode 的權限"

File’s permission bits are exactly mode (octal or symbolic). Since an exact match is required, if

you want to use this form for symbolic modes, you may have to specify a rather complex mode string.

For example ‘-perm g=w’ will only match files which have mode 0020 (that is, ones for which group

write permission is the only permission set). It is more likely that you will want to use the ‘/’

or ‘-’ forms, for example ‘-perm -g=w’, which matches any file with group write permission. See

the EXAMPLES section for some illustrative examples.

-perm -mode 搜尋檔案的權限 "全部要包含 mode 的權限"

當搜尋 -rwxr–r– 的檔案,也就是搜尋 0744 的檔案,如果檔案的屬性為 -rwsr-xr-x,即 4755 時,此檔案也會被列出來,因為 -rwsr-xr-x 有包括 -rwxr–r– 的屬性

All of the permission bits mode are set for the file. Symbolic modes are accepted in this form,

and this is usually the way in which would want to use them. You must specify ‘u’, ‘g’ or ‘o’ if

you use a symbolic mode. See the EXAMPLES section for some illustrative examples.

-perm /mode 搜尋檔案的權限 "包含任一 mode 的權限"

當搜尋 -rwxr-xr-x ,即 0755 的檔案,如果檔案屬性為-rw——- ,即 0500 ,則此檔案會被列出來,因為 -rw——- 已經包含 -rw——- 的屬性了。

Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form.

You must specify ‘u’, ‘g’ or ‘o’ if you use a symbolic mode. See the EXAMPLES section for some

illustrative examples. If no permission bits in mode are set, this test currently matches no

files. However, it will soon be changed to match any file (the idea is to be more consistent with

the behaviour of perm -000).

-perm +mode

Deprecated, old way of searching for files with any of the permission bits in mode set. You should

use -perm /mode instead. Trying to use the ‘+’ syntax with symbolic modes will yield surprising

results. For example, ‘+u+x’ is a valid symbolic mode (equivalent to +u,+x, i.e. 0111) and will

therefore not be evaluated as -perm +mode but instead as the exact mode specifier -perm mode and so

it matches files with exact permissions 0111 instead of files with any execute bit set. If you

found this paragraph confusing, you’re not alone – just use -perm /mode. This form of the -perm

test is deprecated because the POSIX specification requires the interpretation of a leading ‘+’ as

being part of a symbolic mode, and so we switched to using ‘/’ instead.

-regex pattern

File name matches regular expression pattern. This is a match on the whole path, not a search.

For example, to match a file named ‘./fubar3′, you can use the regular expression ‘.*bar.’ or

‘.*b.*3′, but not ‘f.*r3′. The regular expressions understood by find are by default Emacs Regular

Expressions, but this can be changed with the -regextype option.

-type c

File is of type c:

b block (buffered) special

c character (unbuffered) special

d directory

p named pipe (FIFO)

f regular file

l symbolic link; this is never true if the -L option or the -follow option is in effect,

unless the symbolic link is broken. If you want to search for symbolic links when -L is in

effect, use -xtype.

s socket

D door (Solaris)

-uid n File’s numeric user ID is n.

-used n

File was last accessed n days after its status was last changed.

-user uname

File is owned by user uname (numeric user ID allowed).

-wholename pattern

File name matches shell pattern pattern. The metacharacters do not treat ‘/’ or ‘.’ specially; so,

for example,

find . -wholename ‘./sr*sc’

will print an entry for a directory called ‘./src/misc’ (if one exists). To ignore a whole direc-

tory tree, use -prune rather than checking every file in the tree. For example, to skip the direc-

tory ‘src/emacs’ and all files and directories under it, and print the names of the other files

found, do something like this:

find . -wholename ‘./src/emacs’ -prune -o -print

———————————————-

尋找檔案內容

find . | xargs grep ‘string’

無迴響

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress