How can I use xargs to copy files that have spaces and quotes in their names?

后端 未结 22 1057
春和景丽
春和景丽 2020-12-02 03:49

I\'m trying to copy a bunch of files below a directory and a number of the files have spaces and single-quotes in their names. When I try to string together find

相关标签:
22条回答
  • 2020-12-02 04:36

    Look into using the --null commandline option for xargs with the -print0 option in find.

    0 讨论(0)
  • 2020-12-02 04:40

    This method works on Mac OS X v10.7.5 (Lion):

    find . | grep FooBar | xargs -I{} cp {} ~/foo/bar
    

    I also tested the exact syntax you posted. That also worked fine on 10.7.5.

    0 讨论(0)
  • 2020-12-02 04:40

    I have found that the following syntax works well for me.

    find /usr/pcapps/ -mount -type f -size +1000000c | perl -lpe ' s{ }{\\ }g ' | xargs ls -l | sort +4nr | head -200
    

    In this example, I am looking for the largest 200 files over 1,000,000 bytes in the filesystem mounted at "/usr/pcapps".

    The Perl line-liner between "find" and "xargs" escapes/quotes each blank so "xargs" passes any filename with embedded blanks to "ls" as a single argument.

    0 讨论(0)
  • 2020-12-02 04:41

    The easiest way to do what the original poster wants is to change the delimiter from any whitespace to just the end-of-line character like this:

    find whatever ... | xargs -d "\n" cp -t /var/tmp
    
    0 讨论(0)
提交回复
热议问题