mattrib Command in Linux



The mattrib command in Linux is a powerful tool used to modify file attributes on MS-DOS filesystems. This command is particularly useful for users who need to manage file attributes such as read-only, hidden, system, and archive on FAT filesystems.

Table of Contents

Here is a comprehensive guide to the options available with the mattrib command −

Understanding mattrib Command

The mattrib command is part of the mtools package, which provides a collection of utilities to access MS-DOS filesystems from Unix-like systems. The mattrib command allows you to change the attributes of files and directories on MS-DOS filesystems, making it an essential tool for managing files on FAT-formatted drives.

Installing the mtools Package

Before using the mattrib 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
mattrib Command in Linux1

On Red Hat-based systems like CentOS, you can use −

sudo yum install mtools

Syntax of mattrib Command

The basic syntax of the mattrib command is as follows −

mattrib [options] files
  • options − Various options that modify the behavior of the command.
  • files − The files or directories whose attributes you want to change.

Understanding File Attributes

Before diving into the usage of the mattrib command, it's important to understand the different file attributes that can be modified −

  • Read-Only (r) − Prevents a file from being modified.
  • Hidden (h) − Hides a file from directory listings.
  • System (s) − Marks a file as a system file.
  • Archive (a) − Marks a file as needing to be archived.

How to Use mattrib Command in Linux?

The mattrib command allows you to add or remove these attributes from files and directories −

mattrib
mattrib Command in Linux2

Here are some common options used with the mattrib command −

  • +r − Adds the read-only attribute.
  • -r − Removes the read-only attribute.
  • +h − Adds the hidden attribute.
  • -h − Removes the hidden attribute.
  • +s − Adds the system attribute.
  • -s − Removes the system attribute.
  • +a − Adds the archive attribute.
  • -a − Removes the archive attribute.

Examples of mattrib Command in Linux

Adding the Read-Only Attribute

Let's start with a simple example of adding the read-only attribute to a file. Suppose you have a file named example.txt on an MS-DOS filesystem. You can use the mattrib command to add the read-only attribute −

mattrib +r example.txt
mattrib Command in Linux3

This command sets the read-only attribute for example.txt, preventing it from being modified.

Removing the Read-Only Attribute

To remove the read-only attribute from a file, you can use the -r option. For example, to remove the read-only attribute from example.txt, use the following command −

mattrib -r example.txt
mattrib Command in Linux4

This command removes the read-only attribute, allowing the file to be modified.

Adding the Hidden Attribute

You can use the +h option to add the hidden attribute to a file, making it invisible in directory listings. For example, to hide example.txt, use the following command −

mattrib +h example.txt
mattrib Command in Linux5

This command sets the hidden attribute for example.txt, hiding it from directory listings.

Removing the Hidden Attribute

To remove the hidden attribute from a file, you can use the -h option. For example, to unhide example.txt, use the following command −

mattrib -h example.txt
mattrib Command in Linux6

This command removes the hidden attribute, making the file visible in directory listings.

Adding the System Attribute

The system attribute can be added to a file using the +s option. This marks the file as a system file. For example, to mark example.txt as a system file, use the following command −

mattrib +s example.txt
mattrib Command in Linux7

This command sets the system attribute for example.txt.

Removing the System Attribute

To remove the system attribute from a file, you can use the -s option. For example, to remove the system attribute from example.txt, use the following command −

mattrib -s example.txt
mattrib Command in Linux8

This command removes the system attribute from the file.

Adding the Archive Attribute

The archive attribute can be added to a file using the +a option. This marks the file as needing to be archived. For example, to mark example.txt for archiving, use the following command −

mattrib +a example.txt
mattrib Command in Linux9

This command sets the archive attribute for example.txt.

Removing the Archive Attribute

To remove the archive attribute from a file, you can use the -a option. For example, to remove the archive attribute from example.txt, use the following command −

mattrib -a example.txt
mattrib Command in Linux10

This command removes the archive attribute from the file.

Setting Multiple Attributes

You can set multiple attributes for a file in a single command. For example, to set the read-only and hidden attributes for example.txt, use the following command −

mattrib +rh example.txt
mattrib Command in Linux11

This command sets both the read-only and hidden attributes for example.txt.

Removing Multiple Attributes

Similarly, you can remove multiple attributes in a single command. For example, to remove the read-only and hidden attributes from example.txt, use the following command −

mattrib -rh example.txt
mattrib Command in Linux12

This command removes both the read-only and hidden attributes from example.txt.

Setting Attributes for Multiple Files

You can set attributes for multiple files at once by specifying multiple filenames. For example, to set the read-only attribute for file1.txt and file2.txt, use the following command −

mattrib +r file1.txt file2.txt
mattrib Command in Linux13

This command sets the read-only attribute for both file1.txt and file2.txt.

Using Wildcards

The mattrib command supports the use of wildcards to set attributes for multiple files matching a pattern. For example, to set the read-only attribute for all .txt files in the current directory, use the following command −

mattrib +r *.txt
mattrib Command in Linux14

This command sets the read-only attribute for all .txt files in the current directory.

Checking File Attributes

To check the attributes of a file, you can use the mdir command, which is also part of the mtools package. For example, to check the attributes of example.txt, use the following command −

mdir -/ example.txt
mattrib Command in Linux15

In this example, the A attribute indicates that the archive attribute is set for example.txt.

Conclusion

The mattrib command in Linux is a powerful tool for managing file attributes on MS-DOS filesystems. By understanding how to use this command and its various options, you can effectively manage file attributes such as read-only, hidden, system, and archive.

Whether you're setting attributes for individual files, multiple files, or using wildcards, the mattrib command provides the flexibility and control you need.

Advertisements