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
I did not know this method, you always learn something new.
Excellent tip, thanks.
Thank you
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
JAJAJAJAJAJA I didn't put the Arch and Fedora ISOs because the example tried to copy only Deb distros… HAHAHAHAHA.
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.
Sure, it's a regular expression
It is actually an extended pattern. A regular expression is something else, but it looks like it. 🙂
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 😀
It is a pleasure to do my bit. 🙂
In fact, I've always been interested in learning about regular expressions ... do you get excited and do a newbie post about it? 😀
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.
hahahahaha nothing don't worry, you keep commenting that you still learn ahahahaha, the important thing is to share 😀
Very good tip You will never go to bed without learning something new.
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.
Good trick. I did not know him 🙂
A pleasure 😉
But when you put isos you mean to put debian.iso ubuntu.iso /! (Etc etc)? no
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
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
you say you skip directories, however in the examples you only skip files, do you know how to skip a particular directory? Regards.