Move files and directories in Ubuntu

In this article, we would cover how to move files and directories in Ubuntu through command-line interface (CLI). Although, some may find themselves not so comfortable when it comes to command-line interface. But, once we understand how it’s done then, using CLI is as easy as using Graphical User Interface (GUI).

We have already covered copying files and directories. And, removing files and directories here.

Here, we would use mv command-line utility. We will first start with the files and then directories in separate sections.

Move files in Ubuntu

mv command-line utility can be used to either move or rename files. We will see how its done using relevant examples.

Example I. Move files from one directory to other.

mv abc.odt ~/Downloads

The above command would move abc.odt file to Downloads directory. So, this is how it is –

mv <file_name> <directory_path>

Note: We have just provided the file_name, use full path of the file if you are not in the current directory in the terminal. For instance,

mv /home/$USER/Documents/abc.odt ~/Downloads

Example II. Rename files through mv command-line utility. Let’s say we have abc.odt and we want to rename it to xyz.odt

mv abc.odt xyz.odt

In this case, the contents of file remains the same. Just that, abc.odt file has been renamed as xyz.odt.

Example III. Use interactive option while overwriting files through mv command-line utility.

mv -i abc.odt ~/Downloads/xyz.odt

If the file xyz.odt already exists, then it will prompt us –

mv: overwrite '~/Downloads/xyz.odt'?

Press y to Confirm, and n to NO.

Next, moving directories.

Move Directories in Ubuntu

There isn’t much of difference when it comes to directories. Couple of examples –

Example I. Let’s say we have directory dir1/ and we want it to move to directory ~/Downloads. Then,

mv dir1/ ~/Downloads

It will put dir1/ under ~/Downloads directory.

Example II. Rename a directory. Continuing with the above example –

mv dir1/ dir2/

This will rename the directory dir1/ to dir2/

In conclusion, we have covered here how to move files and directories in Ubuntu here.

Similar Posts