3

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?

2 Answers 2

3

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.

0
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .