1、
mail -s "test"  sld34@163.com< aa.txt -- -f  422071607@qq.com    从422071607@qq.com发一封邮件到sld34@163.com

2、
[root@3001 lianding]# gunzip -dc t_w.tar.gz | tar xf - westuniongroup.cn

$gunzip -dc file.tar.gz | tar tf -
可以看包里面的文件
然后用
$gunzip -dc file.tar.gz | tar xf - file1 file2
其中file1 file2是你要解出来的文件。这个方法因为用的是stdout和pipe,
不需要占用多少的临时磁盘空间。
gunzip -dc maildata.tar.gz | tar xf - mail/star-blowers.com/sales


3.sed -e "s/|/\n/g" .bash_history | cut -d '' -f 1 | sort |uniq -c|sort -nr|head -10

将 .bash_history文件里的|替换为换行,并以空格为分界符,截取字段1,按字符排序,

显示前面几条最常用的命令
     39 ls
     19 df
     10 fdisk -l
      7 ping www.baidu.com
      6 ll
      6 cd ..
      5 route
      5 pwd
      5 ifconfig


4.获取服务器IP
[root@localhost ~]# ifconfig eth0|grep "inet addr:"|awk -F : '{print $2}'|awk '{print $1}'
192.168.3.128

[root@localhost ~]# ifconfig eth0|grep "inet addr:"|awk '{print $2}'|cut -c 6-
192.168.3.128

 


5.route add 192.168.0.144 mask 255.255.255.0 192.168.0.1 metric 3 if 2
  route change 192.168.0.155 mask 255.255.255.0 192.168.0.1 metric 3 if 2
  route delete 192.168.0.155

 


6. for i in `ps aux |grep sshd |grep -v grep | awk {'print $2'}`;do kill $i;done
查找到对应进程,并杀掉


7.for i in *;do mv "$i" "$(echo $i|tr A-Z a-z)";done
将当前目录文件名转换为小写

8.清除VIM中的^M的方法
sed -e 's/^M//' filename
vim中 :s/^M//gc


9.1.一文本里面内容是
192.168.1.2/24
167.178.1.3/24
212.121.1.3/24
192.168.1.2/24
167.178.1.3/24
212.121.1.3/24
123.124.122.121/24
123.124.122.121/24
编写脚本将其前面添加入“abcd”并将 /去掉然后以排序汇总
[root@localhost home]# sed 's/^/4444/g' 1.txt
4444abc
4444123
4444777
4444
文本开头插入对应字段

sed 's#/# #g' 1.txt
123.124.122.121 24
去除/转换为空格

 

10.在每一行后面增加两行空行
sed 'G;G'


11,awk -F \| '{ print $2 }' 1.txt|sed 's/^/www\./g'
以|分隔的服务器网站信息列表,截取域名字段,并添加www.头,显示


12.find . -type f -exec ls -l {} \;
查找当前路径下的所有普通文件,并把它们列出来

 

13.find logs -type f -mtime +5 -exec rm {} \;
删除LOG目录下更新时间为5日以上的文件
 find /var/log -type f -mtime +5 -exec ls -lh {} \;|awk '{print $5 }'|sort -rn 


14.find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./install.log > ? n

删除5天前的日志文件,并提示是否确认删除

find在有点系统中会一次性得到将匹配到的文件都传给exec,但是有的系统对exec的命令长度做限制,就会报:”参数列太长“,这就需要使用xargs

。xargs是部分取传来的文件。

 


15.cut -d '分隔字符' -f fields
参数
-d: 后面接分隔字符,与-f一起使用
-f: 依据-d的分隔字符将一段信息分割成为数据,用-f取取第几段的意思
-c: 以字符(characters)的单位取出固定字符的区间
cat /etc/passwd | cut -d ':' -f 1
cat /etc/passwd | cut -d ':' -f 6

 

16.判断用户是否存在
echo -n "Please input user name:"
read name
grep "$name" /etc/passwd > /dev/null 2>&1
if [ $? -eq 0 ];then
echo $name exist
exit 0
else
echo $name not exist
exit 1
fi

 

17.

这些都是我的LINUX 服务器里的备份时的文件夹

20110306  20110311  20110315  20110319  20110323  20110327  20110331         20110403  20110407  20110411  20110415  20110419 

20110423  20110427  20110501  20110505  20110509

for a in 2011*;do
tar zcvf $a $a.tar.gz
done

 

 


18.
mysql> use mysql
Database changed

执行下面的语句即可!
grant all on 数据库名.* to '数据库名'@'%';
4.更新一下权限
mysql> flush privileges;

 


19.find -type f -size 0 -exec rm -rf {}\;  删除0字节文件


20.sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config  sed在这个文件里Root一行,将no替换为yes

 

 

21.ps aux|grep mysql|grep -v grep|awk '{print $2}'|xargs kill -9 杀掉mysql进程

 

22.free -m |grep "Mem" |awk '{print $2}'  获取内存大小

 

23.清理日志脚本
ls /var/www/vhosts/ |  read domain ; do cat /dev/null >  /var/www/vhosts/$domain/statistics/logs/error_log;done 


ls /var/www/vhosts/ | while read domain ; do du -sh /var/www/vhosts/$domain/cgi-bin;done


ls /var/www/vhosts/ | while read domain ; do rm zzy_temp_index.html /var/www/vhosts/$domain/httpdocs;done

 

24.
ls | xargs -I {} mv {} `date +%N`.eml
有待测试


for a in 00* ;do
  cd $a
  ls | while read line;do mv $line `date +%N`.eml;done

done

 


25.ls |xargs -I {} mv {} /root   移动当前目录下的文件到/root下
   ls *.eml |xargs -I {} cp {} ../001/


26.ls | while read line;do mv $line `date +%N`.eml;done
将当前目录下的文件改名为一个随机数.eml

ls |while read line;do echo $line;done
666q3e46y.eml
9jglajglalg.eml
aaa.eml
algljglj.eml
{date+%N}.eml
glagjlajgl.eml
t5yqty.eml

 


27.
#!/bin/bash
b=`cd /home`
for a in `ls |grep 00\.`;do
cd $a
ls | while read line;do mv $line `date +%N`.eml;done
done

 

28.
[root@localhost home]# vi yidong.sh
#!/bin/bash
for a in `ls |grep 00\.`;do
cd $a
ls | while read line;do mv $line `date +%N`.eml;done
cd /home
done


29.

ls *.eml |xargs -I {} cp {} ../001/

实现当前目录下的文件内容移动到其他目录下,并创建好目录,文件名为随机产生
#!/bin/bash
cd /home
for a in `ls |grep 00\.`;do
cd /
mkdir $a
cd /home/$a
ls | while read line;do mv $line `date +%N`.eml
cd /home/$a
ls *.eml |xargs -I {} cp {} /$a;
done
cd /home
done


30.查看频繁连接的IP,并添加IP阻止掉。 小攻击停有用的
netstat -an| grep :80 | grep -v 127.0.0.1 |awk '{ print $5 }' | sort|awk -F: '{print $1,$4}' | uniq -c | awk '$1 >50 {print $1,$2}'

netstat -an |grep :80 |grep -v 127.0.0.1 |awk '{print $5}' |sort |awk -F: '{print $1,$4 }' |uniq -c |awk '$1 >50 {print $1,$2}'
132 113.89.94.149
62 59.51.64.20

iptables -I INPUT -s 123.150.183.0/24 -j DROP


tail access2|awk '{print $1,$4}'
220.174.208.154 [26/Jul/2011:16:32:14
203.208.60.163 [26/Jul/2011:16:32:18
67.195.111.39 [26/Jul/2011:16:32:22
67.195.111.39 [26/Jul/2011:16:32:23
58.214.13.92 [26/Jul/2011:16:32:46
61.189.164.25 [26/Jul/2011:16:32:59
203.208.60.163 [26/Jul/2011:16:33:24
203.208.60.163 [26/Jul/2011:16:33:40
58.245.27.222 [26/Jul/2011:16:33:52
61.189.164.24 [26/Jul/2011:16:34:00

 


31.剔除远程登录的用户
[root@fc4 ~]# w
11:12:23 up 44 min, 2 users, load average: 0.08, 0.06, 0.16
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
shily    tty1     -                11:11   26.00s 0.04s 0.04s -bash
changyj pts/1    192.168.238.1    11:10    0.00s 0.12s 0.10s sshd: changyj
[root@fc4 ~]# pkill -KILL -t tty1

或ps -ef|grep tty1  kill -9 id号

 

32.排除文件夹下的某个文件打包程序代码
tar zcvf data.tar.gz /data/ --exclude=/data/inc

 

33.for fname in *; do mv $fname `echo $fname|tr [A-Z] [a-z]`; done
析:
for file in `ls | grep '[A-Z]'`
do
str=`echo $file|tr 'A-Z' 'a-z'`
mv $file $str
done
1)ls | grep '[A-Z]' :ls 出所有含有大写字母的文件
2)for file in `command` :for 循环
3)echo AVdxFV | tr 'A-Z' 'a-z' : 把'AVdxFV' 中所有的大写换成小写字母; tr :translate的意思,具体看help。

 


34.case选择应用应用

#!/bin/bash
a_type=$1
case ${a_type} in
  aa)echo "aaa"
  ;;
  bb)echo "bbb"
  ;;
  *)echo "other"
  ;;
esac


修正版:
vi case.sh

read var
#!/bin/bash
echo "aa"
echo "bb"
echo "other"
echo "please input your choice"
read var
a_type=$var
case ${a_type} in
  aa)echo "aaa"
  ;;
  bb)echo "bbb"
  ;;
  *)echo "other"
  ;;
esac


35.
 seq 命令   
它有三个选项   -f, --format=FORMAT use printf style floating-point FORMAT (default: %g)   -s, --separator=STRING use STRING to

separate numbers (default: \n)   -w, --equal-width equalize width by padding with leading zeroes   -f 最常用 ,
 例如一次制做 10 个名 dir001 , dir002 .. dir010 的目录,我们可以   
运行下面的命令便可  
 seq -f 'dir%03g' 1 10 | xargs mkdir   
或   mkdir $(seq -f 'dir%03g' 1 10)   
它用的是 printf 的格式 , %03g' 代表以三位浮点数,以此方法,

 这样会比写一个脚本快, 不必写成  
 for dir in 001 002 003 004 005 006 007 008 009 010   do   mkdir dir$   done  
 也可用 seq 下载一些用数字的 jpeg , 只要格式有数字顺序便可,尤以一些 xxx site ;)  
 for i in `seq -f '%02g' 1 20`   do   if ! wget -P $HOME/tmp -c ; then   wget -P $HOME/tmp -c $_   fi   done   -s 选项

主要改变输出的分格符, 预设是 \n , 就是 newline   如用 -s 便可改变 , 如   seq -s ' ' 1 10   1 2 3 4 5 6 7 8 9 10 , 以空格作为

分格   …

 

批量建立文件或者目录
[root@AS4 python]# seq -f 'dir%03g' 3 10 | xargs mkdir
[root@AS4 python]# ll
total 52
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir003
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir004
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir005
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir006
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir007
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir008
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir009
drwxr-xr-x  2 root root 4096 Jan 13 04:38 dir010
建立用户时要添加参数n,否则会报错:
[root@AS4 python]# seq -f 'user%02g' 1 5 | xargs -n 1 useradd

 


36.
去除文件以数字开始的段
[root@linuxas4 shell]# cat test.txt
abcd    kkk
abcd    ll      df
234     dfkjk   23
[root@linuxas4 shell]# sed -r 's/^[0-9]+//' ./test.txt
abcd    kkk
abcd    ll      df
        dfkjk   23

 

 

37.文件之间的对比
[linuxboy823@fedora ~]$ cat file1
1
2
3
4
5
6
[linuxboy823@fedora ~]$ cat file2
1
2
3
4
7
8
[linuxboy823@fedora ~]$ sort file1 file2 |uniq -d>file3
1、找出file1中有而file2中没有的号码
[linuxboy823@fedora ~]$ sort file1 file3|uniq -u
5
6
2、找出file2中有而file1中没有的号码
[linuxboy823@fedora ~]$ sort file2 file3|uniq -u
7
8
3、找出两个文件中都有的号码
[linuxboy823@fedora ~]$ sort file1 file2 |uniq -d
1
2
3
4

 

 

38.
将文件内字符全部转换为大写
[root@linuxas4 shell]# cat test.txt | tr a-z A-Z
ABCD    KKK
ABCD    LL      DF
234     DFKJK   23


39.chattr相关说明
chattr +i test.txt              #使文件只读不可改变(root生效)
chattr +A test.txt              #即使文件被访问,access time 也不会改变


39.mysql脚本(还需要验证)

#/bin/sh
MysqlPassword='yankai'
DBName='test'
PatchFile='sql.txt'
IP='ip.txt'
for i in `cat /root/ip.txt`
        do
        /usr/local/mysql/bin/mysql -uroot -p$MysqlPassword -h$i $DBName < $PatchFile;
        done

 

40.
写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输

入"end"停止。#!/bin/sh
unset var

while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done

 

41.grep -v "^#" main.cf |grep -v "^$"  >main2.cf  去除配置文件里的空格和与#开头的行。

 


42.统计日志文件里访问请求的IP数量:
[root@localhost logs]# cat access2-20110725|awk '{print $1}'|uniq -c|sort -rnd|head -n 10
    163 59.57.214.17
    125 121.204.182.40
    121 121.14.162.62
    113 59.61.46.62
    113 120.42.49.134
    113 110.228.181.133
    111 222.189.239.222
    111 222.189.237.7
    110 125.77.250.31
    110 121.204.174.220

参考例题:
要求分析Apache访问日志,找出里面数量在前面100位的ip数.日志大约在78M左右.一下是apache日志文件
  节选(其中日志access.log):
        218.107.27.137 .....
        202.112.32.22 ....[26/Mar/2006:23:59:55 +0800].......
   shell程序如下:
     方法一:
        (1)#!/bin/bash
           #先找出唯一IP
            cat access.log|awk '{print $1}'|sort -d >ip.txt
        (2) 再用唯一的IP去日志里面计算出现的次数
            while read IP
                 do
                 COUNT=`cat access.log|grep $IP|wc -l`
                    echo $COUNT:$IP >>count.txt
                  done< ip.txt
                  cat count.txt|sort -n -r|head -100 > top100.txt
                  echo "" > count.txt
      方法二:
           awk '{print $1}' access.log|sort -n -t. +0 +1 +2 +3|uniq -c|sort -nr|head -100
      方法三:
          cat access.log|awk '{print $1}'|sort|uniq -c|awk 'NR<=100{print $1,$2}'|sort -r
      方法四:
           cat access.log|awk '{print $1}'|sort -n|uniq -c|sort -nr|head -100

 

43.去除空行的几种方法:
1.grep -v "^$" url.txt >url3.txt
[root@localhost home]# cat url3.txt
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://hi.baidu.com/baidu/
http://www.sina.com.cn/head/www20021123am.html
http://www.sina.com.cn/head/www20041223am.html

2.sed /^$/d url.txt >url4.txt

3.vi环境下
:g/^s*$/d


删除空格的几种方法:
1.替换法:vi环境下
:%s/ *//g

2.sed s/[[:space:]]//g url.txt >url5.txt

3.

 

 

44.[root@localhost home]# vi test.sh

#!/bin/bash
for i in {1..9}
  do
    a=$i$a
    echo $a
  done
[root@localhost home]# sh test.sh
1
21
321
4321
54321
654321
7654321
87654321
987654321

 

45.sed '17d'  删除17行
1) 使用s / - * / / g删除横线- - - - - -。
2) 使用/ ^ $ / d删除空行。
3) 使用$ d删除最后一行
4) 使用1 d删除第一行。
5) 使用awk {print $1}打印第一列。
命令如下,这里使用了c a t,并管道传送结果到s e d命令。

[sam@chenwy sam]$ cat data.txt |sed 's/--*/ /g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}'

 

 

 

46.去除文件含有行号的案例:
[root@localhost home]# cat 2.txt
01 #! /bin/sh 
02 #while 语句 
03 echo "enter passwd"
04 read passwd
05 while [ $passwd != "aaa" ];do
06     echo "sorry try again"
07     read passwd
08 done

[root@localhost home]# sed -i 's/^[0-9]*//g' 2.txt
[root@localhost home]# cat 2.txt 
 #! /bin/sh 
 #while 语句 
 echo "enter passwd"
 read passwd
 while [ $passwd != "aaa" ];do
     echo "sorry try again"
     read passwd
 done

 

 

47. while密码验证简单代码:
 #! /bin/sh 
 #while 语句 
 echo "enter passwd"
 read passwd
 while [ $passwd != "aaa" ];do
     echo "sorry try again"
     read passwd
 done

 #for 语句 
 #! /bin/bash 
 for i in a b c; do 
     echo "$i\n"
 done
   
 #case 语句 
   
 #! /bin/sh 
   
 echo "Enter a number"
 read number 
 case $number in
     1) 
     echo "you number is 1"
     ;; 
     2) 
     echo "yo number is 2"
     ;; 
     *) 
     exit 1  
     ;; 
 esac  
   
 #if else elif fi    
 #! /bin/sh  
 echo "Is it morning? Please answer yes or no."
 read YES_OR_NO 
 if [ "$YES_OR_NO" = "yes" ]; then
   echo "Good morning!"
 elif [ "$YES_OR_NO" = "no" ]; then
   echo "Good afternoon!"
 else
   echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
   exit 1 
 fi
 exit 0


48.
site:www.baidu.com inurl:/more
site:zhidao.baidu.com inurl:/browse/
site:www.sina.com.cn inurl:www20041223am

cat inurl.txt |awk -F "[:]" '{print $2,$4}'  表示以冒号和默认的空格为分界

www.baidu.com inurl
zhidao.baidu.com inurl
www.sina.com.cn inurl

 

49.ls -l /var/log/messages|awk '{print $5}'    当前目录下的总大小统计:
71992 

扩展:

#!/bin/bash 
 # 当/var/log/syslog大于1GB时 
 # 自动将其备份,并清空 
 # 注意这里awk的使用 
 if ! [ -f /var/log/syslog ] 
 then
     echo "file not exist!"
     exit 1 
 fi
 if [ `ls -l /var/log/syslog|awk '{print $5}'` -gt $((1024*1024)) ] 
 then
     cat /var/log/syslog >> ~/log/history # 将日志备份 
     echo >> ~/log/history               # 增加一个空行 
     date >> ~/log/history               # 记录时间 
     echo "-------------------------------------" >> ~/log/history
     echo > /var/log/syslog              # 清空 “

扩展:
服务器测试message》10:
#!/bin/bash
if ! [ -f /var/log/messages ]
then
   echo "file not exist!"
   exit 1
fi
if [ `ls -l /var/log/messages|awk '{print $5}'` -gt $((10)) ]
then
    cat /var/log/messages >> ~/log/history
    echo >> ~/log/history
    date >> ~/log/history
    echo "--------------------------------------------">> ~/log/history
    echo > /var/log/message
fi


50.当前目录下的以.html结尾的文件
find $1 -name "*.html"
./999879.html
./abc.html
./g.html
./124.html

 

 

51.
‘-e /abcd/d’ 删除包含a b c d的行
‘s / [ ] [ ] [ ] * / [ ] / g’ 删除一个以上空格,用一个空格代替
‘s / ^ [ ] [ ] * / / g’ 删除行首空格
‘s / \ . [ ] [ ] * / [ ] / g’ 删除句点后跟两个或更多空格,代之以一个空格
‘/ ^ $ / d’ 删除空行
‘s / ^ . / / g’ 删除第一个字符
‘s /CO L \ ( . . . \ ) / / g’ 删除紧跟C O L的后三个字母
‘s / ^ \ / / / g’ 从路径中删除第一个\
‘s / [ ] / [ ] / / g’ 删除所有空格并用t a b键替代
‘S / ^ [ ] / / g’ 删除行首所有t a b键
‘s / [ ] * / / g’ 删除所有t a b键


将替换结果写入一个文件用w选项,下面的例子将splendid替换为SPLENDID的替换结果写入文件sed.out:
[sam@chenwy sam]$ sed 's/splendid/SPLENDID/w sed.out' quote.txt

 

52.要从命令行中向s e d传值,值得注意的是用双引号,否则功能不执行。
[sam@chenwy sam]$ NAME="It's a go situation"
[sam@chenwy sam]$ REPLACE="GO"
[sam@chenwy sam]$ echo $NAME | sed "s/go/$REPLACE/g"
It's a GO situation


53.删除当前目录下不在文件列表里头的文件方法:
ls |grep -vf filelist |grep  -v filelist|xargs rm -f


54.查找目录下所有*.php文件里包含特定字符串的简单代码:

#!/bin/sh
for f in `find 路径 -name "*.php"`
do
grep "要查找的内容" $f >>/dev/null && echo $f >> count.txt
done

root@localhost test]# sh chazhao.sh   
[root@localhost test]# less count.txt
./a.php
./888/8888.php
./c.php
./77/1.php