[例子]Linux下shell批處理文件的幾種方法

附上網友們總結出來的方法如下(以批量解壓tar文件爲例):

第一:

for tar in *.tar.gz; do tar xvf $tar; done

for tar in *.tar.bz2; do tar xvf $tar; done

第二:用tar命令批量解壓某個文件夾下所有的tar.gz文件

ls *.tar.gz | xargs -n1 tar xzvf

第三:find -maxdepth 1 -name “*.bz2″|xargs -i tar xvjf {}
這條命令可解壓當前目錄下的所有bz2文件,maxdepth表示搜索深度,1代表只搜索當前目錄

第四:for i in $(ls *.tar);do tar xvf $i;done

以上,第二個方法最爲簡單明瞭!

再附上第四種,附上批量壓縮靜態文件(js、css)成tar.gz:

此腳本 好處是判斷文件修改時間,不重複生成

find ./ -type f -printf ‘%TY %Tm %Td %TH %TM %TS\t%p\n’ \
| grep -iE ‘\.(html|txt|css|js)$’ \
| awk -F’\t’ ‘{

gz_file=$2 “.gz”;
stat_cmd=”stat -c \”%Y\” ” gz_file;
exist_cmd=”[  -e \"" gz_file "\" ] && echo 1 || echo 0″;

exist_cmd | getline gz_file_exist;

if (gz_file_exist) {
    stat_cmd | getline last_modify_timestamp;
} else {
    last_modify_timestamp=0;
}

if (last_modify_timestamp < mktime($1)) {
    system(“gzip -c9 ” $2 ” > ” gz_file);
    print gz_file ” [created]“;
}

}’

此腳本結合nginx的gzip static模塊使用,編譯時加上--with-http_gzip_static_module

nginx.conf中加上:

    sendfile on; # 最好打開這個sendfile
    gzip  on;
    gzip_static on;
    gzip_http_version 1.0;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;
    gzip_comp_level 2;
    gzip_min_length 1024;
    gzip_buffers 4 16k;
    gzip_types text/plain html application/x-javascript text/css;



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章