Cheat Sheet #day45 - mkdir

Cheat Sheet #day45 - mkdir

mkdir Cheatsheet

Basic Usage

  • Create a Directory

      mkdir directory_name
    

Common Options

  • Create Parent Directories as Needed

      mkdir -p /path/to/new/directory
    
  • Set Permissions for the New Directory

      mkdir -m 755 directory_name
    
  • Display a Message for Each Created Directory

      mkdir -v directory_name
    

Examples

  • Create a Single Directory

      mkdir my_directory
    
  • Create Multiple Directories

      mkdir dir1 dir2 dir3
    
  • Create a Directory with Parent Directories

      mkdir -p /parent/child/grandchild
    
  • Create a Directory with Specific Permissions

      mkdir -m 700 secure_directory
    
  • Create a Directory and Print a Message

      mkdir -v new_directory
    

Practical Tips

  • Combining Options

      mkdir -p -v /path/to/new_directory
    
  • Using with chmod for Complex Permissions

      mkdir new_directory
      chmod 755 new_directory
    
  • Creating Directories from a List in a File

      while IFS= read -r dir; do mkdir -p "$dir"; done < directories.txt
    

Advanced Usage

  • Creating Directories with Variable Names in Scripts

      for dir in dir1 dir2 dir3; do
        mkdir "$dir"
      done
    
  • Using mkdir in a Pipeline

      echo "dir1\ndir2\ndir3" | xargs mkdir -p
    

Examples with Explanations

  • Create Nested Directories with a Single Command

      mkdir -p /home/user/projects/project1/src
      # Creates the entire path even if parent directories do not exist
    
  • Create Directories with Verbose Output

      mkdir -v project
      # Outputs: mkdir: created directory 'project'
    
  • Create a Directory with Custom Permissions

      mkdir -m 755 public_directory
      # Sets the directory permissions to rwxr-xr-x
    
  • Create Multiple Directories with a Script

      for i in {1..5}; do mkdir "project_$i"; done
      # Creates directories: project_1, project_2, ..., project_5
    

Quick Reference

  • Create a Directory

      mkdir directory_name
    
  • Create Parent Directories as Needed

      mkdir -p /path/to/new/directory
    
  • Set Permissions for the New Directory

      mkdir -m 755 directory_name
    
  • Display a Message for Each Created Directory

      mkdir -v directory_name
    
  • Create Multiple Directories at Once

      mkdir dir1 dir2 dir3
    
  • Create Nested Directories with Verbose Output

      mkdir -p -v /path/to/new_directory
    

This cheatsheet covers the essential commands and options for using mkdir effectively, from basic usage to advanced scripting and handling permissions. Adjust the commands according to your specific requirements and environment.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!