Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.
cp [exclude-matches] *Music* /target_directory
What should go in place of [exclude-matches] to accomplish this?
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
|
Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.
What should go in place of [exclude-matches] to accomplish this? |
||||
|
|
|
In Bash you can do it by enabling the extglob option, like this (replace ls for cp and add the target directory, of course)
You can later disable extglob with
|
|||||||||||||||||||||
|
|
The You turn it on with In your example, you would initially do:
The full available extended globbing operators are (excerpt from
So, for example, if you wanted to list all the files in the current directory that are not
Of course, normal shell globing works, so the last example could also be written as:
|
|||||||||||||||||||||
|
|
Not in bash (that I know of), but:
I know this is not exactly what you were looking for, but it will solve your example. |
|||||||||||||||||||||
|
|
If you want to avoid the mem cost of using the exec command, I believe you can do better with xargs. I think the following is a more efficient alternative to
|
||||
|
|
|
You can also use a pretty simple
|
|||||
|
|
My personal preference is to use grep and the while command. This allows one to write powerful yet readable scripts ensuring that you end up doing exactly what you want. Plus by using an echo command you can perform a dry run before carrying out the actual operation. For example:
will print out the files that you will end up copying. If the list is correct the next step is to simply replace the echo command with the copy command as follows:
|
|||||||||
|
|
One solution for this can be found with find.
Find has quite a few options, you can get pretty specific on what you include and exclude. Edit: Adam in the comments noted that this is recursive. find options mindepth and maxdepth can be useful in controlling this. |
|||||||||||||||||||||
|
|
this would do it excluding exactly 'Music'
this and that for excluding things like Music?* or *?Music
|
||||
|
|
|
In bash, an alternative to An example that may be what the original poster wanted:
When done, |
||||
|
|
|
The following works lists all This works in
|
|||||||||
|