访问文件夹下的文件夹

sh/bash/dash/ksh/zsh等Shell脚本
回复
wangjun403
帖子: 433
注册时间: 2009-07-06 14:26

访问文件夹下的文件夹

#1

帖子 wangjun403 » 2009-10-31 18:31

假设一父文件夹下有N多子文件夹和文件(假设为C程序),子文件夹下又有N多子文件夹和文件。 假设有10层

怎么写脚本可以列出从父文件夹下的最后的子文件夹下的所有文件(包括中间文件夹)?

我的思路
1、访问父文件夹下的文件夹和文件,并列出(以便为修改文件属性)
2、检测父文件夹下的所有东西,如果是文件夹,则进入此文件夹,并列出此文件夹下的所有东西

但我写的脚本,只能列出父文件夹下一个子文件夹里的东西,其他的文件夹就不起作用了

代码: 全选

#!/bin/bash
for loop in `ls`
do 
	test  -d $loop #测试是否为目录
	if [ "$?" = 0 ] #为目录
	then
		echo -e "$loop \t\t is a directory"#显示目录名称

		for loop1 in `ls`
		do
			echo $loop1
#			chmod 744 $loop1
		done
	else
		echo -e "$loop \t\t is a file"
                chmod 644 $loop
	fi
done
#	echo $#
写这个脚本主要是因为复制linux下文件到windows下,然后再复制回来时,所有的文件和文件属性都会改变
文件都会有执行属性,在“ls”的时候,显示的都是绿色,看着比较不舒服

新手,希望有人指点
生命只不过是上帝借你一用的资本!
t3swing
帖子: 1028
注册时间: 2008-11-01 21:42
来自: 树下板凳

Re: 访问文件夹下的文件夹

#2

帖子 t3swing » 2009-10-31 23:43

不如先用find找出要修改的目录,再一起改
民族的脊梁,是踏实做事的人,非只知道骂街的泼妇。
头像
xzap
帖子: 256
注册时间: 2006-08-24 21:25

Re: 访问文件夹下的文件夹

#3

帖子 xzap » 2009-11-01 0:48

这个不必一定要脚本吧,貌似我用pcmanfm,更改一个里面有内容的文件夹属性时会询问是否更改里面的文件和文件夹的属性的。直接是就可以了吧

如果一定要脚本,如果只是改执行属性

代码: 全选

find . -exec chmod -x {} \;
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

Re: 访问文件夹下的文件夹

#4

帖子 cnkilior » 2009-11-01 9:35

代码: 全选

find -type f -exec chmod -x {}
头像
t1e2s3t4
帖子: 11
注册时间: 2009-10-13 12:58

Re: 访问文件夹下的文件夹

#5

帖子 t1e2s3t4 » 2009-11-03 23:12

==========================source code
1 #!/bin/bash
2
3 old=$IFS
4
5 loopDir() {
6 IFS=$'\n'
7 local dir=$1
8 echo input=$dir
9 for path in `ls $1`
10 do
11 #echo -n dir=$dir
12 #echo path=$path
13 local name="$dir/$path"
14 echo -n " name=$name"
15 #[ $path = "." ] || [ $path = ".." ] && continue;
16 if [ -d $name ]
17 then
18 echo " is directory"
19 loopDir $name
20 elif [ -f $name ]
21 then
22 echo " is file"
23 fi
24 done
25 return 1
26 }
27
28 IFS=$old
29 loopDir $1
30 exit 1

============================================= result
ghost@ubuntu:~/study/shell$ ./test.sh /home/ghost
input=/home/ghost
name=/home/ghost/Desktop is directory
input=/home/ghost/Desktop
name=/home/ghost/Documents is directory
input=/home/ghost/Documents
name=/home/ghost/examples.desktop is file
name=/home/ghost/Music is directory
input=/home/ghost/Music
name=/home/ghost/Pictures is directory
input=/home/ghost/Pictures
name=/home/ghost/Pictures/01645_bergs_1600x1200.jpg is file
name=/home/ghost/Pictures/01951_greenlandmystery_1600x1200.jpg is file
name=/home/ghost/Pictures/I Love Music.jpg is file ////////////////////IFS=$'\n'
name=/home/ghost/Pictures/qie2.jpg is file
name=/home/ghost/Pictures/qie.jpg is file
name=/home/ghost/Pictures/sexq.jpg is file
name=/home/ghost/Pictures/xiongmao.jpg is file
name=/home/ghost/Public is directory
input=/home/ghost/Public
name=/home/ghost/study is directory
input=/home/ghost/study
name=/home/ghost/study/perl is directory
input=/home/ghost/study/perl
name=/home/ghost/study/shell is directory
input=/home/ghost/study/shell
name=/home/ghost/study/shell/99.sh is file
name=/home/ghost/study/shell/cdparser.sh is file
name=/home/ghost/study/shell/passwd is file
name=/home/ghost/study/shell/study_log is file
name=/home/ghost/study/shell/test.sh is file
name=/home/ghost/Templates is directory
input=/home/ghost/Templates
name=/home/ghost/Videos is directory
input=/home/ghost/Videos
ghost@ubuntu:~/study/shell$
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 访问文件夹下的文件夹

#6

帖子 aerofox » 2009-11-04 21:51

如果你只是想把所有的文件去掉执行权限,而所有的目录保留执行权限,那么 chmod 本身就搞定:

代码: 全选

chmod -R a-x+X /home/ghost
如果只去掉部分普通文件的执行权限,则可以用 find 配合 chmod,也可以再用上 xargs。
回复