Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to move all files from one folder to another, except some specific folders.

I tried this to move all files except the .git, assets or build folders:

find .. ! -regex '\(.git\|assets\|build\)' | xargs mv -t ../build

What I except this command to do:

  • Find all files/folders in parent folder that are not .git, assets or build.
  • Move those files/folders to ../build

The folder structure is like this:

.
├── application
├── assets
├── build
├── download_files
├── images
├── mybb
├── system
└── tools

My working directory is the tools folder.

If I execute find .. ! -regex '\(.git\|assets\|build\)' it still shows .git, assets and build.

What's wrong with my command?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

You may have missed some wildcard characters, try

find .. ! -regex '\(.*\/\.git.*\|.*\/assets.*\|.*\/build.*\)'

I also included the path separator (/) so that assets or build appearing as part of a path doesn't match.

share|improve this answer
    
Excellent! Thanks a lot! –  EsTeGe Aug 6 '12 at 14:40
add comment

You can also use bash's extended globs to filter those dirs. First make sure extglob is enabled.

shopt -s extglob

then

mv ../!(assets|build) ../build

The glob won't match files starting with ., so no need to filter out .git. However, if you want to move other files starting with ., then also enable the dotglob shell option.

shopt -s extglob dotglob
mv ../!(.git|assets|build) ../build
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.