How to use the command 'robocopy' (with examples)

How to use the command 'robocopy' (with examples)

Robocopy, short for “Robust File Copy,” is a powerful command-line utility in Windows used for copying files and directories. Unlike the standard copy commands, Robocopy is designed for handling complex file copying operations with efficiency. It effectively deals with varied requirements such as mirroring entire directories, handling large file sizes, preserving file attributes, and resuming after network outages. This utility is especially useful in environments where reliability and precision in file copying are paramount. Below are several use cases demonstrating the power and versatility of Robocopy.

Use case 1: Copy all .jpg and .bmp files from one directory to another

Code:

robocopy path\to\source_directory path\to\destination_directory *.jpg *.bmp

Motivation:

This use case is particularly beneficial for organizing multimedia content or migrating specific types of files, such as images, from one directory to another. By focusing on just .jpg and .bmp files, users can efficiently manage storage resources and ensure only relevant files are duplicated.

Explanation:

  • robocopy: Invokes the Robocopy command.
  • path\to\source_directory: Specifies the directory from which files will be copied.
  • path\to\destination_directory: Indicates where the files will be copied to.
  • *.jpg *.bmp: These are the wildcards specifying the type of files to be copied. Only files with .jpg and .bmp extensions will be copied.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023  8:15:12 AM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

    Files : *.jpg
           *.bmp

  ...

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :        10         0         0         0         0         0
   Files :       150       150         0         0         0         0
   Bytes :  48.5 m    48.5 m         0         0         0         0
   ...
   Speed :   8.321 m/s
   Ended : Wednesday, October 4, 2023  8:15:52 AM

Use case 2: Copy all files and subdirectories, including empty ones

Code:

robocopy path\to\source_directory path\to\destination_directory /E

Motivation:

This operation is ideal for backing up an entire directory structure, preserving the hierarchy and any empty folders that might be necessary for organizational purposes or future file additions. It ensures that the destination directory is a perfect replicate of the source directory’s folder structure.

Explanation:

  • /E: This option ensures that all subdirectories, including those that are empty, are copied to the destination directory. This is particularly important if maintaining the exact directory structure is necessary for specific applications or organizational standards.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023  8:18:34 AM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

  Files : *
        
  Options : *.* /S /E /COPY:DAT /R:1000000 /W:30

  ...

------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       180       180         0         0         0         0
   Files :     1,350     1,350         0         0         0         0
   Bytes : 100.43 g   100.43 g         0         0         0         0
   Times :   2:30:12    0:30:12
   ...

   Ended : Wednesday, October 4, 2023 10:48:46 AM

Use case 3: Mirror/Sync a directory, deleting anything not in source and include all attributes and permissions

Code:

robocopy path\to\source_directory path\to\destination_directory /MIR /COPYALL

Motivation:

This use case is crucial for strictly maintaining exact copies between two directory locations. Typically leveraged in data backup solutions, it ensures that any changes in the source are reflected in the destination, while files not present in the source are removed from the destination.

Explanation:

  • /MIR: This stands for mirror, which means the command will make the destination directory mirror the source directory. It deletes any extra files at the destination.
  • /COPYALL: Ensures that all file attributes, including timestamps, security information, and more, are copied along with the files.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023 11:05:20 AM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

  Files : *

  Options : *.* /S /E /COPYALL /PURGE /MIR /R:1000000 /W:30

  ...

------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       120       120         0         0         0         5
   Files :       950       945         5         0         0         3
   Bytes :  85.43 g   85.39 g    0.04 g         0         0         0
   Times :   1:38:14    0:20:41
   ...

   Ended : Wednesday, October 4, 2023 12:43:34 PM

Use case 4: Copy all files and subdirectories, excluding source files that are older than destination files

Code:

robocopy path\to\source_directory path\to\destination_directory /E /XO

Motivation:

This command is useful in scenarios where you want to ensure only newer versions of files are copied over to a destination directory. It is highly effective in situations where updates or modifications should preserve the latest file versions without overwriting newer files with older versions.

Explanation:

  • /E: Ensures all subdirectories are copied, even those that are empty.
  • /XO: Stands for “exclude older,” meaning any files in the source directory that are older than those in the destination are not copied.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023  1:07:11 PM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

  Files : *

  Options : *.* /S /E /COPY:DAT /XO /R:1000000 /W:30

  ...

------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       110       110         0         0         0        10
   Files :     1,240       780       460         0         0         0
   Bytes : 200.12 g   140.06 g   60.06 g         0         0         0
   Times :   1:12:06    0:12:06
   ...

   Ended : Wednesday, October 4, 2023 1:19:17 PM

Use case 5: List all files 50 MB or larger instead of copying them

Code:

robocopy path\to\source_directory path\to\destination_directory /MIN:52428800 /L

Motivation:

This functionality is useful for auditing purposes or for identifying large files that might need to be managed differently, such as being compressed or stored on different media to conserve space or improve system performance.

Explanation:

  • /MIN:52428800: This sets the minimum file size filter to 52428800 bytes (50 MB), so only files of this size or larger are considered.
  • /L: When this option is used, Robocopy only lists the files matching the criteria without copying them.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023  3:02:23 PM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

  Files : *

  Options : *.* /L /MIN:52428800


  ------------------------------------------------------------------------------
                     Newer          Overwrite      
        * EXTRAS    , Larger
  ------------------------------------------------------------------------------
  
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs : 0    0       0             0             0          0            0
   Files : 42       0             42            0            0             0
   Bytes : 3.5 g   0.0 g         3.5 g          0.0 g          0.0 g          0.0 g

   Times : 0:00:11    0:00:11
  
   Ended : Wednesday, October 4, 2023 3:02:34 PM

Use case 6: Allow resuming if network connection is lost and limit retries to 5 and wait time to 15 sec

Code:

robocopy path\to\source_directory path\to\destination_directory /Z /R:5 /W:15

Motivation:

In network environments, interruptions might occur, causing file transfers to stop or fail. This capability to resume the transfer once the connection is restored is critical for large files or tasks with time constraints. Limiting the number of retries and pause duration between retries optimizes productivity and prevents excessive delays.

Explanation:

  • /Z: Enables restartable mode for copying files, which means that if the network drops, Robocopy will pause the operation and resume from where it stopped once the connection is restored.
  • /R:5: Specifies the maximum number of retries for a failed copy attempt, in this case, 5.
  • /W:15: Sets the wait time between retries to 15 seconds.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, October 4, 2023  4:10:45 PM
   Source : path\to\source_directory\
     Dest : path\to\destination_directory\

  Files : *

  Options : *.* /S /Z /R:5 /W:15 /COPY:DAT

  ...

------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       110       110         0         0         0         0
   Files :       420       420         0         0         0         0
   Bytes :  15.02 g   15.02 g         0         0         0         0
   Times :   0:25:22    0:25:22
   Retries :         3
  
   Ended : Wednesday, October 4, 2023 4:37:10 PM

Use case 7: Display help

Code:

robocopy /?

Motivation:

Accessing help is crucial for users who are unfamiliar with Robocopy’s extensive command-line options or need a quick refresher on specific command syntax. This command provides a comprehensive list of all the available options and their descriptions, facilitating better understanding and usage of Robocopy.

Explanation:

  • /?: Typing this at the end of the Robocopy command provides a detailed listing of all available options and instructions on how to use them.

Example Output:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

   Syntax
   ROBOCOPY source destination [file [file]...] [options]

   source :: Source Directory (drive:\path or \\server\share\path)
   destination :: Destination Dir  (drive:\path or \\server\share\path) 
   ...     
   
   File Selection Options :
     /A  : Copy only files with the Archive attribute set.
     /M  : Copy only files with the Archive attribute and reset it.
     /IA:[RASHCNETO] : Include only files with any of the given Attributes set.
     ...     (more options listed)

Conclusion:

Robocopy is an invaluable tool for anyone managing files and directories in a Windows environment. Its robust set of features allows for precise control across a wide variety of file copying scenarios. Whether you are backing up data, syncing files across locations, or filtering files based on attributes like date and size, Robocopy provides a reliable and efficient solution.

Related Posts

How to Use the Command 'nload' (with Examples)

How to Use the Command 'nload' (with Examples)

’nload’ is a useful command-line tool that allows users to visualize network usage directly within the terminal.

Read More
Using the Command `ngrep` to Capture Network Traffic (with Examples)

Using the Command `ngrep` to Capture Network Traffic (with Examples)

ngrep, short for Network Grep, is a command-line network packet analyzer and capturing tool with powerful filtering capabilities using regular expressions.

Read More
How to use the command 'qm nbdstop' (with examples)

How to use the command 'qm nbdstop' (with examples)

The qm nbdstop command is a utility within Proxmox Virtual Environment (PVE), specifically designed to stop an embedded Network Block Device (NBD) server associated with a particular virtual machine (VM).

Read More