How to Move File and Folders via Command Line

To move files and folders via the command line, you can use the mv command, which stands for “move.” The mv command allows you to rename files or move them to a different directory.

Here are some examples of how to use the mv command:

  1. Move a file to a different directory:
   mv /path/to/file.txt /path/to/destination/

This command moves the file file.txt to the destination directory. If the destination directory is not specified, the file will be moved to the current working directory.

  1. Rename a file:
 mv /path/to/oldfile.txt /path/to/newfile.txt

This command renames the file oldfile.txt to newfile.txt.

  1. Move multiple files to a directory:
mv file1.txt file2.txt /path/to/destination/

This command moves file1.txt and file2.txt to the destination directory.

  1. Move a directory to a different location:
mv /path/to/directory /path/to/destination/

This command moves the entire directory and its contents to the destination directory.

  1. Move and overwrite files:
mv -f /path/to/source/file.txt /path/to/destination/

The -f option forces the move and overwrites any existing files in the destination directory with the same name.

Remember to replace /path/to/ with the actual file or directory paths.If you are inside the directory which contains the file that is to be moved to different directory you can omit the path to file and just use the file name.

mv file.txt /path/to/destination/

If you are inside the directory to which the file is to be moved and you have file path. You can use following command:

mv /path/to/source/file.txt . 

here “.” is used to denote the current directory, or you may use

mv /path/to/source/file.txt

If destination path does not have folder , you can use following command to create the folder first.

mkdir -p /folder1/folder2/folder3/destination_folder

The -p option is used to create parent directories as needed, ensuring that the full directory path is created.If any folder doesn’t exist in the destination path it will create the folder.

It’s important to exercise caution when using the mv command, especially when moving or renaming files and directories, as it can permanently modify or overwrite data. Double-check your command and make sure you specify the correct paths to avoid unintended consequences.

Also read How to compress and decompress folders in command line

Important command line commands.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Leave a Comment

Pin It on Pinterest

Scroll to Top