分页: 1 / 1
访问文件夹下的文件夹
发表于 : 2009-10-31 18:31
由 wangjun403
假设一父文件夹下有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”的时候,显示的都是绿色,看着比较不舒服
新手,希望有人指点
Re: 访问文件夹下的文件夹
发表于 : 2009-10-31 23:43
由 t3swing
不如先用find找出要修改的目录,再一起改
Re: 访问文件夹下的文件夹
发表于 : 2009-11-01 0:48
由 xzap
这个不必一定要脚本吧,貌似我用pcmanfm,更改一个里面有内容的文件夹属性时会询问是否更改里面的文件和文件夹的属性的。直接是就可以了吧
如果一定要脚本,如果只是改执行属性
代码: 全选
find . -exec chmod -x {} \;
Re: 访问文件夹下的文件夹
发表于 : 2009-11-01 9:35
由 cnkilior
代码: 全选
find -type f -exec chmod -x {}
Re: 访问文件夹下的文件夹
发表于 : 2009-11-03 23:12
由 t1e2s3t4
==========================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$
Re: 访问文件夹下的文件夹
发表于 : 2009-11-04 21:51
由 aerofox
如果你只是想把所有的文件去掉执行权限,而所有的目录保留执行权限,那么 chmod 本身就搞定:
代码: 全选
chmod -R a-x+X /home/ghost
如果只去掉部分普通文件的执行权限,则可以用 find 配合 chmod,也可以再用上 xargs。