I have this folder structure:

`----> bar

How can I extract the content of bar into foo?

I tried mv -f bar/* . from within foo.

-f, --force | dont't ask before overwrite

but I get "could not move bar/ajax to foo/ajax because the directory is not empty"

How can I solve this?

  • mv complains for reasons, in the end it depends what exactly you want to happen when there are folders and files to be overwritten... There are also some corner cases to take note of, e.g. what happens when there is a bar/bar/, or other conflicts. Jul 5, 2018 at 11:02
  • Is there anything else in foo? (is it just bar?) Jul 5, 2018 at 18:11
  • @ctrl-alt-delor, there are many other files and folders in foo
    – Black
    Jul 6, 2018 at 8:04
  • It is best to edit the question, so that people see it (not just leave amendments in the comments). Jul 6, 2018 at 8:04

5 Answers 5


mv will overwrite files, but it will refuse to overwrite directories. There's no single command that will merge directories and remove the source directories (which is probably what you want with mv). Even rsync --remove-source-files will leave empty directories.

You can use a combination of commands:

cp -a dev/. .
rm -r dev

which copies everything in dev to the current directory and then removes the dev directory.


rsync -a --remove-source-files dev/ .
find dev -depth -type d -exec rmdir {} \;

which uses rsync to move all the files, and then deletes the empty directories left behind.

  • this is copy, not move.
    – OfusJK
    Jan 10 at 12:04
  • @OfusJK This is copy+delete, which is what "move" is in some situations.
    – muru
    Jan 10 at 14:45

Issue at Hand

You wish to move the contents of foo/bar/ up a level to foo/.

I will be referencing this post on superuser as well as this post from serverfault in the solution.


According to user Stephan202, you are looking for the following commands to execute this task:

cd /path/to/foo/bar
mv * .[^.]* ..

It should also be possible, from within foo/bar/, to run the following command as well:

(shopt -s dotglob; mv -- * ..)

Verify you have the correct permissions as well. If needed run the command with root(sudo) privileges.


Please comment if you have any questions or issues with this answer. I highly suggest you read through each link I have provided thoroughly before attempting the commands. I appreciate feedback to correct any misconceptions and to improve my posts. I can update my answer as needed.

Best of Luck!


Edit: I revised this answer to indicate the pros and cons.

Some manual pages are rather terse because the information is mentioned in the corresponding Info manual.


-f, --force

       do not prompt before overwriting


        Note: mv will only replace empty directories in the destination. Conflicting populated directories are skipped with a diagnostic.


-f, --force

       If a destination file exists but is normally unwritable, standard input is a terminal, and the -f or --force option is not given, mv prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response is not affirmative, the file is skipped.

mv cannot replace non-empty directories but can overwrite existing files owned by the user which are normally unwritable (the owner do not have write permissions, c.f. the quotation).

|-- ajax
`-- bar
    `-- ajax

You can merge the directories foo/ajax and foo/bar/ajax either keeping all files or overwritting identical files as proposed by @muru.

However, this method is unsuitable if the contents of these directories are unrelated. It may be not obvious at first glance so let's see a new tree.

prompt% tree home/network
├── data
│   ├── img
│   │   └── demo.png
│   └── src
│       ├── contact.pdf
│       └── report.pdf
├── hardware
│   ├── api
│   ├── docs
│   └── src
│       └── driver.c
├── misc
│   └── src
│       └── init.py
└── src
    ├── main.c
    └── manager.c

We can move the contents of network/data into network and choose to merge directories having the same name (c.f. below).

prompt% cp -ab home/network/data/* home/network
prompt% rm -r home/network/data/*
prompt% tree home/network
├── data
├── hardware
│   ├── api
│   ├── docs
│   └── src
│       └── driver.c
├── img
│   └── demo.png
├── misc
│   └── src
│       └── init.py
└── src
    ├── contact.pdf
    ├── main.c
    ├── manager.c
    └── report.pdf

As we can see, the final directory network/src may contain unrelated files after moving. If it is undesirable, we can rename files and directories having the same names after moving.

prompt% mv -b -S "_data" home/network/data/* home/network
prompt% tree home/network
|-- data
|-- hardware
|   |-- api
|   |-- docs
|   `-- src
|       `-- driver.c
|-- lib
|   `-- demo.png
|-- misc
|   `-- src
|       `-- init.py
|-- src
|   |-- contact.pdf
|   `-- report.pdf
`-- src_data
    |-- main.c
    `-- manager.c

If you have a directory foo that only contains a directory bar. (bar may contain other stuff).

And you want to rename foo/barfoo, then do:

mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original

You can leave of the -T if your mv does not have this option, however it makes it safer.


Simply use the move command :

cd foo
mv bar/* .

As Jeff Schaller suggest, use

 shopt -s dotglob 

before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting

  • In bash, consider setting dotglob so that this picks up “hidden” files.
    – Jeff Schaller
    Jun 21, 2018 at 12:05
  • Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
    – Kusalananda
    Jun 21, 2018 at 12:06
  • 1
    Does not work, I get `Not possible because the directory is not empty"
    – Black
    Jul 5, 2018 at 10:30

You must log in to answer this question.

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