Skip to content

Using the mkdir Command in the ChromeOS Linux Environment

The mkdir (make directory) command in Linux is used to create new directories. It is a fundamental tool for organizing files and structuring directories efficiently in the ChromeOS Linux (Crostini) environment.

Basic Usage

Creating a Single Directory

To create a new directory, use:

mkdir directory_name

For example:

mkdir my_folder

Creating Multiple Directories

To create multiple directories at once:

mkdir dir1 dir2 dir3

Creating Parent Directories

If you need to create a nested directory structure, use the -p flag:

mkdir -p parent/child/grandchild

This ensures that all parent directories are created if they do not already exist.

Setting Permissions While Creating a Directory

You can set specific permissions while creating a directory using the -m option:

mkdir -m 755 new_directory

This sets the permissions to 755, meaning the owner has full access, while others have read and execute permissions.

Verifying Directory Creation

To confirm that a directory was created, list the contents of the current directory:

ls -l

For a nested directory structure:

tree parent

(If tree is not installed, you can install it using sudo apt install tree.)

Handling Errors

  • If the directory already exists, mkdir will return an error. Use the -p flag to avoid this.
  • Ensure you have the necessary permissions when creating directories in restricted locations by using sudo.

Practical Use Cases

  • Organizing project files:
    mkdir -p ~/projects/my_project/{src,bin,docs}
    
  • Creating directories with specific permissions:
    mkdir -m 700 secure_folder
    
  • Automating directory creation in scripts:
    mkdir -p ~/backup/$(date +%Y-%m-%d)
    

Conclusion

The mkdir command is a simple yet powerful tool for creating and managing directories in the ChromeOS Linux environment. With options for setting permissions, handling nested directories, and organizing files effectively, mkdir is an essential command for Linux users.