BASH copy all files except one

后端 未结 8 548
逝去的感伤
逝去的感伤 2020-11-30 19:13

I would like to copy all files out of a dir except for one named Default.png. It seems that there are a number of ways to do this. What seems the most effective to you?

相关标签:
8条回答
  • 2020-11-30 19:31

    I'd just do:

    cp srcdir/* destdir/ ; rm destdir/Default.png
    

    unless the files are big. Otherwise use e.g.

    find srcdir -type f/ |grep -v Default.png$ |xargs -ILIST cp LIST destdir/
    
    0 讨论(0)
  • 2020-11-30 19:31
    # chattr +i /files_to_exclude
    # cp source destination
    # chattr -i /files_to_exclude
    
    0 讨论(0)
  • 2020-11-30 19:34

    This works great for copying everything except node modules. enjoy and thanks to the answers above I have just added on to it. Rsync is better in my opinion than CP as you can see progress bar without asking for it.

       rsync -av fromDirectory/ ToDirectory/ --exclude=node_modules
    
    0 讨论(0)
  • 2020-11-30 19:37

    Should be as follows:

    cp -r !(Default.png) /dest
    

    If copying to a folder nested in the current folder (called example in the case below) you need to omit that directory also:

    cp -r !(Default.png|example) /example
    
    0 讨论(0)
  • 2020-11-30 19:42

    Simple, if src/ only contains files:

    find src/ ! -name Default.png -exec cp -t dest/ {} +
    

    If src/ has sub-directories, this omits them, but does copy files inside of them:

    find src/ -type f ! -name Default.png -exec cp -t dest/ {} +
    

    If src/ has sub-directories, this does not recurse into them:

    find src/ -type f -maxdepth 1 ! -name Default.png -exec cp -t dest/ {} +
    
    0 讨论(0)
  • 2020-11-30 19:45

    use the shell's expansion parameter with regex

    cp /<path>/[^not_to_copy_file]* .
    

    Everything will be copied except for the not_to_copy_file

    -- if something is wrong with this. please Specify !

    0 讨论(0)
提交回复
热议问题