- 论坛徽章:
- 0
|
包括目录里的文件及子目录和其下面的文件。并且link后的属主和权限也都一样
学一样东西最好的方法就是去用,我现在对变量替换很熟悉了。
- [root@localhost tmp]# cat 1.sh
- #!/bin/bash
- link_file(){
- local subdir
- local tmp_1 tmp_2 tmp_3 tmp_4 file_name
- local tmp_in_file tmp_out_file
- subdir=$1
- for i in `ls -a $subdir`
- do
- tmp_in_file=$subdir'/'$i
- tmp_out_file=$out_dir${subdir#$in_dir}'/'$i
- if [ -h $tmp_in_file ]
- then
- tmp_3=$(LANG='C';ls -l $subdir|awk -v name="$i" '{if ($9==name) print $0}')
- tmp_4=${tmp_3/l*-> /}
- case ${tmp_4:0:1} in
- '/')
- ln -s ${tmp_4} $tmp_out_file
- ;;
- '.')
- ln -s ${subdir%/*}${tmp_3/l*-> ../} $tmp_out_file
- ;;
- *)
- ln -s $tmp_in_file $tmp_out_file
- ;;
- esac
- file_name=${tmp_3/->*/}
- eval file_name='('$file_name')'
- chown ${file_name[2]}':'${file_name[3]} $tmp_out_file
- else
- if [ -d $tmp_in_file ]
- then
- if [ $i = . -o $i = .. ]
- then
- continue
- fi
- tmp_1="$subdir/$i"
- tmp_2="$out_dir${subdir#$in_dir}/$i"
- mk_dir "$tmp_1" "$tmp_2"
- link_file $subdir'/'$i
- else
- link $tmp_in_file $tmp_out_file
- fi
- fi
- done
- }
- mk_dir(){
- local dir
- local u g o
- local dir_name
- dir_name="${1##*/}"
- eval dir='('$(LANG='C';ls -l ${1%/*}/|awk -v name="$dir_name" '{if ($9==name) print $0}')')'
- mkdir $2
- chown ${dir[2]}':'${dir[3]} $2
- u=${dir[0]:1:3}
- g=${dir[0]:4:3}
- o=${dir[0]:7:3}
- u=${u//-/}
- g=${g//-/}
- o=${o//-/}
- chmod u=$u,g=$g,o=$o $2
- }
- in_dir=${1%/}
- out_dir=${2%/}
- if [ -e $out_dir ]
- then
- echo "out dir exists"
- exit 1
- fi
- if [ ! -d $in_dir ]
- then
- echo "input is no dir or no exists"
- exit 1
- fi
- mk_dir $in_dir $out_dir
- link_file $in_dir
复制代码
[root@localhost tmp]# ./1.sh /etc /root/ddd
[root@localhost tmp]# find /root/ddd|sed 's#/root/ddd/##g'|sort >/tmp/bb
[root@localhost tmp]# find /etc|sed 's#/etc/##g'|sort >/tmp/aa
[root@localhost tmp]# diff aa bb
91d90
< /etc
1694a1694
> /root/ddd |
|