这样好,有个直接的命令,大概是 dircmp 吧,用它就行了。lucky_yjw 写了:我就直说算了,我想递归比较两个目录之间的文件.bones7456 写了:嗯,用find + -exec 就更强大了.aerofox 写了:用 ls 加管道是最坏的选择,得自己处理各种各样的特殊符号问题。这个例子没处理隐藏文件,如果确实要处理隐藏文件,可以用 .[^.]* 来匹配。代码: 全选
for f in *; do echo "$f"; done
如何把带空格的文件夹名字作为 for 的参数列表
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 如何把带空格的文件夹名字作为 for 的参数列表
- lucky_yjw
- 帖子: 129
- 注册时间: 2009-07-17 20:57
Re: 如何把带空格的文件夹名字作为 for 的参数列表
没有这命令啊?我是用diff来比较的,但是它好像不支持递归比较!aerofox 写了:这样好,有个直接的命令,大概是 dircmp 吧,用它就行了。lucky_yjw 写了:我就直说算了,我想递归比较两个目录之间的文件.bones7456 写了:嗯,用find + -exec 就更强大了.aerofox 写了:用 ls 加管道是最坏的选择,得自己处理各种各样的特殊符号问题。这个例子没处理隐藏文件,如果确实要处理隐藏文件,可以用 .[^.]* 来匹配。代码: 全选
for f in *; do echo "$f"; done
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 如何把带空格的文件夹名字作为 for 的参数列表
是 dirdiff,默认没安装,源里有的。
代码: 全选
DIRDIFF(1) DIRDIFF(1)
NAME
dirdiff - display differences and merge changes between directory trees
SYNOPSIS
dirdiff [-a|--all] [-o|--only pattern] [-I|--ignore pattern] [-r|--rcs] [-t|--bktag] [-c|--context num] [-b] [-w]
[-B] [-i] [-d] [-S] [-k] dir1 dir2 ...
OPTIONS
-a,--all
Don't exclude any files.
-o,--only pattern
Only process files matching pattern.
-I,--ignore pattern
Don't process files matching pattern.
-r,--rcs
Ignore differences in RCS strings.
-t,--bktag
Ignore differences in Bitkeeper strings.
-c,--context num
Set number of lines of context to show.
- lucky_yjw
- 帖子: 129
- 注册时间: 2009-07-17 20:57
Re: 如何把带空格的文件夹名字作为 for 的参数列表
要下载啊,晕...我本想编一个通用的。顺便问一下,它能递归比较吗?aerofox 写了:是 dirdiff,默认没安装,源里有的。代码: 全选
DIRDIFF(1) DIRDIFF(1) NAME dirdiff - display differences and merge changes between directory trees SYNOPSIS dirdiff [-a|--all] [-o|--only pattern] [-I|--ignore pattern] [-r|--rcs] [-t|--bktag] [-c|--context num] [-b] [-w] [-B] [-i] [-d] [-S] [-k] dir1 dir2 ... OPTIONS -a,--all Don't exclude any files. -o,--only pattern Only process files matching pattern. -I,--ignore pattern Don't process files matching pattern. -r,--rcs Ignore differences in RCS strings. -t,--bktag Ignore differences in Bitkeeper strings. -c,--context num Set number of lines of context to show.
- lucky_yjw
- 帖子: 129
- 注册时间: 2009-07-17 20:57
Re: 如何把带空格的文件夹名字作为 for 的参数列表
谢了,我的输出目录可能与你的不太一样,应该重复7遍:tubunu 写了: 可以的,上面定义的匹配模式是从开始找任意个非空格字符再加任意个空格字符,然后重复8次,再输出后面的所有字符,例如:
drwxr-x--- 2 xxx staff 256 Jul 24 05:18 test 1
'drwxr-x--- ','2 '等都是符合模式的,第8次就是'05:18 ',再输出后面的所有,就是目录名了。由于ls -l的输出是规范的,可以这样做。
代码: 全选
drwxr-xr-x 2 ubuntu ubuntu 4096 2009-09-25 11:21 asd ty fg sdrte et bg st gsdf dfger dfg erg dfg ert egre t ert ert
drwxr-xr-x 7 ubuntu ubuntu 4096 2009-09-24 17:37 C example
drwx------ 2 root root 16384 2009-09-24 11:25 lost+found
代码: 全选
ls -la|grep ^d|sed 's/^\([^ ]* *\)\{7\}//'
.
..
asd ty fg sdrte et bg st gsdf dfger dfg erg dfg ert egre t ert ert
C example
lost+found
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 如何把带空格的文件夹名字作为 for 的参数列表
dirdiff 可以递归比较。
不要用 ls 加管道的方式还选择目录,那样很难处理带特殊字符的名字。对当前目录下所以子目录作处理:
不要用 ls 加管道的方式还选择目录,那样很难处理带特殊字符的名字。对当前目录下所以子目录作处理:
代码: 全选
for f in *; do
if [ -d "$f" ]; then
....
fi
done
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
Re: 如何把带空格的文件夹名字作为 for 的参数列表
diff -rlucky_yjw 写了:没有这命令啊?我是用diff来比较的,但是它好像不支持递归比较!aerofox 写了:这样好,有个直接的命令,大概是 dircmp 吧,用它就行了。lucky_yjw 写了:我就直说算了,我想递归比较两个目录之间的文件.bones7456 写了:嗯,用find + -exec 就更强大了.aerofox 写了:用 ls 加管道是最坏的选择,得自己处理各种各样的特殊符号问题。这个例子没处理隐藏文件,如果确实要处理隐藏文件,可以用 .[^.]* 来匹配。代码: 全选
for f in *; do echo "$f"; done
^_^ ~~~
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
- darkfish
- 帖子: 90
- 注册时间: 2009-09-18 10:03
- 来自: 北京
- 联系:
Re: 如何把带空格的文件夹名字作为 for 的参数列表
最先问的问题可以这样解决

代码: 全选
IFS_OLD=$IFS #IFS备份
IFS=:
for file in $(find -type d -printf "%p$IFS")
do
echo "$file"
echo -------
done



-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 如何把带空格的文件夹名字作为 for 的参数列表
文件名中带冒号怎么办?darkfish 写了:最先问的问题可以这样解决代码: 全选
IFS_OLD=$IFS #IFS备份 IFS=: for file in $(find -type d -printf "%p$IFS") do echo "$file" echo ------- done
![]()
![]()
- darkfish
- 帖子: 90
- 注册时间: 2009-09-18 10:03
- 来自: 北京
- 联系:
Re: 如何把带空格的文件夹名字作为 for 的参数列表
再改个其他的东西吧……这样的逻辑走下去,好像没有什么完美的解决办法……aerofox 写了:文件名中带冒号怎么办?darkfish 写了:最先问的问题可以这样解决代码: 全选
IFS_OLD=$IFS #IFS备份 IFS=: for file in $(find -type d -printf "%p$IFS") do echo "$file" echo ------- done
![]()
![]()
- O_O_BOT
- 帖子: 2461
- 注册时间: 2009-05-20 19:32
Re: 如何把带空格的文件夹名字作为 for 的参数列表
while IFS= read -r -d $'\0' file; do
a[i++]="$file"
done < <(find /tmp -type f -print0)
命令扩展
a[i++]="$file"
done < <(find /tmp -type f -print0)
命令扩展
irc 聊天室
ubuntu-cn 的irc 频道为
irc.ubuntu.com 8001 #ubuntu-cn
UTF8编码 可用 irssi xchat pidgin weechat 登录
http://webchat.freenode.net/?channels=ubuntu-cn
[url]irc://irc.freenode.net/ubuntu-cn[/url]
ubuntu-cn 的irc 频道为
irc.ubuntu.com 8001 #ubuntu-cn
UTF8编码 可用 irssi xchat pidgin weechat 登录
http://webchat.freenode.net/?channels=ubuntu-cn
[url]irc://irc.freenode.net/ubuntu-cn[/url]
- darkfish
- 帖子: 90
- 注册时间: 2009-09-18 10:03
- 来自: 北京
- 联系:
Re: 如何把带空格的文件夹名字作为 for 的参数列表
我靠,牛人……O_O_BOT 写了:while IFS= read -r -d $'\0' file; do
a[i++]="$file"
done < <(find /tmp -type f -print0)
命令扩展
怎么练就这身本事的?
为什么这里用了print0,而在读入的时候用'\0'作分割符?
print0不是用一个空字符来代替print的换行符吗?
while循环在干什么?
< < 又是什么??
read -d之后要$干什么?
在这里向高人请教则个。



-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 如何把带空格的文件夹名字作为 for 的参数列表
如果安个 zsh,可以很简单:
代码: 全选
#!/bin/zsh
for f in **/*(.); do
echo "$f"
done
- O_O_BOT
- 帖子: 2461
- 注册时间: 2009-05-20 19:32
Re: 如何把带空格的文件夹名字作为 for 的参数列表
<(find /tmp -type f -print0) 此为 进程替换 命令输出 通过fd 连接 你把他当文件就是了O_O_BOT 写了:while IFS= read -r -d $'\0' file; do
a[i++]="$file"
done < <(find /tmp -type f -print0)
命令扩展
不用 | 是因为 两边有 子shell, 子的变量影响不了 父shell
其他 -d 是read的flag 无特别, $''是bash的 特殊字符扩展
IFS= 是为read 准备的 保证read 不忽略 什么东西
最后 结果就是 每个文件名 是数组的 一个元素 文件名 可以有任何合法字符\n space ....
irc 聊天室
ubuntu-cn 的irc 频道为
irc.ubuntu.com 8001 #ubuntu-cn
UTF8编码 可用 irssi xchat pidgin weechat 登录
http://webchat.freenode.net/?channels=ubuntu-cn
[url]irc://irc.freenode.net/ubuntu-cn[/url]
ubuntu-cn 的irc 频道为
irc.ubuntu.com 8001 #ubuntu-cn
UTF8编码 可用 irssi xchat pidgin weechat 登录
http://webchat.freenode.net/?channels=ubuntu-cn
[url]irc://irc.freenode.net/ubuntu-cn[/url]