天天看點

shell相關的批量操作

1、批量删掉檔案名字尾的簡單方法

name=<code>ls</code> #定義清單

for i in $name #取值

do

mv $i ${i%.} #删除内容(%)後的.

done

下面是網上的原文

要思路簡單易于了解的話,可以這樣:

1234 for file in <code>find . -name "*.txt"</code>do    mv $file ${file%.}done

${file%.} 是bash中字元串處理“掐頭去尾法”中的去尾法。

2、循環中單個過程引用多變量

A=(<code>cat /root/dev.txt</code>)

B=(<code>cat /root/mount.txt</code>)

for ((i=0;i&lt;3;i++))

echo ${A[$i]} ${B[$i]}

3、定義整數清單

#!/bin/bash

for i in {1..10} #1

#for((i=1;i&lt;=10;i++)) #2

#for i in <code>seq 1 10</code> #3

echo $i

4、在指定行的前/後插入指定内容

5、比對行添加注釋

描述:sed -i '/比對條件1/s/位置:開頭^結尾$或者再次比對/替換内容或者說要添加的内容/' 文本檔案

sed -i '1,4 s/^/#/' test.sh #指定1-4行行首添加#

sed -i '1,4 s/^#//' test.sh #指定1-4行删除行首内容#

sed -i '/grep/s/^/#/' test.sh #比對關鍵字在其行首添加#

sed -i '/grep/s/$/#/' test.sh #比對關鍵字在其行尾添加#

sed -i '/dcits/s/proxy/tianle/' test.sh #比對dcits關鍵字,再将比對到的行中的proxy替換成tianle

sed -i '/grep/s/^#//' test.sh #去掉比對行的行首#注釋

sed -i '5,26 {/^#/d}' login2.py #比對5-26行中開頭為#的行進行删除操作

6、指定列比對後進行列印(此例子有問題)

for filename in <code>ls -hl |grep -v ^d|awk '$5 &amp;gt; 10k {print $9}'</code> #查找目前目錄中檔案大于10k的

cp $filename /tmp

本文轉自 天樂 51CTO部落格,原文連結:http://blog.51cto.com/tianlegg/2054113,如需轉載請自行聯系原作者

繼續閱讀