剛寫的一個移動腳本

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
xxdaystar
帖子: 225
注册时间: 2006-07-28 14:58
来自: 廣州

剛寫的一個移動腳本

#1

帖子 xxdaystar » 2009-03-25 13:52

估計誰都寫過,被rm累一次映像深刻。名字就叫rr

使用:

代码: 全选

aa$ ls -l
drwxr-xr-x 3 tseong tseong 96 2009-03-25 13:42 aa
drwxr-xr-x 2 tseong tseong 48 2009-03-25 13:41 bb
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:41 dd
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ee
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ff
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ww

aa$ ../rr *
Successfully dusting 
Dirs:  aa bb
Files: dd ee ff ww

aa$ ls -l
总用量 0

全部移到拉圾箱裡面了,按日期分類:

代码: 全选

~/dustbin/2009-03-25$ ls -l
总用量 0
drwxr-xr-x 3 tseong tseong 96 2009-03-25 13:42 aa[134409]
drwxr-xr-x 2 tseong tseong 48 2009-03-25 13:41 bb[134409]
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:41 dd[134409]
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ee[134409]
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ff[134409]
-rw-r--r-- 1 tseong tseong  0 2009-03-25 13:42 ww[134409]
不管怎麼說,這是我寫的第一個“大型”腳本 :em05
本來中間還有一些文件名重復檢查腳本的,嫌複雜,刪掉了

代码: 全选

     1	#!/bin/bash
     2	DUSTBIN="/home/tseong/dustbin/"
     3	DAY=$(date +%Y-%m-%d)
     4	TIME=$(date +%H%M%S)
     6	MYDUSTBIN="$DUSTBIN$DAY/"
     6	
     7	
     8	flag=true
     9	
    10	for filename in "$@";do
    11	
    12		# it's a dir
    13		if [ -d $filename ] ;then
    14			if [ ! -O $filename ]; then
    15				echo "you are not the owner of dir $filename!"
    16				flag=false
    17			else
    18				dirlist="$dirlist $filename"
    19			fi
    20	
    21		# it's a file
    22		elif [ -f $filename ];then
    23			if [ ! -O $filename ];then
    24				echo "you are not the owner of $filename"
    25				flag=false
    26			else	
    27				filelist="$filelist $filename"
    28			fi
    29		
    30		# exists?
    31		elif [ ! -e $filename ]; then	
    32			echo "$filename doesn't exists "
    33			flag=false	
    34	
    35		else 
    36			echo "$filename is not a file or dir"		
    37		fi
    38	
    39	done
    40	
    41	
    42	if [ $flag = true ] ; then
    43	
    44		# does the des. dir exists ? ok:mkdir
    45		[ -d $MYDUSTBIN ] || mkdir $MYDUSTBIN
    46				[ $? ] || exit 0;
    47	
    48		# move and rename the file in the des.
    49		for myfile in $filelist ; do
    50			newaddr="$MYDUSTBIN$myfile[$TIME]"
    51			mv "$myfile"  $newaddr
    52				[ $? ] || exit 0;
    53		done
    54	
    55		# move and rename the dir in the des.
    56		for mydir in $dirlist ; do
    57			newaddr="$MYDUSTBIN$mydir[$TIME]"
    58			mv $mydir $newaddr
    59				[ $? ] || exit 0;
    60		done
    61	
    62		echo "Successfully dusting "
    63		[ -n "$dirlist" ] &&  echo "Dirs: $dirlist"
    64		[ -n "$filelist" ] && echo "Files:$filelist"
    65	
    66	else
    67		echo "Nothing is removed."
    68	
    69	fi
加了行號上去~
上次由 xxdaystar 在 2009-03-25 14:16,总共编辑 2 次。
头像
xxdaystar
帖子: 225
注册时间: 2006-07-28 14:58
来自: 廣州

Re: 剛寫的一個移動腳本

#2

帖子 xxdaystar » 2009-03-25 13:53

可以再寫多一個配合cron定期清理或rotate。
头像
be00
帖子: 805
注册时间: 2008-07-28 19:51
来自: 长沙
联系:

Re: 剛寫的一個移動腳本

#3

帖子 be00 » 2009-03-25 14:04

鼓励,我的还在调试,老在出错,正好可以借鉴你的语法
我的乌斑兔儿:http://zhanggang.net/m/b/b1/ubuntu右键选择在新的窗体打开
右边有一只黄手 ----> 或者注册Dropbox中文版感谢我
头像
lerosua
论坛版主
帖子: 8455
注册时间: 2007-11-29 9:41
联系:

Re: 剛寫的一個移動腳本

#4

帖子 lerosua » 2009-03-25 14:06

给手快的人的?呵呵
回复