如何用脚本实现分割文件#!/bin/bashif [ $# -ne 2 ]; thenecho 'Usage: split file size(in bytes)'exitfifile=$1size=$2if [ ! -f $file ]; thenecho "$file doesn't exist"exitfi#TODO: test if $size is a valid integerfilesize=`/bin/ls -l $file | awk '{print $5}'`echo filesize: $filesizelet pieces=$filesize/$sizelet remain=$filesize-$pieces*$sizeif [ $remain -gt 0 ]; thenlet pieces=$pieces+1fiecho pieces: $piecesi=0while [ $i -lt $pieces ];doecho split: $file.$i:dd if=$file of=$file.$i bs=$size count=1 skip=$i let i=$i+1doneecho "#!/bin/bash" >; mergeecho "i=0" >;>; mergeecho "while [ $i -lt $pieces ];" >;>; mergeecho "do" >;>; mergeecho " echo merge: $file.$i" >;>; mergeecho " if [ ! -f $file.$i ]; then" >;>; mergeecho " echo merge: $file.$i missed" >;>; mergeecho " exit" >;>; mergeecho " fi" >;>; mergeecho " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >;>; mergeecho " let i=$i+1" >;>; mergeecho "done" >;>; mergechmod u+x merge'如何查找日期为某一天的文件#!/bin/sh# The right of usage, distribution and modification is here by granted by the author.# The author deny any responsibilities and liabilities related to the code. #OK=0A=`find $1 -print`if expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fiif expr $3 == 2 >;/dev/null ; then M=Feb ; OK=1 ; fiif expr $3 == 3 >;/dev/null ; then M=Mar ; OK=1 ; fiif expr $3 == 4 >;/dev/null ; then M=Apr ; OK=1 ; fiif expr $3 == 5 >;/dev/null ; then M=May ; OK=1 ; fiif expr $3 == 6 >;/dev/null ; then M=Jun ; OK=1 ; fiif expr $3 == 7 >;/dev/null ; then M=Jul ; OK=1 ; fiif expr $3 == 8 >;/dev/null ; then M=Aug ; OK=1 ; fiif expr $3 == 9 >;/dev/null ; then M=Sep ; OK=1 ; fiif expr $3 == 10 >;/dev/null ; then M=Oct ; OK=1 ; fiif expr $3 == 11 >;/dev/null ; then M=Nov ; OK=1 ; fiif expr $3 == 12 >;/dev/null ; then M=Dec ; OK=1 ; fiif expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fiif expr $OK == 1 >; /dev/null ; thenls -l --full-time $A 2>;/dev/null | grep "$M $4" | grep $2 ;elseecho Usage: $0 path Year Month Day;echo Example: $0 ~ 1998 6 30;fi如何计算当前目录下的文件数和目录数# ls -l * |grep "^-"|wc -l ---- to count files# ls -l * |grep "^d"|wc -l ----- to count dir如何只列子目录?ls -F | grep /$ 或者 alias sub = "ls -F | grep /$"(linux)ls -l | grep "^d" 或者 ls -lL | grep "^d" (Solaris)如何实现取出文件中特定的行内容如果你只想看文件的前5行,可以使用head命令,如: head -5 /etc/passwd如果你想查看文件的后10行,可以使用tail命令,你知道怎么查看文件中间一段吗?你可以使用sed命令如: sed -n '5,10p' /etc/passwd 这样你就可以只查看文件的第5行到第10行。
如何查找含特定字符串的文件例如查找当前目录下含有"the string you want find..."字符串的文件:$find . -type f -exec grep “the string you want find...” {} ; -print如何列出目录树下面的短小的shell程序可以列出目录树, 充分利用了sed强大的模式匹配能力.目录树形式如下:.`----shellp`----updates`----wu-ftpd-2.4| `----doc| | `----examples| `----src| | `----config| | `----makefiles| `----support| | `----makefiles| | `----man| `----util脚本如下:#!/bin/sh# dtree: Usage: dtree [any directory]dir=${1:-.}(cd $dir; pwd)find $dir -type d -print | sort -f | sed -e "s,^$1,," -e "/^$/d" -e"s,[^/]*/([^/]*)$,`----1," -e "s,[^/]*/,| ,g"如何实现取出文件中特定的列内容我们经常会遇到需要取出分字段的文件的某些特定字段,例如/etc/password就是通过“:”分隔各个字段的。
可以通过cut命令来实现。
例如,我们希望将系统账号名保存到特定的文件,就可以:cut -d: -f 1 /etc/passwd >; /tmp/users-d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段。
当然也可以通过cut取得文件中每行中特定的几个字符,例如:cut -c3-5 /etc/passwd就是输出/etc/passwd文件中每行的第三到第五个字符。
-c 和-f 参数可以跟以下子参数:N 第N个字符或字段N- 从第一个字符或字段到文件结束N-M 从第N个到第M个字符或字段-M 从第一个到第N个字符或字段在vim中实现批量加密============================================================#!/bin/bash# Encrypt file with vimif (test $# -lt 2) thenecho Usage: decrypt password filenameelsevim -e -s -c ":set key=$1" -c ':wq' $2echo "$2 encrypted."fi============================================================[weeder@SMTH weeder]$ for file in *.txt ; do encrypt test $file ; donetest2.txt encrypted.test4.txt encrypted.test9.txt encrypted.kick.txt encrypted.echo "$2 encrypted."fi[weeder@SMTH weeder]$ for file in *.txt ; do encrypt test $file ; donetest2.txt encrypted.test4.txt encrypted.test9.txt encrypted.kick.txt encrypted.too_old.txt encrypted.too_old_again.txt encrypted.bg5.txt encrypted.[weeder@SMTH weeder]$$@等特定shell变量的含义在shell脚本的实际编写中,有一些特殊的变量十分有用:$# 传递到脚本的参数个数$* 以一个单字符串显示所有向脚本传递的参数。