Skip to content
6 – Directory Management How-To
1. How to create a single directory:
- Use
mkdir followed by one directory name
- Example:
mkdir myfolder
- This creates a new directory in current location
- Verify creation with
ls -la
2. How to create multiple directories simultaneously:
- Use
mkdir with multiple directory names separated by spaces
- Example:
mkdir folder1 folder2 folder3
- All directories are created in the current working directory
- This is more efficient than creating one at a time
3. How to create multi-level directory structures:
- Use
mkdir -p option for parent directory creation
- Example:
mkdir -p project/src/components
- The
-p flag automatically creates all missing parent directories
- If any parent already exists, it won’t cause an error
4. How to create directories with custom permissions:
- Use
mkdir -m followed by permission mode
- Example:
mkdir -m 750 secure_folder
- Permission format: 3 digits representing owner/group/others
- Common modes: 755 (read/write/execute for all), 750 (restricted access)
5. How to view directory information:
- Use
ls -la command to list all files and directories
- Shows hidden files, permissions, ownership, and sizes
- Displays directory listing in long format with detailed information
6. How to delete empty directories:
- Use
rmdir command followed by directory name
- Example:
rmdir empty_folder
- Only works on empty directories
- Directory must be empty before deletion
7. How to delete directories with contents:
- Use
rm -r option to remove recursively
- Example:
rm -r folder_with_files
- The
-r flag removes directory and all its contents
- Be extremely careful as this operation cannot be undone
8. How to create a complete directory structure:
- Create parent directories:
mkdir -p main/sub1/sub2
- Create additional directories:
mkdir dir1 dir2 dir3
- Set specific permissions:
mkdir -m 750 secure_dir
- Verify creation:
ls -la
9. How to safely delete directories:
- Check contents first:
ls -la directory_name
- Remove empty directories with:
rmdir directory_name
- For non-empty directories use:
rm -r directory_name
- Always double-check before using
-r option
10. How to set up a typical project directory structure:
- Create main project folder:
mkdir project_name
- Create subdirectories:
mkdir -p project_name/{src,docs,test,assets}
- Set appropriate permissions for sensitive directories
- Verify structure with
ls -la project_name
11. How to avoid common mkdir errors:
- Check if directory already exists before creating
- Use
-p flag to prevent parent directory errors
- Ensure proper permissions are set when needed
- Test with
echo or ls before final creation
12. How to create secure directories with restricted access:
- Use
mkdir -m 750 for read/write/execute for owner only
- Set group permissions appropriately
- Avoid using 777 permissions unless absolutely necessary
- Regularly check directory permissions with
ls -la
13. How to create multiple similar directories:
- Use pattern matching with wildcards if supported by shell
- Create a script that runs multiple
mkdir commands
- Example:
mkdir -p project_{a,b,c}/src/{css,js,assets}
- This creates 3 projects each with src subdirectories
14. How to maintain directory structures:
- Regularly remove unused directories with
rm -r
- Use
find command to locate old or unnecessary directories
- Keep track of directory creation dates for maintenance
- Always backup important directory structures before deletion