免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3689 | 回复: 7
打印 上一主题 下一主题

如何拷贝带空格的文件。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-16 19:00 |只看该作者 |倒序浏览
目的: 把文件夹1下把所有的文件拷贝到文件夹2里。文件夹1里还有文件夹,要拷贝的文件也包括文件夹1里子文件夹里的所有文件。
问题: 这些要拷贝的文件的文件名里包含了空格,有的还有两个空格。当我获取了他们的文件名,传给拷贝命令。拷贝命令认为是几个文件,报找不到文件的错误。

       比如:  其中一个文件的文件名aa bb cc.txt
               for i in `find ./ -name "*.txt"`
               do
                   cp $i  "$home/target/"
               done

              # cp aa bb cc.txt, 找不到aa, bb,cc.txt

请高手指点该如何拷贝?

论坛徽章:
0
2 [报告]
发表于 2007-07-16 19:02 |只看该作者
touch  "aa bb"
cp  "aa bb"  cc.txt

论坛徽章:
0
3 [报告]
发表于 2007-07-16 19:04 |只看该作者
不是要对单个文件进行处理。
文件夹1上有很多文件。
通过find来自动获取的所有的文件名。

论坛徽章:
0
4 [报告]
发表于 2007-07-16 19:17 |只看该作者
How do I loop through files with spaces in their name?

    So, you're going to loop through a list of files? How is this list
    stored? If it's stored as text, there probably was already an
    assumption about the characters allowed in a filename. Every
    character except '\0' (NUL) is allowed in a file path on Unix.  So
    the only way to store a list of file names in a file is to
    separate them by a '\0' character (if you don't use a quoting
    mechanism as for xargs input).

    Unfortunately most shells (except zsh) and most standard unix text
    utilities (except GNU ones) can't cope with "\0"
    characters. Moreover, many tools, like "ls", "find", "grep -l"
    output a \n separated list of files. So, if you want to
    postprocess this output, the simpler is to assume that the
    filenames don't contain newline characters (but beware that once
    you make that assumption, you can't pretend anymore your code is
    reliable (and thus can't be exploited)).

    So, if you've got a newline separated list of files in a
    list.txt file, Here are two ways to process it:

    1-

    while IFS= read -r file <&3; do
      something with "$file" # be sure to quote "$file"
    done 3< list.txt
    (if your read doesn't have the "-r" option, either make another
    assumption that filenames don't contain backslashes, or use:

    exec 3<&0
    sed 's/\\/&&/g' < list.txt |
    while IFS= read file; do
      something with "$file" <&3 3<&-
    done
    )

    2-

    IFS="
    " # set the internal field separator to the newline character
      # instead of the default "<space><tab><NL>".
   
    set -f # disable filename generation (or make the assumption that
           # filenames don't contain *, [ or ? characters (maybe more
           # depending on your shell)).
   
    for file in $(cat < list.txt); do
      something with "$file" # it's less a problem if you forget to
                             # quote $file here.
    done
   
    Now, beware that there are things you can do before building
    this list.txt. There are other ways to store filenames. For
    instance, you have the positional parameters.
   
    with:
    set -- ./*.txt
   
    you have the list of txt files in the current directory, and no
    problem with weird characters. Looping through them is just a
    matter of:
   
    for file
    do something with "$file"
    done
   
    You can also escape the separator. For instance, with
   
    find . -exec sh -c 'printf %s\\n "$1" | sed -n '"':1
      \$!{N;b1
      }
      s/|/|p/g;s/\n/|n/g;p'" '{}' '{}' \;
      
    instead of
   
    find . -print
   
    you have the same list of files except that the \n in filenames
    are changed to "|n" and the "|" to "|p". So that you're sure
    there's one filename per line and you have to convert back "|n"
    to "\n" and "|p" to "|" before referring to the file.

论坛徽章:
0
5 [报告]
发表于 2007-07-16 21:25 |只看该作者

回复 #1 cncharman 的帖子

土方法,不知道会不会有其他问题

find ./ -name "*.txt" |sed -e "s/ /\\\ /g" | sed -e "s/^/cp /g" | sed -e "s/$/ \$HOME\/target/g" | bash

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2007-07-17 00:14 |只看该作者
find /path/A-type f -name "*.txt"|xargs -i cp {} /path/B

论坛徽章:
0
7 [报告]
发表于 2007-07-17 21:10 |只看该作者
原帖由 寂寞烈火 于 2007-7-17 00:14 发表
find /path/A-type f -name "*.txt"|xargs -i cp {} /path/B



谢谢六楼的答案,和其它朋友的答案。

论坛徽章:
0
8 [报告]
发表于 2007-07-17 21:30 |只看该作者
6楼的答案真不错,
for file in $idr
do
....
done

格式不太确定,这样可能也行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP