How to Move Files and Directories in Linux with mc command.

How to Move Files and Directories in Linux with mc command.

In this tutorial, we will explain how to use the mv command to move files and directories.

Moving files and directories is one of the most basic tasks you often need to perform on a Linux system.

 

How to Use the mv Command

The mv command (short from move) is used to rename and move and files and directories from one location to another. The syntax for the mv command is as follows:

mv [OPTIONS] SOURCE DESTINATION

The SOURCE can be one, or more files or directories, and DESTINATION can be a single file or directory.

  • When multiple files or directories are given as a SOURCE, the DESTINATION must be a directory. In this case, the SOURCE files are moved to the target directory.
  • If you specify a single file as SOURCE, and the DESTINATION target is an existing directory, then the file is moved to the specified directory.
  • If you specify a single file as SOURCE, and a single file as DESTINATION target then you’re renaming the file .
  • When the SOURCE is a directory and DESTINATION doesn’t exist, SOURCE will be renamed to DESTINATION. Otherwise if DESTINATION exist, it be moved inside the DESTINATION directory.

To move a file or directory, you need to have write permissions on both SOURCE and DESTINATION. Otherwise, you will receive a permission denied error.

For example, to move the file file1 from the current working directory to the /tmp directory you would run:

mv file1 /tmp

To rename a file you need to specify the destination file name:

mv file1 file2

The syntax for moving directories is the same as when moving files. In the following example, if the dir2 directory exists, the command will move dir1 inside dir2. If dir2 doesn’t exist, dir1 will be renamed to dir2:

mv dir1 dir2

 

Moving Multiple Files and Directories

To move multiple files and directories, specify the files you want to move as the source. For example, to move the files file1 and file2 to the dir1 directory you would type:

mv file1 file2 dir1

The mv command also allows you to use pattern matching. For example, to move all pdf files from the current directory to the ~/Documents directory, you would use:

mv *.pdf ~/Documents

 

mv Command Options

The mv command accepts several options that affect default command behavior.

In some Linux distributions, mv may be an alias to the mv command with a custom set of options. For example, in CentOS mv is an alias to mv -i. You can find whether mv is an alias using the type command:

type mv

If mv is alias the output will look something like this:

mv is aliased to `mv -i'

If conflicting options are given, the last one takes precedence.

 

Prompt before overwriting

By default, if the destination file exists, it will be overwritten. To prompt for confirmation, use the -i option:

mv -i file1 /tmp
mv: overwrite '/tmp/file1'?

To overwrite the file type y or Y.

 

Force overwriting

If you try to overwrite a read-only file, the mv command will prompt you whether you want to overwrite the file:

mv -i file1 /tmp
mv: replace '/tmp/file1', overriding mode 0400 (r--------)? 

To avoid being prompted use the -f options:

mv -f file1 /tmp

This option is especially useful when you need to overwrite multiple read-only files.

 

Do not overwrite existing files

The -n option tells mv never to overwrite any existing file:

mv -n file1 /tmp

If a file1 exists the command above will do nothing. Otherwise it will move the file to the /tmp directory.

 

Backing up files

If the destination file exists you can create a backup of it using the -b option:

mv -b file1 /tmp

The backup file will have the same name as the original file with a tilde (~) appended to it.

Use the ls command to verify that the backup was created:

ls /tmp/file1*
/tmp/file1  /tmp/file1~

 

Verbose output

Another option that can be useful is -v. When this option is used, the command prints the name of each moved file:

mv -i file1 /tmp
renamed 'file1' -> '/tmp/file1'

 

Conclusion

The mv command is used to move and rename files and directories.

For more information about the mv command, check the man page or type man mv in your terminal.

New Linux users who are intimidated by the command line can use the GUI file manager to move their files.