
mcopy Command in Linux
The mcopy command in Linux is a versatile utility used to copy files to and from MS-DOS filesystems. This command is part of the mtools package, which provides a collection of utilities to access MS-DOS filesystems from Unix-like systems.
The mcopy command is particularly useful for users who need to transfer files between Linux and MS-DOS filesystems, such as those found on USB flash drives, floppy disks, and other removable media.
Table of Contents
Here is a comprehensive guide to the options available with the mcopy command −
Understanding mcopy Command
The mcopy command allows you to copy files between Linux and MS-DOS filesystems. It supports various options to customize the copying process, making it a powerful tool for file management. The mcopy command can be used to copy single files, multiple files, and entire directories.
Installing the mtools Package
Before using the mcopy command, you need to ensure that the mtools package is installed on your system. You can install it using your package manager. For example, on Debian-based systems like Ubuntu, you can use the following command −
sudo apt-get install mtools

On Red Hat-based systems like CentOS, you can use −
sudo yum install mtools
Syntax of mcopy Command
The basic syntax of the mcopy command is as follows −
mcopy [options] source target
- options − Various options that modify the behavior of the command.
- source − The source file or directory to be copied.
- target − The destination file or directory.
Examples of mcopy Command in Linux
The mcopy command can be used to copy files and directories between Linux and MS-DOS filesystems. Here are some common use cases and examples −
Copying a Single File to an MS-DOS Filesystem
To copy a single file from a Linux filesystem to an MS-DOS filesystem, you can use the following command −
mcopy file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:.
Copying a Single File from an MS-DOS Filesystem
To copy a single file from an MS-DOS filesystem to a Linux filesystem, you can use the following command −
mcopy A:file.txt .

In this example, the mcopy command copies the file file.txt from the root directory of the MS-DOS filesystem mounted as drive A: to the current directory.
Copying Multiple Files to an MS-DOS Filesystem
To copy multiple files from a Linux filesystem to an MS-DOS filesystem, you can use the following command −
mcopy file1.txt file2.txt A:

In this example, the mcopy command copies the files file1.txt and file2.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:.
Copying a Directory to an MS-DOS Filesystem
To copy an entire directory from a Linux filesystem to an MS-DOS filesystem, you can use the -s option to copy directories recursively −
mcopy -s mydir A:

In this example, the mcopy command copies the directory mydir and all its contents from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:.
Copying a Directory from an MS-DOS Filesystem
To copy an entire directory from an MS-DOS filesystem to a Linux filesystem, you can use the -s option to copy directories recursively −
mcopy -s A:mydir .

In this example, the mcopy command copies the directory mydir and all its contents from the root directory of the MS-DOS filesystem mounted as drive A: to the current directory.
Copying Files with Wildcards
The mcopy command supports the use of wildcards to copy multiple files that match a specific pattern. For example, to copy all .txt files from a Linux filesystem to an MS-DOS filesystem, you can use the following command −
mcopy *.txt A:

In this example, the mcopy command copies all files with the .txt extension from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:.
Preserving File Attributes
The mcopy command can preserve file attributes such as modification time and permissions when copying files. To preserve file attributes, you can use the -p option −
mcopy -p file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:, preserving its file attributes.
Overwriting Existing Files
By default, the mcopy command will prompt you before overwriting an existing file on the target filesystem. To overwrite existing files without prompting, you can use the -o option −
mcopy -o file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:, overwriting any existing file with the same name without prompting.
Copying Files with Verbose Output
The mcopy command can provide verbose output to show detailed information about the copying process. To enable verbose output, you can use the -v option −
mcopy -v file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:, providing detailed information about the copying process.
Making a Backup of the Target File
To make a backup of the target file if it already exists, you can use the -b option −
mcopy -b file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:, making a backup of any existing file with the same name.
Preventing Overwriting of Existing Files
To prevent overwriting existing files on the target filesystem, you can use the -n option −
mcopy -n file.txt A:

In this example, the mcopy command copies the file file.txt from the current directory to the root directory of the MS-DOS filesystem mounted as drive A:, without overwriting any existing file with the same name.
Conclusion
The mcopy command in Linux is a powerful utility for copying files to and from MS-DOS filesystems. By understanding how to use this command and its various options, you can effectively manage file transfers between Linux and MS-DOS filesystems, ensuring that your data is correctly copied and preserved.
Whether you're copying individual files, entire directories, or using advanced options to customize the copying process, the mcopy command provides the flexibility and control you need.