Hostxpeed
Login Get Started →
Getting Started

How to Create and Delete Directories

4 min read
23 views
Jun 12, 2026

Prerequisites

Before creating or deleting directories, make sure you have:

  • SSH access to your VPS
  • Write permissions in the parent directory

⚠️ Deleting directories with rm -rf is permanent and cannot be undone. Use with extreme caution!

Create Directories

Connect to your VPS:

ssh hxroot@YOUR_SERVER_IP -p 22

Method 1: Using mkdir (Single Directory)

mkdir myfolder

Method 2: Create Multiple Directories

mkdir folder1 folder2 folder3

Method 3: Create Nested Directories

mkdir -p parent/child/grandchild

The -p flag creates parent directories if they don''t exist.

Method 4: Create Directory with Permissions

mkdir -m 755 myfolder

Method 5: Create Directory with Verbose Output

mkdir -v myfolder

Output: mkdir: created directory 'myfolder'

Delete Directories

Method 1: Using rmdir (Empty Directories Only)

rmdir myfolder

Only works if directory is empty.

Method 2: Using rm -r (Recursive Delete)

rm -r myfolder

Deletes directory and all contents.

Method 3: Using rm -rf (Force Recursive Delete)

rm -rf myfolder

No confirmation, deletes everything. Use very carefully!

Method 4: Interactive Delete (Confirm Each File)

rm -ri myfolder

Method 5: Delete Multiple Directories

rm -rf folder1 folder2 folder3

Practical Examples

Create website directory structure:

mkdir -p /var/www/mysite/{public_html,logs,backups}
mkdir /var/www/mysite/public_html/{css,js,images}

Create backup directory with timestamp:

mkdir "backup_$(date +%Y%m%d_%H%M%S)"

Delete old backup directories:

rm -rf backup_2024*

Check if Directory Exists Before Creating

#!/bin/bash
if [ ! -d "myfolder" ]; then
    mkdir myfolder
    echo "Directory created"
else
    echo "Directory already exists"
fi

Create Directory Tree in One Line

mkdir -p project/{src,docs,tests,{assets/{css,js,img},config}}

Creates:

project/
  src/
  docs/
  tests/
  assets/
    css/
    js/
    img/
  config/

Safe Delete Script

#!/bin/bash
echo "Directory to delete: $1"
echo "Are you sure? (yes/no)"
read confirmation
if [ "$confirmation" = "yes" ]; then
    rm -rf "$1"
    echo "Directory deleted"
else
    echo "Operation cancelled"
fi

Delete Directories by Age

Delete directories older than 30 days:

find /path -type d -mtime +30 -exec rm -rf {} \;

Common Mistakes to Avoid

  • rm -rf / - Deletes entire system (never run this!)
  • rm -rf . - Deletes current directory and everything in it
  • rm -rf /* - Deletes all files in root (almost as bad as rm -rf /)

✅ You can now create and delete directories on your Hostxpeed VPS.

Was this article helpful?