最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】用find和cp组合实现将find出来的文件批量拷贝到目标文件夹

Linux crifan 3740浏览 0评论

【背景】

想要将find出来的一堆文件,用cp去拷贝到目标文件夹。

但是自己试了试find和xarg,结果没搞定。

注:

之前已经折腾了:

【已解决】利用linux管道或重定向将find出来的文件送给rm删除

和:

【整理】Linux命令xargs的用法和参数详解

但是现在还是无法实现所需要的效果。

【折腾过程】

1.后来参考:

xargs cp 的问题

去试试:

CLi@PC-CLI-1 /cygdrive/d/to_del/docbook
$ find . -name *.pdf
./arm_vs_mips/release/pdf/arm_vs_mips.pdf
./binutils_intro/release/pdf/binutils_intro.pdf
./build_website/release/pdf/build_website.pdf
./buy_house/release/pdf/buy_house.pdf
./char_encoding/release/pdf/char_encoding.pdf
./compute_basic/release/pdf/compute_basic.pdf
./crifanlib_csharp/release/pdf/crifanlib_csharp.pdf
./crifanlib_python/release/pdf/crifanlib_python.pdf
./crifan_rec_soft/release/pdf/crifan_rec_soft.pdf
./crosstool_ng/release/pdf/crosstool_ng.pdf
./cross_compile/release/pdf/cross_compile.pdf
./csharp_summary/release/pdf/csharp_summary.pdf
./cygwin_intro/release/pdf/cygwin_intro.pdf
./dma_pl08x_analysis/release/pdf/dma_pl08x_analysis.pdf
./docbook_dev_note/release/pdf/docbook_dev_note.pdf
./english_learn/release/pdf/english_learn.pdf
./fieldbus_intro/release/pdf/fieldbus_intro.pdf
./firmware_download/release/pdf/firmware_download.pdf
./hardware_basic/release/pdf/hardware_basic.pdf
./interrupt_related/release/pdf/interrupt_related.pdf
./json_tutorial/release/pdf/json_tutorial.pdf
./language_dev_basic/release/pdf/language_dev_basic.pdf
./language_summary/release/pdf/language_summary.pdf
./linux_nand_driver/release/pdf/linux_nand_driver.pdf
./linux_wireless/release/pdf/linux_wireless.pdf
./mpeg_vbr/release/pdf/mpeg_vbr.pdf
./nand_get_type/release/pdf/nand_get_type.pdf
./programming_language_basic/release/pdf/programming_language_basic.pdf
./python_beginner_tutorial/release/pdf/python_beginner_tutorial.pdf
./python_intermediate_tutorial/release/pdf/python_intermediate_tutorial.pdf
./python_summary/release/pdf/python_summary.pdf
./python_topic_beautifulsoup/release/pdf/python_topic_beautifulsoup.pdf
./python_topic_re/release/pdf/python_topic_re.pdf
./python_topic_str_encoding/release/pdf/python_topic_str_encoding.pdf
./python_topic_web_scrape/release/pdf/python_topic_web_scrape.pdf
./rec_soft_npp/release/pdf/rec_soft_npp.pdf
./regular_expression/release/pdf/regular_expression.pdf
./runtime_upgrade_linux/release/pdf/runtime_upgrade_linux.pdf
./soft_dev_basic/release/pdf/soft_dev_basic.pdf
./symbology_code128/release/pdf/symbology_code128.pdf
./symbology_gs1128/release/pdf/symbology_gs1128.pdf
./symbology_plessey/release/pdf/symbology_plessey.pdf
./symbology_upc/release/pdf/symbology_upc.pdf
./uboot_starts_analysis/release/pdf/uboot_starts_analysis.pdf
./usb_basic/release/pdf/usb_basic.pdf
./usb_disk_driver/release/pdf/usb_disk_driver.pdf
./usb_hid/release/pdf/usb_hid.pdf
./virtualbox_tutorial/release/pdf/virtualbox_tutorial.pdf
./virutal_machine_tutorial/release/pdf/virutal_machine_tutorial.pdf
./vmware_tutorial/release/pdf/vmware_tutorial.pdf
./website_transfer/release/pdf/website_transfer.pdf
./web_scrape_emulate_login/release/pdf/web_scrape_emulate_login.pdf

CLi@PC-CLI-1 /cygdrive/d/to_del/docbook
$ find . -name *.pdf | xargs -i cp {} ../docbook_pdf/

CLi@PC-CLI-1 /cygdrive/d/to_del/docbook
$ ls ../docbook_pdf/
arm_vs_mips.pdf*       cross_compile.pdf*       hardware_basic.pdf*      programming_language_basic.pdf*    regular_expression.pdf*     usb_disk_driver.pdf*
binutils_intro.pdf*    crosstool_ng.pdf*        interrupt_related.pdf*   python_beginner_tutorial.pdf*      runtime_upgrade_linux.pdf*  usb_hid.pdf*
build_website.pdf*     csharp_summary.pdf*      json_tutorial.pdf*       python_intermediate_tutorial.pdf*  soft_dev_basic.pdf*         virtualbox_tutorial.pdf*
buy_house.pdf*         cygwin_intro.pdf*        language_dev_basic.pdf*  python_summary.pdf*                symbology_code128.pdf*      virutal_machine_tutorial.pdf*
char_encoding.pdf*     dma_pl08x_analysis.pdf*  language_summary.pdf*    python_topic_beautifulsoup.pdf*    symbology_gs1128.pdf*       vmware_tutorial.pdf*
compute_basic.pdf*     docbook_dev_note.pdf*    linux_nand_driver.pdf*   python_topic_re.pdf*               symbology_plessey.pdf*      web_scrape_emulate_login.pdf*
crifan_rec_soft.pdf*   english_learn.pdf*       linux_wireless.pdf*      python_topic_str_encoding.pdf*     symbology_upc.pdf*          website_transfer.pdf*
crifanlib_csharp.pdf*  fieldbus_intro.pdf*      mpeg_vbr.pdf*            python_topic_web_scrape.pdf*       uboot_starts_analysis.pdf*
crifanlib_python.pdf*  firmware_download.pdf*   nand_get_type.pdf*       rec_soft_npp.pdf*                  usb_basic.pdf*

达到效果了。

 

man xargs中,对应的参数的解释:

       -I replace-str
              Replace occurrences of replace-str in the initial-arguments with names read from standard input.  Also, unquoted blanks do not terminate in‐
              put items; instead the separator is the newline character.  Implies -x and -L 1.

       -i [replace-str], --replace[=replace-str]
              This  option  is a synonym for -Ireplace-str if replace-str is specified.  If the replace-str argument is missing, the effect is the same as
              -I{}.  This option is deprecated; use -I instead.

 

2.但是对于上述的,大括号,即curly bracket的含义,不是很清楚:

【已解决】Linux shell中的空的(左右)大括号的含义

 

【总结】

find . -name *.pdf | xargs -i cp {} ../docbook_pdf/

的写法的含义解释:

  • find . -name *.pdf :递归式的找出当前文件夹及其子文件下的所有.pdf文件
  • xargs -i xxx :其中xxx==cp {} ../docbook_pdf/,表示将输入的内容,用{}替换
    • cp {} ../docbook_pdf/ :对于每一个find出来的文件,拷贝到对应的目标文件夹中。

转载请注明:在路上 » 【已解决】用find和cp组合实现将find出来的文件批量拷贝到目标文件夹

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
59 queries in 0.108 seconds, using 18.85MB memory