How to cp copy and exclude internal files or directories (equivalent to rsync –exclude)

If I were to ask you to mention a command to copy a folder to another location, almost everyone would mention cp.

Now, if I tell you that in addition, you must copy all the content of that folder except 1 file, many would be left thinking, and others would mention Rsync, then with the parameter –Exclude you can exclude X file or folder and not copy it. But ... did you know that cp also allows you to do this? ... O_O … Yes friends, cp has its own "exclude" hehe.

For example, we have the folder isos containing: ubuntu.iso, debian.iso y archlinux.iso :

And it happens that we want to copy to the other folder (distros-deb, which is empty) the file debian.iso y ubuntu.iso, that is, all except archlinux.iso

For this we could copy a file and then another, manually, but it is much smarter to use the options that the system offers us, right? … 😀… for example, to do this just:

cp isos/!(archlinux.iso) distros-deb/

And this is enough to copy EVERYTHING contained in the isos directory to distros-deb, everything except archlinux.iso 😉

But suppose we not only have those 3 files, but we also have fedora.iso and chakra.iso ... and we want to do the same, it would also be excluded from the copy fedora.iso and chakra.iso, let's see how to do it:

cp isos/!(archlinux.iso|fedora.iso|chakra.iso) distros-deb/

As you can see, several files or folders can be excluded, we only separate them by a pipe (|) and matter resolved 😀

By this I do not mean that cp is better for everything than rsync ... but, both are excellent tools, for example ... did you know the parameter -u de cp? ... hehe, sure not 😉

Well, nothing more to add ... is this an interesting tip? 😀

regards


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Josh said

    I did not know this method, you always learn something new.
    Excellent tip, thanks.

    1.    KZKG ^ Gaara said

      Thank you

  2.   crotus said

    The tip is very good, I did not know it! It only remains to explain to Arch and Fedora users why you excluded their iso's hehe

    1.    KZKG ^ Gaara said

      JAJAJAJAJAJA I didn't put the Arch and Fedora ISOs because the example tried to copy only Deb distros… HAHAHAHAHA.

  3.   hexborg said

    Here we have to make a couple of points. One is that this only works if the extglob option of the bash is enabled. If it is not, it is activated with this command:

    shopt -s extglob

    It can be put in the .bashrc to always have it activated.

    The other point is that this trick is not an option of the cp command, but it works at the bash level. Which means that it can be used with any command. Not only with cp. You can do the test by writing:

    echo Files: isos /! (archlinux.iso | fedora.iso | chakra.iso)

    Otherwise it is a very useful trick. Along with the -u option to cp, which I also find useful from time to time.

    1.    Daniel Rojas said

      Sure, it's a regular expression

      1.    hexborg said

        It is actually an extended pattern. A regular expression is something else, but it looks like it. 🙂

    2.    KZKG ^ Gaara said

      Yes, the -u in cp is really interesting. I admit that I am a big fan of rsync ... but I don't know, I have an attachment to poor cp hahaha.

      About activating shopt, I did not know, I assumed that this worked automatically, thanks for the tip.

      And yes, I was suspecting it had more to do with Bash than cp, but I hadn't tried making an rm or a cat or something like that yet :)

      Thanks for the comment, I really do 😀

      1.    hexborg said

        It is a pleasure to do my bit. 🙂

        1.    KZKG ^ Gaara said

          In fact, I've always been interested in learning about regular expressions ... do you get excited and do a newbie post about it? 😀

          1.    hexborg said

            LOL!! You already have me. 🙂 With how happy I was without comment… 🙂

            Well, the truth is that he is calling me. 🙂 But I still have to think about it for a bit. It seems difficult to explain.

            1.    KZKG ^ Gaara said

              hahahahaha nothing don't worry, you keep commenting that you still learn ahahahaha, the important thing is to share 😀


  4.   tufadorin said

    Very good tip You will never go to bed without learning something new.

    1.    KZKG ^ Gaara said

      Exactly, and the best of all is that I learn a lot with the comments you leave on the posts, I love learning strange things every day HAHAHA.

  5.   giskard said

    Good trick. I did not know him 🙂

    1.    KZKG ^ Gaara said

      A pleasure 😉

  6.   @Jlcmux said

    But when you put isos you mean to put debian.iso ubuntu.iso /! (Etc etc)? no

  7.   lifter said

    Indeed it turned out to be a very interesting tip. Not only because of the article itself, but also because of the added value of the comments.
    Beautiful community of <º Linux

  8.   MARTHA OF THE WELL said

    Your help has been of no use to me, you should give an example, so that the students can better understand your magnificent technique.
    Thank you for taking the time, I will always remember this page in my heart

  9.   felipe016 said

    you say you skip directories, however in the examples you only skip files, do you know how to skip a particular directory? Regards.