现在的位置: 首页 > 综合 > 正文

Linux指令学习(二)

2013年12月11日 ⁄ 综合 ⁄ 共 3813字 ⁄ 字号 评论关闭

1、批量将某些文件写到文件夹中,写一个shell脚本

mvtopack.sh

#!/bin/bash

for f in`ls`

do

     mkdir ${f}"urls"

     mv ${f} ./${f}"urls"

done

2、当linux系统中安装了两个jdk时,需要转换使用,需要修改

cd ~

vi  .bash_profile

修改其中的内容

3.排序

sort -n -r -k 2 -t : aa.txt

-n:数字排序,-r:逆排序,-k 2 第2列 , -t :  表示用冒号分割,aa.txt文件

4、输出一个文件的前n行,输出到另一个文件

cat    aa.txt | sed -n '1-10p' > bb.txt

5、将两个文件内容合并到一个地方:

cat aa.txt  bb.txt > cc.txt

6、uniq

进行排序之后,您会发现有些行是重复的。有时候该重复信息是不需要的,可以将它除去以节省磁盘空间。不必对文本行进行排序,但是您应当记住 uniq 在读取行时会对它们进行比较并将只除去两个或更多的连续行。下面的示例说明了它实际上是如何工作的:

清单 1. 用 uniq 除去重复行

				
        $ cat happybirthday.txt
Happy Birthday to You!
Happy Birthday to You!
Happy Birthday Dear Tux!
Happy Birthday to You!
        $ sort happybirthday.txt 
Happy Birthday Dear Tux!
Happy Birthday to You!
Happy Birthday to You!
Happy Birthday to You!
        $ sort happybirthday.txt | uniq
Happy Birthday Dear Tux!
Happy Birthday to You!
      

警告:请不要使用 uniq 或任何其它工具从包含财务或其它重要数据的文件中除去重复行。在这种情况下,重复行几乎总是表示同一金额的另一个交易,将它除去会给会计部造成许多困难。千万别这么干!

单 2. 使用 -u 和 -d 选项

				
        $ sort happybirthday.txt | uniq -u
Happy Birthday Dear Tux!
        $ sort happybirthday.txt | uniq -d
Happy Birthday to You!
      

您还可以用 -c 选项从 uniq 中获取一些统计信息:3. 使用 -c 选项

				
        $ sort happybirthday.txt | uniq -uc
      1 Happy Birthday Dear Tux!
        $ sort happybirthday.txt | uniq -dc
      3 Happy Birthday to You!
      

6、是文件类型的移到文件夹中

#!/bin/bash
for f in `ls`
do
        if [ -f $f ];then
                mkdir $f"g";
                mv $f ./$f"g";
        fi
done

7、切割文件,只要一列

cut -d: -f 1 /etc/passwd > /tmp/users
-d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段

8、去重文件中的空行

grep -v '^$' input.txt >output.txt

9、显示数字,循环

#!/bin/bash
for(( i=2; i<11; ++i)) ; do
        mkdir "data"$i;
done

echo "Using for loop method # 1... "
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo -n "$i "
done

echo "Using while loop..."
j=1
while [ $j -le 10 ]
do
   echo -n "$j "
   j=$(( j + 1 )) # increase number by 1
done
echo ""

ps -ax | grep nutch | awk '{print "kill " ,$1}'

学习使用awk

#!/bin/bash
#there is a bug in the command ,process number length is not certain,so "-f 1" is possible blank.
#arr=`ps ax | grep 'java -jar manager.jar' | cut -d " " -f 1`;
arr=`ps ax | awk '$0 ~/java -jar .*app.jar$/ && $0 !~ /grep /  {print $1}'`
for comm in  $arr ;do
        echo $comm;
        `kill -9 $comm`;
done;

cat monitor_net.txt | grep eth0 | awk '{if($1>"08:29:00" && $1<"08:52:41"){reckb=$6/1024; if(reckb > 300)  print reckb}}' >140_thread_net_result.txt

sleep

#!/bin/bash
a=`ls | wc -l`
echo $a;
if [ $a -gt 1 ];then
        sleep 10s;
        echo "test";
else
        echo "test .....";
fi

a=`date --date='yesterday' +%Y%m%d`;

批量处理的脚本,只要肯想,一定有解决办法:

#!/bin/bash
k=1;
v=ww_info_20120801
r=ww_info_20120801_done
for((i=1;i<43153;++i));do
a=`head -n $i $v | tail -n 1 | cut -f 1`
j=`expr $i + 1`
b=`head -n $j $v | tail -n 1 | cut -f 1`
if [ $k == 1 ];then
        `head -n $i $v | tail -n 1 | awk '{print "1"$0}' >> $r`
else
        `head -n $i $v | tail -n 1 | awk '{print "2"$0}' >> $r`
fi

if [ $a -gt $b ];then
        k=`expr $k + 1`
        k=$(($k%2))
fi;
done

#!/bin/bash
cd /usr/xxx/data/data1_head_Result_history
a=`ls -al -t | grep "re" | head -n 277 | awk '{print $9}'`;
j=0
for i in $a; do
        j=`expr $j + 1`
        echo $j;
        `mv $i /usr/xxx/data/houchuli/`
done

正确使用expr,如果expr的参数有非数字,就会报 语法错误!!!!!!!!

#!/bin/bash
sum=0;
for a in `ls`; do
        b=`wc -l ${a} | cut -f 1 -d " "`;
        echo $b;
        sum=`expr $sum + $b`;
        echo $sum;
done;

linux下使用dhcp,dsl联网

系统默认是dhcp,在/etc/network/interfaces 中的内容是:

auto lo
iface lo inet loopback

#dhcp链接网络
auto eth0
iface eth0 inet dhcp

#dsl链接网络
auto dsl-provider
iface dsl-provider inet ppp
pre-up /sbin/ifconfig eth0 up # line maintained by pppoeconf
provider dsl-provider

从dsl断开连到dhcp时,执行:service networking restart

当从dhcp连到dsl时,执行:pppoeconf  eth0

随机取样的代码
awk 'BEGIN{srand();while(i<10000){k=int(rand()*100000000);if(!(k in a)){a[k]++;i++}}}(NR in a){print $1}' urfile
很好用!

 查看文件编码格式:

vim aa.txt

:set fieencoding

find命令还是一定要用的,只有自己写过才能记得更清楚

find / -amin -10 # 查找在系统中最后10分钟访问的文件
  find / -atime -2 # 查找在系统中最后48小时访问的文件
  find / -empty # 查找在系统中为空的文件或者文件夹
  find / -group cat # 查找在系统中属于 groupcat的文件
  find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
  find / -mtime -1 #查找在系统中最后24小时里修改过的文件
  find / -nouser #查找在系统中属于作废用户的文件
  find / -user fred #查找在系统中属于FRED这个用户的文件
find . -maxdepth 2 -name fred 

xargs传递参数
ls ~/Downloads/ | grep jdk | xargs -i mv  ~/Downloads/{} ./
ls | grep idea | xargs -I {} mv {} ideaIC-12.1.3.tar.gz1
-i
表示 find 传递给xargs的结果 由{}来代替 

-I 我认为是和i差不多,可以这么认为 -i可以用-I {} 来代替 
-p 交互式提问y来确认命令的每次执行。 
-t 在执行前回显各个command 
还有参数-s 和 -x 具体查手册.

抱歉!评论已关闭.