Unix shell file copy flattening folder structure

后端 未结 5 1397
走了就别回头了
走了就别回头了 2021-01-30 08:52

On the UNIX bash shell (specifically Mac OS X Leopard) what would be the simplest way to copy every file having a specific extension from a folder hierarchy (including subdirect

5条回答
  •  抹茶落季
    2021-01-30 09:22

    If you really want to run just one command, why not cons one up and run it? Like so:

    $ find /foo  -name '*.txt' | xargs echo | sed -e 's/^/cp /' -e 's|$| /dest|' | bash -sx
    

    But that won't matter too much performance-wise unless you do this a lot or have a ton of files. Be careful of name collusions, however. I noticed in testing that GNU cp at least warns of collisions:

    cp: will not overwrite just-created `/dest/tubguide.tex' with `./texmf/tex/plain/tugboat/tubguide.tex'
    

    I think the cleanest is:

    $ find /foo  -name '*.txt' | xargs -i cp {} /dest
    

    Less syntax to remember than the -exec option.

提交回复
热议问题