> ## Documentation Index
> Fetch the complete documentation index at: https://dylankenneally-react-native-ssh-sftp-96.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SFTP Directory Operations

> Manage directories on remote servers using SFTP.

export const EditThisPage = ({filePath}) => {
  const REPO_EDIT_BASE_URL = 'https://github.com/dylankenneally/react-native-ssh-sftp/edit/master';
  return <div className="mt-10 rounded-2xl border border-zinc-950/10 bg-zinc-50 px-4 py-3 dark:border-white/10 dark:bg-white/5">
      <p className="m-0 text-sm text-zinc-600 dark:text-zinc-300">
        See something to improve?{' '}
        <a href={`${REPO_EDIT_BASE_URL}/${filePath}`}>Edit this page on GitHub</a>{' '}
        to suggest a change.
      </p>
    </div>;
};

## sftpLs()

Lists the files and directories in the specified path using SFTP.

### Signature

```typescript theme={null}
sftpLs(path: string, callback?: CallbackFunction<LsResult[]>): Promise<LsResult[]>
```

### Parameters

<ParamField path="path" type="string" required>
  The path to list on the remote server.
</ParamField>

<ParamField path="callback" type="CallbackFunction<LsResult[]>" optional>
  Optional callback function to handle the result asynchronously.

  ```typescript theme={null}
  (error: any, response?: LsResult[]) => void
  ```
</ParamField>

### Returns

<ResponseField name="Promise" type="Promise<LsResult[]>">
  A promise that resolves to an array of `LsResult` objects representing the files and directories in the specified path.
</ResponseField>

### LsResult Type

```typescript theme={null}
interface LsResult {
  filename: string;
  isDirectory: boolean;
  modificationDate: string;
  lastAccess: string;
  fileSize: number;
  ownerUserID: number;
  ownerGroupID: number;
  flags: number;
}
```

<ResponseField name="filename" type="string">
  The name of the file or directory.
</ResponseField>

<ResponseField name="isDirectory" type="boolean">
  Whether the item is a directory (`true`) or a file (`false`).
</ResponseField>

<ResponseField name="modificationDate" type="string">
  The last modification date of the file or directory.
</ResponseField>

<ResponseField name="lastAccess" type="string">
  The last access date of the file or directory.
</ResponseField>

<ResponseField name="fileSize" type="number">
  The size of the file in bytes. For directories, this may be `0` or the block size.
</ResponseField>

<ResponseField name="ownerUserID" type="number">
  The user ID of the file or directory owner.
</ResponseField>

<ResponseField name="ownerGroupID" type="number">
  The group ID of the file or directory owner.
</ResponseField>

<ResponseField name="flags" type="number">
  File permission flags.
</ResponseField>

### Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Using Promise
try {
  const files = await client.sftpLs('/home/user');
  files.forEach((file) => {
    console.log(`${file.isDirectory ? 'DIR' : 'FILE'}: ${file.filename}`);
    console.log(`  Size: ${file.fileSize} bytes`);
    console.log(`  Modified: ${file.modificationDate}`);
  });
} catch (error) {
  console.error('Failed to list directory:', error);
}

// Using callback
client.sftpLs('/home/user', (error, files) => {
  if (error) {
    console.error('Failed to list directory:', error);
    return;
  }
  files?.forEach((file) => {
    console.log(`${file.isDirectory ? 'DIR' : 'FILE'}: ${file.filename}`);
  });
});

// Filter only directories
const files = await client.sftpLs('/home/user');
const directories = files.filter((file) => file.isDirectory);
console.log('Directories:', directories.map((d) => d.filename));
```

***

## sftpMkdir()

Creates a directory on the remote server using SFTP.

### Signature

```typescript theme={null}
sftpMkdir(path: string, callback?: CallbackFunction<void>): Promise<void>
```

### Parameters

<ParamField path="path" type="string" required>
  The path of the directory to create on the remote server.
</ParamField>

<ParamField path="callback" type="CallbackFunction<void>" optional>
  Optional callback function to handle the result.

  ```typescript theme={null}
  (error: any, response?: void) => void
  ```
</ParamField>

### Returns

<ResponseField name="Promise" type="Promise<void>">
  A promise that resolves when the directory is created successfully, or rejects with an error if the operation fails.
</ResponseField>

### Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Using Promise
try {
  await client.sftpMkdir('/home/user/new-directory');
  console.log('Directory created successfully');
} catch (error) {
  console.error('Failed to create directory:', error);
}

// Using callback
client.sftpMkdir('/home/user/new-directory', (error) => {
  if (error) {
    console.error('Failed to create directory:', error);
    return;
  }
  console.log('Directory created successfully');
});

// Create nested directories (may require multiple calls)
async function createNestedDir(client: SSHClient, path: string) {
  const parts = path.split('/').filter(Boolean);
  let currentPath = '';

  for (const part of parts) {
    currentPath += '/' + part;
    try {
      await client.sftpMkdir(currentPath);
    } catch (error) {
      // Directory might already exist
      console.log(`Path ${currentPath} already exists or error:`, error);
    }
  }
}

await createNestedDir(client, '/home/user/path/to/nested/dir');
```

***

## sftpRmdir()

Removes a directory on the remote server using SFTP.

<Warning>
  The directory must be empty before it can be removed. If the directory contains files or subdirectories, the operation will fail.
</Warning>

### Signature

```typescript theme={null}
sftpRmdir(path: string, callback?: CallbackFunction<void>): Promise<void>
```

### Parameters

<ParamField path="path" type="string" required>
  The path of the directory to remove on the remote server.
</ParamField>

<ParamField path="callback" type="CallbackFunction<void>" optional>
  Optional callback function to handle the result or error.

  ```typescript theme={null}
  (error: any, response?: void) => void
  ```
</ParamField>

### Returns

<ResponseField name="Promise" type="Promise<void>">
  A promise that resolves when the directory is successfully removed, or rejects with an error if the operation fails.
</ResponseField>

### Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Using Promise
try {
  await client.sftpRmdir('/home/user/empty-directory');
  console.log('Directory removed successfully');
} catch (error) {
  console.error('Failed to remove directory:', error);
}

// Using callback
client.sftpRmdir('/home/user/empty-directory', (error) => {
  if (error) {
    console.error('Failed to remove directory:', error);
    return;
  }
  console.log('Directory removed successfully');
});

// Remove directory with contents (recursive deletion)
async function removeDirectoryRecursive(
  client: SSHClient,
  path: string
): Promise<void> {
  const files = await client.sftpLs(path);

  // Remove all files and subdirectories first
  for (const file of files) {
    const fullPath = `${path}/${file.filename}`;

    if (file.isDirectory) {
      await removeDirectoryRecursive(client, fullPath);
    } else {
      await client.sftpRm(fullPath);
    }
  }

  // Remove the now-empty directory
  await client.sftpRmdir(path);
}

// Usage
try {
  await removeDirectoryRecursive(client, '/home/user/directory-with-files');
  console.log('Directory and all contents removed');
} catch (error) {
  console.error('Failed to remove directory:', error);
}
```

<EditThisPage filePath="docs/api/sftp/directory-operations.mdx" />
