KandZ – Tuts

We like to help…!

Linux CLI HowTo 6 🐧 Directory Management How To

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:

  1. Create parent directories: mkdir -p main/sub1/sub2
  2. Create additional directories: mkdir dir1 dir2 dir3
  3. Set specific permissions: mkdir -m 750 secure_dir
  4. Verify creation: ls -la

9. How to safely delete directories:

  1. Check contents first: ls -la directory_name
  2. Remove empty directories with: rmdir directory_name
  3. For non-empty directories use: rm -r directory_name
  4. 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

Leave a Reply