Linux mcopy Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mcopy command, a tool for copying files and directories between different file systems, including FAT, NTFS, and Linux file systems. The lab covers understanding the mcopy command, copying files and directories using mcopy, and exploring advanced mcopy options. To use mcopy, you will need to have the mtools package installed on your system. The lab also notes that the mtools package may need to be installed, as it is a collection of utilities for accessing MS-DOS file systems from Unix-like systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/mkdir -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} linux/cp -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} linux/mv -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} linux/rm -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} linux/touch -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} linux/wildcard -.-> lab-422792{{"`Linux mcopy Command with Practical Examples`"}} end

Understand the mcopy Command

In this step, you will learn about the mcopy command, which is a tool used to copy files and directories between different file systems, including FAT, NTFS, and Linux file systems.

The mcopy command is part of the mtools package, which is a collection of utilities for accessing MS-DOS file systems from Unix-like systems. To use mcopy, you will need to have the mtools package installed on your system.

Let's start by checking if the mtools package is installed on your system:

sudo apt-get update
sudo apt-get install -y mtools

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libfuse2
The following NEW packages will be installed:
  libfuse2 mtools
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.

Now, let's explore the basic usage of the mcopy command:

mcopy -v ~/project/file.txt a:

Example output:

Copying ~/project/file.txt to a:file.txt

In this example, we used the mcopy command to copy the file file.txt from the ~/project directory to the root directory of the first FAT/VFAT file system (typically the first floppy disk or USB drive).

The -v option enables verbose mode, which provides more detailed output during the copy operation.

The a: at the end of the command specifies the destination for the copy operation. In this case, a: represents the first FAT/VFAT file system.

Copy Files and Directories Using mcopy

In this step, you will learn how to use the mcopy command to copy files and directories between different file systems.

Let's start by creating a directory and some files in the ~/project directory:

mkdir ~/project/source_dir
touch ~/project/source_dir/file1.txt ~/project/source_dir/file2.txt

Now, let's copy the entire source_dir directory to the a: device:

mcopy -s ~/project/source_dir a:

Example output:

Copying ~/project/source_dir/file1.txt to a:file1.txt
Copying ~/project/source_dir/file2.txt to a:file2.txt

In this example, we used the -s option to copy the entire directory recursively. The mcopy command will copy each file within the source_dir directory to the root directory of the a: device.

You can also copy a single file using the mcopy command:

mcopy ~/project/source_dir/file1.txt a:

Example output:

Copying ~/project/source_dir/file1.txt to a:file1.txt

In this case, only the file1.txt file is copied to the root directory of the a: device.

Explore Advanced mcopy Options

In this final step, you will learn about some advanced options available with the mcopy command.

One useful option is the -a flag, which preserves the original file attributes when copying files. This can be helpful when working with files that have specific permissions or timestamps:

mcopy -a ~/project/source_dir/file1.txt a:

Example output:

Copying ~/project/source_dir/file1.txt to a:file1.txt

Another option is the -M flag, which allows you to set the file modification time of the copied file to match the original:

mcopy -M ~/project/source_dir/file2.txt a:

Example output:

Copying ~/project/source_dir/file2.txt to a:file2.txt

You can also use the -D option to specify the destination directory when copying files:

mcopy -D destination_dir ~/project/source_dir/file1.txt a:

Example output:

Copying ~/project/source_dir/file1.txt to a:destination_dir/file1.txt

In this example, the file1.txt is copied to the destination_dir directory on the a: device.

Finally, the mcopy command also supports wildcards, allowing you to copy multiple files at once:

mcopy ~/project/source_dir/*.txt a:

Example output:

Copying ~/project/source_dir/file1.txt to a:file1.txt
Copying ~/project/source_dir/file2.txt to a:file2.txt

This will copy all the .txt files from the source_dir directory to the root of the a: device.

Summary

In this lab, you learned about the mcopy command, which is a tool used to copy files and directories between different file systems, including FAT, NTFS, and Linux file systems. You started by checking if the mtools package, which includes the mcopy command, was installed on your system, and then explored the basic usage of the mcopy command to copy a file from your local file system to a FAT/VFAT file system. You then learned how to use the mcopy command to copy an entire directory and its contents to a FAT/VFAT file system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like