Linux mattrib Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux mattrib command and learn how to manage file and directory attributes. The mattrib command allows you to set various attributes, such as read-only, hidden, and system, on individual files and directories. We will cover the purpose and syntax of the mattrib command, as well as practical examples of modifying file and directory attributes using this tool. This lab will provide you with the knowledge to effectively control the behavior and visibility of files and directories on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/ls -.-> lab-422788{{"`Linux mattrib Command with Practical Examples`"}} linux/touch -.-> lab-422788{{"`Linux mattrib Command with Practical Examples`"}} linux/chown -.-> lab-422788{{"`Linux mattrib Command with Practical Examples`"}} linux/chmod -.-> lab-422788{{"`Linux mattrib Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mattrib Command

In this step, we will explore the purpose and syntax of the mattrib command in Linux. The mattrib command is used to modify file attributes, which are metadata associated with a file that describe its characteristics and behavior.

First, let's understand the basic syntax of the mattrib command:

mattrib [options] filename

The available options for the mattrib command include:

  • -a: Set the archive attribute
  • -c: Clear the archive attribute
  • -r: Set the read-only attribute
  • -h: Set the hidden attribute
  • -s: Set the system attribute
  • -i: Set the immutable attribute
  • -d: Clear the directory attribute

For example, to set the read-only attribute on a file named example.txt, you would run:

sudo mattrib -r example.txt

Example output:

Attributes of example.txt changed.

To clear the read-only attribute, you would use the -c option:

sudo mattrib -c example.txt

Example output:

Attributes of example.txt changed.

The mattrib command allows you to manage file attributes, which can be useful for controlling the behavior and visibility of files on your system.

Modify File Attributes Using the mattrib Command

In this step, we will learn how to use the mattrib command to modify file attributes for individual files and directories.

First, let's create a new file and directory to practice with:

touch example.txt
mkdir example_dir

Now, let's set the read-only attribute on the example.txt file:

sudo mattrib -r example.txt

Example output:

Attributes of example.txt changed.

You can verify the attribute change by using the ls -l command:

ls -l example.txt

Example output:

-r--r--r-- 1 labex labex 0 Apr 12 12:34 example.txt

Notice the r attribute in the file permissions, indicating the file is read-only.

Next, let's set the hidden attribute on the example_dir directory:

sudo mattrib -h example_dir

Example output:

Attributes of example_dir changed.

You can verify the attribute change by using the ls -a command to list all files, including hidden ones:

ls -a example_dir

Example output:

. .. .example_dir

The example_dir directory is now hidden and won't be shown in a regular ls command.

Finally, let's clear the read-only attribute on example.txt:

sudo mattrib -c example.txt

Example output:

Attributes of example.txt changed.

Verify the attribute change by checking the file permissions again:

ls -l example.txt

Example output:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 example.txt

The mattrib command allows you to easily manage file and directory attributes to control their behavior and visibility on your system.

Manage Attributes for Multiple Files and Directories

In this final step, we will explore how to use the mattrib command to manage file and directory attributes for multiple items at once.

First, let's create a few more files and directories to work with:

touch file1.txt file2.txt file3.txt
mkdir dir1 dir2 dir3

Now, let's set the hidden attribute on all the files:

sudo mattrib -h file1.txt file2.txt file3.txt

Example output:

Attributes of file1.txt changed.
Attributes of file2.txt changed.
Attributes of file3.txt changed.

You can verify the attribute changes using the ls -a command:

ls -a

Example output:

. .. .file1.txt .file2.txt .file3.txt dir1 dir2 dir3

Next, let's set the read-only attribute on all the directories:

sudo mattrib -r dir1 dir2 dir3

Example output:

Attributes of dir1 changed.
Attributes of dir2 changed.
Attributes of dir3 changed.

You can verify the attribute changes using the ls -l command:

ls -l

Example output:

dr-xr-xr-x 2 labex labex 4096 Apr 12 12:34 dir1
dr-xr-xr-x 2 labex labex 4096 Apr 12 12:34 dir2
dr-xr-xr-x 2 labex labex 4096 Apr 12 12:34 dir3
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 .file1.txt
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 .file2.txt
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 .file3.txt

The mattrib command allows you to easily manage file and directory attributes for multiple items at once, making it a powerful tool for file and directory management.

Summary

In this lab, you learned the purpose and syntax of the mattrib command in Linux, which is used to modify file attributes. You explored how to set and clear various file attributes such as read-only, hidden, and system using the mattrib command. You also learned how to manage attributes for multiple files and directories, including setting the read-only attribute on a file and the hidden attribute on a directory. The mattrib command provides a way to control the behavior and visibility of files on your system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like