Moving all files in a directory up one level via command line

If you find that you’ve accidentally nested files and folders too deeply within a directory, it’s easy to resolve by moving the impacted files via a single command.

First, let’s look at an example directory structure:

directory1
-- directory2
-- directory3
-- default.php
-- index.php
directory2
index.php

In this case, the contents of the top-level /directory1 were mistakenly nested to deeply during a file migration.

To move its contents out to the top level, the following command would be used:

mv directory1/* .

However, you’ll need to reconcile the duplicate items or else the files in the top-level destination will be overwritten during the move. In this case, those duplicate files would be:

/directory1/directory2
/directory2

/directory1/index.php
index.php

With those conflicts resolved and the mv command issued, your resulting structure would look like this:

directory1
directory2
directory3
default.php
index.php