zoukankan      html  css  js  c++  java
  • 利用shell批量改名和linux中取随机数的方法

    先批量创建文件

    #!/bin/sh
    
    if [ ! -d /tmp/chenyao ]
    then
      mkdir /tmp/chenyao -p
    fi
    
    cd /tmp/chenyao
    
    for i in {1..10}
    do
      touch chenyao-$i.html
    done

    批量改名

    [root@lamp scripts]# cd /tmp/chenyao/
    [root@lamp chenyao]# pwd
    /tmp/chenyao
    [root@lamp chenyao]# ll 
    total 0
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-1.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-10.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-2.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-3.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-4.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-5.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-6.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-7.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-8.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-9.html
    [root@lamp chenyao]# f=chenyao-1.html
    [root@lamp chenyao]# echo $f
    chenyao-1.html
    [root@lamp chenyao]# echo $f|sed 's#chenyao(.*)html#linux1php#g'    
    linux-1.php
    [root@lamp chenyao]# mv $f `echo $f|sed 's#chenyao(.*)html#linux1php#g'`          //先在命令行中执行命令测试,成功之后再复制到脚本中,这样保险,这是思想
    [root@lamp chenyao]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-10.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-2.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-3.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-4.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-5.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-6.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-7.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-8.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-9.html
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-1.php
    [root@lamp chenyao]# vim piliang2.sh                                //写实现批量改名的脚本
    #!/bin/sh
    
    cd /tmp/chenyao
    
    for f in `ls /tmp/chenyao/`
    do
     mv $f `echo $f|sed 's#chenyao(.*)html#linux1php#g'` &>/dev/null
    done
    [root@lamp scripts]# sh piliang2.sh  
    [root@lamp scripts]# ll /tmp/chenyao/                                //可以看到已经成功改名
    total 0
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-1.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-10.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-2.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-3.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-4.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-5.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-6.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-7.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-8.php
    -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-9.php
    [root@lamp scripts]#   

    老男孩的博客:批量改名

    http://oldboy.blog.51cto.com/2561410/711342

  • 相关阅读:
    MyBatis学习(五)resultMap测试
    MyBatis学习(四)XML配置文件之SQL映射的XML文件
    Mybatis学习(三)XML配置文件之mybatis-config.xml
    每次回顾,总会有一点小收获!
    php数组去重、魔术方法、redis常用数据结构及应用场景
    MySQL使用可重复读作为默认隔离级别的原因
    后端程序猿标配之linux命令
    常用字符串函数
    nginx配置隐藏index.php
    MySQL的sql_mode解析与设置
  • 原文地址:https://www.cnblogs.com/along1226/p/4989931.html
Copyright © 2011-2022 走看看