Skip to content

Mv command linux file move rename

Command definition

The mv command in Linux is used to move files and directories from one location to another, and it can also be used to rename files and directories. When you use mv, the original file is removed from its initial location and placed in the new specified location. If the destination is a different name, the file will be renamed; if it’s the same name, it will overwrite the existing file without warning unless the -i option is used.

Usage examples

  1. Move a file to a different directory: This command moves filename.txt from the current directory to myfolder.
    bash mv filename.txt myfolder/

  2. Rename a file: This command renames filename.txt to newfilename.txt in the current directory.
    bash mv filename.txt newfilename.txt

  3. Move and rename a file: This command moves filename.txt to myfolder and renames it to newfilename.txt.
    bash mv filename.txt myfolder/newfilename.txt

  4. Move multiple files to a directory: This command moves file1.txt and file2.txt to myfolder.
    bash mv file1.txt file2.txt myfolder/

  5. Use the interactive mode: This command prompts for confirmation before overwriting filename.txt in myfolder if it already exists.
    bash mv -i filename.txt myfolder/

  6. Move a directory: This command moves the entire directory myfolder to another directory called newfolder.
    bash mv myfolder/ newfolder/

  7. Move all files with a specific extension: This command moves all .txt files from the current directory to myfolder.
    bash mv *.txt myfolder/

  8. Move files and directories recursively: This command moves myfolder and all its contents to newfolder.
    bash mv -r myfolder/ newfolder/

  9. Rename a directory: This command renames myfolder to newfolder.
    bash mv myfolder newfolder

  10. Move a file and preserve attributes: This command moves filename.txt to myfolder while preserving its attributes.
    bash mv -p filename.txt myfolder/