Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder structure like this:


I want to copy from this server to another just the folder MAT11NA_09_XX_XX_XXX, but not MAT11NA_09_XX_XX_XXXL. When I finish with this I want to copy MAT11NA_09_XX_XX_XXXL but not MAT11NA_09_XX_XX_XXX. This part MAT11NA_09_ doesn't change, the L at the end also doesn't change.

The same for the rest of the folders...

How do I do this in Unix/Linux command line?

Thank you.

share|improve this question

migrated from Oct 2 '09 at 18:52

This question came from our site for professional and enthusiast programmers.

I'm having a lot of trouble reading this - in particular, I don't see any slashes in your description of your directory structure. Any change you could simplify it a bit? – Jefromi Oct 2 '09 at 18:46
Are you asking how to copy something from one server to another? Or how to copy a folder? Or how to cope with long filenames? – Beta Oct 2 '09 at 18:50
WTF is that "belon" tag?!? – Massimo Oct 2 '09 at 19:11

With an "L"

cp -r *L destination

Without an "L"

cp -r *[^L] desyination
share|improve this answer

I know it's stupid, but at least it's straight forward.

This will copy all files/directories ending with L:

cp `ls | grep -E 'L$'` DEST

This will copy all files/directories NOT ending with L:

cp `ls | grep -vE 'L$'` DEST

Replace DEST with your destination.

share|improve this answer
Wouldn't "cp *L DEST" have been a lot simpler? – Massimo Oct 2 '09 at 19:10
ls is not supposed to be used to generate filenames for shell expansion, it is intended for user presentation of filenames. You should really, really use just shell expansion, or find -print0/xargs -0. – Juliano Oct 2 '09 at 23:58

For the ones that you do want the L, just do:

scp -r MAT*L destserver:/whatever/

To do ones that do not have L, you want extended Globs with bash:

shopt -s extglob
scp -r ./!(MAT*L) destever:/whatever/

I haven't test this with scp, but this roughly a way to do it. You could also consider using the find command with something like (for ones not with L).

find . ! -iname 'MAT*L' ! -iname '.*' -print0 | xargs -0 tar zcf foo.tar.gz

and then just copy the tar file.

share|improve this answer

Ok, I've tried all of them.

"scp -r MAT*L destserver:/whatever/" - this one looks like it's working.

shopt extglob
scp -r ./!(MAT*L) destever:/whatever/

For this one I ran the command "shopt extglob", then did the copy. But I got this error "-bash: !: event not found", it's not working.

I tried also the first answer... The folder structure is kinda like this:


I want to take MAT11NA_09_XX_XX_XXX folders and put in a folder on another server.

Then I want to take MAT11NA_09_XX_XX_XXXL folder and put in another folder on another server.

Then...I want to take MAT11NA_11_XX_XX_XXX folders and put in in a folder (different from the folder where I put MAT11NA_09_XX_XX_XXX) on another server.

The same for MAT11NA_11_XX_XX_XXXL...

I don't want to put ALL L folders in one folder from another server, and I don't want to put ALL non-L folders in another one folder from another server.

Hope you understand this time, sorry. Thank you.


Ok, after the last answerr I am using these and it seems they are working:

scp -r MAT11NA_09_*L contentuser@/destination

scp -r MAT11NA_09_*[^L] contentuser@/destination
share|improve this answer
What about the find command option? – Kyle Brandt Oct 2 '09 at 19:41
Or Denis' glob: ./*[^L] – Kyle Brandt Oct 2 '09 at 19:42
You can link your Stack Overflow and Server Fault accounts: – Dennis Williamson Oct 2 '09 at 21:29
The "event not found" error is caused by the exclamation point "!" - you can avoid this error by running set +H – Dennis Williamson Oct 2 '09 at 21:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.