Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61840

How to move files and directories excluding one specific directory to this directory

enter image description here

inside the directory

~/domains/annejulie.blue-world.pl/git

i want to get all files and directories excluding annejulie.blue-world.pl.git directory and move them into that directory (annejulie.blue-world.pl.git)

How to do this in terminal with find and grep command? is it possible?

Upvotes: 5

Views: 7702

Answers (3)

gniourf_gniourf
gniourf_gniourf

Reputation: 46893

With find, assuming your find supports -mindepth and -maxdepth.

find . -mindepth 1 -maxdepth 1 \! -name annejulie.blue-world.pl.git -exec echo mv {} annejulie.blue-word.pl.git \;

This doesn't perform the move, rather it prints on the terminal what operations it will perform. Remove the echo after the -exec word if you're happy with the result.

If you have an mv that supports the -t option, you can use this:

find . -mindepth 1 -maxdepth 1 \! -name annejulie.blue-world.pl.git -exec echo mv -t annejulie.blue-word.pl.git {} +

If your find doesn't support -mindepth and -maxdepth, this POSIX-compatible should do:

find \( \! -name '.' -type d -prune -o \! -type d \) \! -name annejulie.blue-world.pl.git -exec echo mv {} annejulie.blue-word.pl.git \;

it works but it's really ugly.


Of course, the best option is to use

mv -- * annejulie.blue-world.pl.git

and let mv complain that it can't move to a subdirectory of itself.

Upvotes: 2

anubhava
anubhava

Reputation: 785971

You can use find:

cd ~/domains/annejulie.blue-world.pl/git
find . -and -not -path './annejulie.blue-world.pl.git*' -exec mv {} ./annejulie.blue-world.pl.git \;

Upvotes: 0

Seema Kadavan
Seema Kadavan

Reputation: 2648

Execute the following command first in terminal. This extends regexes.

shopt -s extglob

Now you can execute the following mv command

mv !(<file/dir not to be moved>) <Path to dest>

For example, If you are at ~/Test and you need to move all except ~/Test/Dest to ~/Test/Dest, you can execute it as given below, assuming you are at ~/Test

mv !(Dest) ~/Test/Dest

Upvotes: 13

Related Questions