HowTo: Linux / Unix Move Directory Up One Level

Posted on in Categories last updated April 2, 2012

I would like to move a directory one level up into the root directory. I’ve all html files located in /home/apache2/www/html directory, and I’d like to move /home/apache2/www/html to one level up at /home/apache2/www/. How can I accomplish this over the ssh session?

You need to use mv command that moves one or more files or directories from one place to another. You must have have write permission for the directories which the file will move between. The syntax is as follows to move /home/apache2/www/html directory up one level at /home/apache2/www/ directory. Open a command-line terminal (select Applications > Accessories > Terminal), and then type (for remote system use ssh for login):
$ mv /home/apache2/www/html/ /home/apache2/www/
OR
$ mv /home/apache2/www/html/ ..
Please note that Unix / Linux uses the special name .. to refer to the parent directory of a particular directory. In this example move /usb/archives/data/ directory to up one level using the mv command as follows:
$ cd /usb/archives/data/
$ mv -v data/ ..

Sample outputs:

Fig.01: mv Unix / Linux Command
Fig.01: mv Unix / Linux Command

Where,

  • -v : Cause mv to be verbose, showing files after they are moved.

How Do I Move Multiple Directories Up One Level?

The syntax is as follows to moves each file named by a source operand to a destination file in the existing directory named by the directory operand:
$ mv dir1 dir2 ..
In this example move both /home/vivek/projects/foo/ /home/vivek/projects/bar/ directories up one level:
$ mv /home/vivek/projects/foo /home/vivek/projects/bar ..
You can move files using the same syntax. In this example move movie-cd1.avi movie-cd2.avi files up one level:
$ mv /home/vivek/Downloads/movie-cd1.avi /home/vivek/Downloads/movie-cd2.avi ..

How Do I Move All Directories Up One Level?

Use the following syntax to move all directories and files in /home/vivek/data/sales/ to /home/vivek/data/:
$ mv -v /home/vivek/data/sales/ ..
OR
$ cd /home/vivek/data/sales/
$ mv -v sales/ ..

How Do I Move All *.pl or *.c Files Up One Level?

Use the following syntax to move all *.pl (perl files) or *.c (C program files) up one level:
$ mv -v /home/vivek/projects/*.pl ..
$ mv -v /home/vivek/projects/*.c ..

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 4 comments/add one below):

4 comment

  1. Hi, I think these lines:

    $ cd /usb/archives/data/
    $ mv -v data/ ..

    should be changed to:

    $ cd /usb/archives/
    $ mv -v data/ ..

    and these lines:

    $ cd /home/vivek/data/sales/
    $ mv -v sales/ ..

    to:

    $ cd /home/vivek/data/
    $ mv -v sales/ ..

    :)

  2. Corollary: if it happens that your directory has the same name as its parent directory, an error is raised when mv dir/ .. is executed.
    In this case, a simple prior directory rename step is necessary. After the real move operation, the name can be restored again.

    This situation typically occurs when you copy a directory from elswhere accidentally one level lower than an existing target directory with the same name.

Comments are closed.