Get Free Linux Server Security, Performance & Troubleshooting Tips

Move a folder in Linux using mv command

Move a folder in Linux using mv command
4.5 (90%) 8 votes

Q: How can I move a folder in Unix, Linux and MacOS operating systems using the command line?
A: Folders / directories and files can be moved and renamed using the mv command in the Linux terminal.

mv command syntax

The Linux mv commnand sintax is pretty easy:

mv source target
mv -option origin destiny

Linux mv command examples

Move folder and folder2 to your /tmp directory

mv folder folder2 /tmp/

Move folder and file1 to /home/ directory

mv folder file1 /home/

The previous examples would move files and folders to the new destinations without changing their names.
mv command can be used to move a very big number of files and folders in a single command line.

mv /home/johndoe/* /home/jane/new_folder/

We will move all the files, folders and all the sub-folders that are located inside /home/johndoe/ directory, to the new destination /home/jane/new_folder/.

Here we used a wildcard (the asterisks), which is used in Unix and Linux like systems, to specifiy that all content must be affected (moved in this case).

In this next example, we will move multiple direcctories from different paths to a new folder:

mv /home/john/foo /home/john/docs /home/tracy

or you can also do it from the origin directory:

cd /home/john
mv foo docs /home/tracy

mv command can be configured to show the output of every action it is doing by adding the verbose option -v, for example:

mv /home/john/docs /home/jane/ -v

This is the output:

`/home/john/docs/' -> `/home/jane/docs'

In the previous examples, when you move a folder or file as we’ve done in the previous examples, you will not receive any confirmation before moving the content from one place to another, unless there is a file or folder with the same name on the destination path.

You can force promt before overwriting the files usign the -i option, in this way mv becomes interactive and will ask you for confirmation on the Linux terminal.

mv -i file1.txt /home/jerry/


mv: overwrite `/home/jerry/file1.txt'?

Other mv command options that may be useful in your Linux system administration day to day tasks. The man page from gnu/mv have lot of options you can explore, let’s see some of the most useful mv options:

       -f, --force
              do not prompt before overwriting

       -i, --interactive
              prompt before overwrite

       -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY

       -T, --no-target-directory
              treat DEST as a normal file

       -u, --update

       -v, --verbose
              explain what is being done

       -Z, --context
              set SELinux security context of destination file to default type

If you need to see the full mv command options, check out the man page as you see below:

linux mv command man page

Just type:

man mv

Leave a Reply
