MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | mv -- move files/directories
  1. Notes
  2. Examples
  3. Command Help (man mv)

  1. Notes
    mv source_file target_file
    
    mv source_file ... target_directory
    
    mv source_directory ... target_directory
    
    target_file can be a file name, a directory (in this case the file will be moved to another directory), or a directory/file name (in this case the file will be moved to another directory and renamed).

    Moving a directory will move the directory and all its contents.

    When moving files and directories, the inode number and timestamp of these files and directories are not changed.
  2. Examples
    • Moving file:
      • Move file1 from folder1 to folder2 (see below the options -i and -n to check if the destination file already exists).
        $ mv folder1/file1 folder2/
        
        # Optionally you can also specify the file name to be used for the destination folder
        $ mv folder1/file1 folder2/file1
        
      • Move file1 from folder1 to folder2 (renaming file1 to file2):
        $ mv folder1/file1 folder2/file2
        
    • Renaming file:
      $ mv file1 file2
      
    • Moving directory:
      $ mv folder1/ folder3/
      
    • Renaming directory:
      $ mv folder1 folder3
      
    • You can use both cp and rm commands to move files.
      First copy file1 from folder1 to folder2 and then delete file1 in folder1:
      $ cp folder1/file1 folder2/ && rm folder1/file1
      
  3. Command Help (man mv)
    The following options can be used:
    -n Do not overwrite an existing file.
       (The -n option overrides any previous -f or -i options.)
    
    -f Do not prompt for confirmation before overwriting the destination path.
       (The -f option overrides any previous -i or -n options.)
    
    -i Cause mv to write a prompt to standard error before moving a file that would overwrite an existing file.
       If the response from the standard input begins with the character 'y' or 'Y', the move is attempted.
       (The -i option overrides any previous -f or -n options.)
    
    -v Cause mv to be verbose, showing files after they are moved.
    
© 2025 mtitek