使用:
代码: 全选
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]
本來中間還有一些文件名重復檢查腳本的,嫌複雜,刪掉了
代码: 全选
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