Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367089
  • 博文数量: 83
  • 博客积分: 5322
  • 博客等级: 中校
  • 技术积分: 1057
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-11 11:27
个人简介

爱生活,爱阅读

文章分类

全部博文(83)

文章存档

2015年(1)

2013年(1)

2012年(80)

2011年(1)

分类: LINUX

2012-02-21 18:05:35

内容:
    1、批量建立一些隐藏文件(以.开头命名的普通文件)
    2、批量修改为非隐藏文件

今天搞一个事情,需要将指定目录下面的普通隐藏文件改为非隐藏文件,翻了翻BASH下的流程控制语法,写脚本尝试下。

一、
建立隐藏文件shell:
  1. [admin@localhost zushell]$ cat touchhidefile.sh
  2. #!/bin/bash
  3. #This is used to create file which name begin with "." in dir
  4. function usage()
  5. {
  6. cat <
  7. USAGE `basename $0` dir num
  8. ARGUMENTS:
  9. dir:in which dir the file is create
  10. num: to touch the number of file which name begin with '.'
  11. END
  12. exit 1
  13. }
  14. if [ $# != 2 ]
  15. then
  16. usage
  17. fi
  18. iDIR=$1
  19. iCOUNT=$2
  20. if [ ! -d $iDIR ]
  21. then
  22. mkdir -p $iDIR
  23. fi
  24. echo "Create dir: $iDIR successed"
  25. DATEE=`date +%F%X`
  26. #echo "-----date:$DATEE"
  27. #iDATE=`echo $DATEE | tr -d " "`
  28. #iDATE=`echo ${DATEE// /}`
  29. iDATE=`echo $DATEE | sed 's/ //g'`
  30. #echo "------idate:$iDATE"
  31. iLOOP=0
  32. while [ $iLOOP -lt $iCOUNT ]
  33. do
  34. #echo "pwd:`pwd`"
  35. #echo $iDIR/"."${iLOOP}_rand_${RANDOM}
  36. touch $iDIR/"."${iLOOP}_{user,maker,deler}_rand_${RANDOM}_${iDATE}_{log,report,bak}
  37. iLOOP=$(($iLOOP+1))
  38. done
  39. echo "Creating $iCOUNT file(s)(which name begin with .) in dir:$iDIR"
  40. #echo "ls -al $iDIR"
更改权限并查看:
  1. [admin@localhost zushell]$ chmod u+x touchhidefile.sh
  2. [admin@localhost zushell]$ ll | grep touchhidefile.sh
    -rwxrwxr-x 1 admin admin   924 2012-02-21 17:02 touchhidefile.sh
    [admin@localhost zushell]$ 
建立隐藏文件:
  1. [admin@localhost zushell]$ ./touchhidefile.sh aa 10
  2. Create dir: aa successed
  3. Creating 10 file(s)(which name begin with .) in dir:aa
  4. [admin@localhost zushell]$ ./touchhidefile.sh bb 20
  5. Create dir: bb successed
  6. Creating 20 file(s)(which name begin with .) in dir:bb
  7. [admin@localhost zushell]$
查看隐藏文件:
  1. [admin@localhost zushell]$ ll -A aa bb
  2. aa:
  3. total 0
  4. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_14188_2012-02-2107:01:19PM_bak
  5. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_27180_2012-02-2107:01:19PM_report
  6. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_5787_2012-02-2107:01:19PM_log
  7. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_16494_2012-02-2107:01:19PM_log
  8. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_26057_2012-02-2107:01:19PM_bak
  9. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_8213_2012-02-2107:01:19PM_report
  10. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_user_rand_1238_2012-02-2107:01:19PM_bak
  11. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_user_rand_20561_2012-02-2107:01:19PM_report
  12. ……
  13. bb:
  14. total 0
  15. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_14038_2012-02-2107:01:28PM_report
  16. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_16080_2012-02-2107:01:28PM_log
  17. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_deler_rand_18136_2012-02-2107:01:28PM_bak
  18. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_32528_2012-02-2107:01:28PM_log
  19. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_5710_2012-02-2107:01:28PM_bak
  20. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_maker_rand_8917_2012-02-2107:01:28PM_report
  21. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_user_rand_16423_2012-02-2107:01:28PM_log
  22. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_user_rand_21042_2012-02-2107:01:28PM_report
  23. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 .0_user_rand_4381_2012-02-2107:01:28PM_bak
二、更改隐藏文件
  1. [admin@localhost zushell]$ cat renamehidefile.sh
  2. #!/bin/bash
  3. #rename hide file(which name begin with ".") to regular file(that is :with no ".") in batch
  4. function usage()
  5. {
  6. cat <
  7. USAGE:`basename $0` dir
  8. ARGUMNETS:
  9. dir: rename the files in dir;
  10. DISCRIPTIONS:
  11. This will rename the file in dir whose name begin with '.'
  12. END
  13. exit 1
  14. }
  15. function renamehidefile()
  16. {
  17. cDIR=$1
  18. for LOOP in `ls -a $cDIR`
  19. do
  20. if [ -f ${cDIR}/${LOOP} ]
  21. then
  22. TMP=`echo $LOOP | sed 's/\.//g'`
  23. # echo "--iTMP:${cDIR}/$TMP"
  24. mv ${cDIR}/${LOOP} ${cDIR}/${TMP}
  25. else :
  26. # echo "${cDIR}/${LOOP} is not a regular file but dir"
  27. fi
  28. done
  29. }
  30. if [ $# -le 0 ]
  31. then
  32. usage
  33. else
  34. #read arg and process it
  35. while [ $# -gt 0 ]
  36. do
  37. iDIR=$1
  38. # echo "-----$iDIR"
  39. case $iDIR in
  40. *)
  41. if [ ! -d $iDIR ]
  42. then
  43. echo "Dir :$iDIR is not dir or doesnt exit! dont process it"
  44. else
  45. renamehidefile $iDIR
  46. fi
  47. #shift the arguments
  48. shift
  49. ;;
  50. esac
  51. done
  52. fi
  53. [admin@localhost zushell]$
 更改属性并查看:
  1. [admin@localhost zushell]$ chmod a+x renamehidefile.sh
  2. [admin@localhost zushell]$ ll | grep renamehidefile.sh
    -rwxrwxr-x 1 admin admin  1022 2012-02-21 17:02 renamehidefile.sh
    [admin@localhost zushell]$ 
更改隐藏文件:

  1. [admin@localhost zushell]$ ./renamehidefile.sh aa bb
  2. [admin@localhost zushell]$
查看更改后的文件:

  1. [admin@localhost zushell]$
  2. [admin@localhost zushell]$ ll -A aa bb
  3. aa:
  4. total 0
  5. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_14188_2012-02-2107:01:19PM_bak
  6. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_27180_2012-02-2107:01:19PM_report
  7. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_5787_2012-02-2107:01:19PM_log
  8. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_16494_2012-02-2107:01:19PM_log
  9. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_26057_2012-02-2107:01:19PM_bak
  10. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_8213_2012-02-2107:01:19PM_report
  11. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_1238_2012-02-2107:01:19PM_bak
  12. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_20561_2012-02-2107:01:19PM_report
  13. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_21450_2012-02-2107:01:19PM_log
  14. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 1_deler_rand_26616_2012-02-2107:01:19PM_bak
  15. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 1_deler_rand_4421_2012-02-2107:01:19PM_report
  16. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 1_deler_rand_7664_2012-02-2107:01:19PM_log
  17. ……
  18. bb:
  19. total 0
  20. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_14038_2012-02-2107:01:28PM_report
  21. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_16080_2012-02-2107:01:28PM_log
  22. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_deler_rand_18136_2012-02-2107:01:28PM_bak
  23. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_32528_2012-02-2107:01:28PM_log
  24. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_5710_2012-02-2107:01:28PM_bak
  25. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_maker_rand_8917_2012-02-2107:01:28PM_report
  26. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_16423_2012-02-2107:01:28PM_log
  27. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_21042_2012-02-2107:01:28PM_report
  28. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 0_user_rand_4381_2012-02-2107:01:28PM_bak
  29. -rw-rw-r-- 1 admin admin 0 2012-02-21 19:01 10_deler_rand_11122_2012-02-2107:01:28PM_report

阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~