215

I have a directory that is unpacked, but is in a folder. How can I move the contents up one level? I am accessing CentOS via SSH.

Caleb
  • 69,278
  • 18
  • 196
  • 226

9 Answers9

308

With the folder called 'myfolder' and up one level in the file hierarchy (the point you want it to put) the command would be:

mv myfolder/* .

So for example if the data was in /home/myuser/myfolder then from /home/myuser/ run the command.

Rudu
  • 3,197
  • 2
  • 16
  • 7
  • 54
    You may need to also match `.*` not just `*` if the archive contained dot-files. Also add ` && rmdir myfolder` to the end o that to remove the now extraneous folder. This is save because it will only run if the mv returns success AND because rmdir will not remove a non-empty directory. – Caleb Aug 24 '11 at 20:53
  • Good point on the `.*`. Removing the original folder is both trivial and not asked for so we'll let OP deal with that him/herself. – Rudu Aug 24 '11 at 20:57
  • 2
    @Caleb is it possible to write both ``*`` and ``.*`` in one line? just curiosity – Richard Nov 13 '12 at 20:40
  • 2
    @Richard Yes, it is. The arguments for `mv` will all be sources except the LAST argument which needs to be the target for moving (and in the case of multiple sources, needs to be a folder). – Caleb Nov 13 '12 at 20:50
  • tested, and it works. – Richard Nov 16 '12 at 14:39
  • 2
    I am getting bash: `/bin/mv: Argument list too long` – dragosrsupercool Nov 17 '14 at 07:46
  • What happens, if `myfolder` contains a subdirectory called `myfolder`? That wouldn't work. Is there any clean way of avoiding this, or tell mv to ignore this or replace the source in that case? – white_gecko Nov 18 '16 at 14:25
  • if you are in the folder that you need to move everything up one level, then use mv * ../. – RoundPi Feb 18 '18 at 14:22
  • 1
    How about nested dirs with same name i.e. exercisism/cli/cli and the problem to move cli (last) one level up? – Yurij Oct 06 '20 at 04:56
  • 1
    Worth noting this doesn't seem to work for hidden files (displayed with the "-a" flag) such as .gitignore – Justin Feb 03 '21 at 20:00
  • Justin: yes, and that's why @kenorb's answer is the best one. – Davor Cubranic Mar 24 '21 at 13:16
53

Just to make it crystal clear, taken from Rudu's answer above, and the comments.

If you need to move all the contents, INCLUDING files that start with a . (like .htaccess, .git, etc), you need to include both * and .* in your command. Like so:

 mv subfolder/* subfolder/.* .

That will move contents of subfolder to your current folder. Note the space before the last ".".

Richard
  • 631
  • 5
  • 3
  • 1
    it works but I get `mv: rename fake/. to ./.: Invalid argument` and `mv: rename fake/.. to ./..: Invalid argument` – Maslow Jul 19 '19 at 14:52
  • If you are unsure about target folder have both folder and files inside then run command separately otherwise it will prompt: "no matches found" – tolginho Jan 06 '21 at 06:05
27

This is possible with the following methods:

  • Using rsync:

    rsync -vua --delete-after foo/ .
    
  • Using cp and rmdir:

    cp -vaR foo/. . && rmdir foo/
    
  • Using mv (with dotglob option enabled):

    shopt -s dotglob # Includes filenames beginning with a dot.
    mv -- foo/* .    # Where foo/ is the folder consisting the files.
    shopt -u dotglob # Disables previously enabled dotglob option.
    

    where foo/ is your folder whose content is to be moved one level up.

    See: help shopt and man bash.

  • Using mv (as suggested here):

    mv foo/* foo/.[^.]* . && rmdir foo/
    

    Note: It would miss a file called foo/..bar...

    Note: For POSIX shells, you need [!.].

kenorb
  • 20,250
  • 14
  • 140
  • 164
  • What are the rsync switches `-v`, `-u`, `-a` and `-r` doing? AFAICT, `-v` is verbose, which just makes rsync print more information. `-u` means update - only copy a file if it is newer. That doesn't seem relevant. `-a` means archive. That will copy the owner and permissions of the file, and make the sync recursive. `-r` means recursive, but that's already covered by `-a`. – rjmunro Feb 07 '17 at 16:59
  • `-u` is useful when you don't want to spent extra time of moving data which is already there (especially for slow storage devices). I've removed `-r`, since it's covered by `-a` as you mentioned. – kenorb Feb 07 '17 at 17:03
  • 1
    `mv foo/* foo/.[^.]* .` would miss a file called `foo/..bar..` for instance. Note that for POSIX shells, you need `[!.]`. – Stéphane Chazelas Feb 07 '17 at 17:24
  • 1
    It should be mentioned, that `rsync -vua --delete-after foo/ .` deletes everything else that might already exists in the current directory. – David Apr 24 '18 at 10:12
  • I don't see how the `cp` suggestion will work? It only copies the files, not moves them, and then `rmdir` will fail because "foo" directory is not empty. – Davor Cubranic Mar 24 '21 at 13:11
  • I do love your idea of making use of the `dotglob` shell option to make the `mv /* .` solution work for all files -- very elegant! But I think you also meant to `&& rmdir foo` afterwards. – Davor Cubranic Mar 24 '21 at 13:14
  • One big disadvantage of `rsync` seems to be that it actually copies the files, which is very expensive for large files. `mv` just changes pointers. – Cornelius Roemer Nov 03 '21 at 18:48
12

just issue an mv command

mv (directory)/* .

if you want to delete the directory then add

rm -rf (directory)

Assumed that you are in a parent directory of (directory)

If you are inside the (directory) then

mv * ../
Ranjith Siji
  • 229
  • 2
  • 4
  • 15
    Additional safety tip: When deleting a directory you know should be empty, `rmdir` complains and exits if the directory *isn't* empty, while `rm -r` would instead just have deleted it and all its contents. (It's a [poka-yoke](http://en.wikipedia.org/wiki/Poka-yoke).) – Anko - inactive in protest Jun 14 '14 at 11:40
  • Great point! I can't think of a reason not to do it that way. – Mark Lalor Nov 21 '15 at 15:03
  • @MarkLalor One reason might be the presence of hidden files, like the ubiquitous `.DS_store` in macOS. The `mv` command above won't move those (without `dotglob` set). – AkselA Feb 07 '17 at 15:24
  • The `mv` invocation as written will miss dot files, and the `rm -rf` one is unsafe. If the move worked correctly, you should be able to just `rmdir` the source directory because it will be empty. – Davor Cubranic Mar 24 '21 at 13:08
3

If you get the "Argument list too long" error by using mv * ../ I suggest doing this instead:

find . -name '*.*' -exec mv {} ../ \;
  • find: search a folder
  • -name: match a desired criteria
  • -exec: run the command that follows
  • {}: insert the filename found
  • \;: mark the end of the exec command
tsveti_iko
  • 271
  • 2
  • 3
2

i have one short hand command

cp */* .

this will copy all subfolders content on level up of course you can use move

mv */* .

or assign new distenation

cp */* /destination 
2

The question doesn't specify what shell, so here's a zsh variant:

mv myfolder/*(D) .

(D) ensures both dot and non-dot files are included, but, crucially, not the . and .. objects. It's equivalent to kenorb's dotglob solution.

AkselA
  • 123
  • 5
1

I think surely the best answer is:

mv myfolder/mysubfolder .

Occasaionally you have the problem that both folders have the same name

mv myfolder myfolder.old 
mv myfolder.old/myfolder . 
rmdir myfolder.old
xpusostomos
  • 111
  • 2
-1

for those of you on a shared server you'd have to use something like this

To move the files

mv -v ~/public_html/public_html/* ~/public_html/

To copy the files

cp -a ~/public_html/public_html/* ~/public_html/

  • 2
    The question above asks about moving not copying contents. This would duplicate the files by leaving the originals in a subfolder. Also being a "shared" server or not really doesn't have anything to do with this. – Caleb Jun 14 '14 at 11:04
  • 1
    your right and here is the answer for it... – Ricardo Havoc Jun 14 '14 at 11:11
  • 4
    Re your edit: how is that any different that [the already upvoted answer](http://unix.stackexchange.com/a/19345/1925)? And what does a "shared server" have to do with it? Please [edit] to explain these items if this is going to be a useful contribution that adds value to what is already here. – Caleb Jun 14 '14 at 11:16
  • 1
    I disagree about the shared server not having nothing to do with it. The way the "dir" is entered makes a world of a difference.. – Ricardo Havoc Jun 14 '14 at 11:17
  • 1
    dude why you so mad?... wow.. I was just sharing a little knowledge. We all understand differently and confront different technical problems differently... Have yourself a good day Caleb.. – Ricardo Havoc Jun 14 '14 at 11:19