---
title: FSItem
description: An FSItem object represents a file or a directory in the file system of a Puter. 
---


An `FSItem` object represents a file or a directory in the file system of a Puter.

## Attributes

#### `id` (String)

A string containing the unique identifier of the item. This is a unique identifier generated by Puter when the item is created.

#### `name` (String)

A string containing the name of the item.

#### `path` (String)

A string containing the path of the item. This is the path of the item relative to the root directory of the file system.

#### `isDir` (Boolean)

A boolean value indicating whether the item is a directory. If this is set to `true`, the item is a directory. If this is set to `false`, the item is a file.

#### `created` (Integer)

An integer containing the Unix timestamp of the date and time when the item was created.

#### `modified` (Integer)

An integer containing the Unix timestamp of the date and time when the item was last modified.

#### `accessed` (Integer)

An integer containing the Unix timestamp of the date and time when the item was last accessed.

#### `size` (Integer)

An integer containing the size of the item in bytes. If the item is a directory, this will be `null`.

## Methods

### `read()`

Reads the contents of the file.

__Syntax__

```js
fsitem.read()
```

__Parameters__

None.

__Return value__

A `Promise` that resolves to a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) containing the contents of the file.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            const blob = await item.read();
            puter.print(await blob.text());
        })();
    </script>
</body>
</html>
```

### `write()`

Writes data to the file, overwriting its existing contents.

__Syntax__

```js
fsitem.write(data)
```

__Parameters__

- `data` (String | File | Blob) (required): The data to write to the file.

__Return value__

A `Promise` that resolves to the [`FSItem`](/Objects/fsitem) object of the written file.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.write('Updated contents!');
            puter.print('File updated');
        })();
    </script>
</body>
</html>
```

### `rename()`

Renames the item.

__Syntax__

```js
fsitem.rename(newName)
```

__Parameters__

- `newName` (String) (required): The new name for the item.

__Return value__

A `Promise` that resolves to the [`FSItem`](/Objects/fsitem) object of the renamed item.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.rename('renamed.txt');
            puter.print('File renamed');
        })();
    </script>
</body>
</html>
```

### `move()`

Moves the item to another location.

__Syntax__

```js
fsitem.move(destination)
fsitem.move(destination, overwrite)
fsitem.move(destination, overwrite, newName)
```

__Parameters__

- `destination` (String) (required): The directory to move the item into, or the item's new path.
- `overwrite` (Boolean) (optional): Whether to overwrite an item that already exists at the destination. Defaults to `false`.
- `newName` (String) (optional): The name to give the item at its new location. Defaults to its current name.

__Return value__

A `Promise` that resolves to the [`FSItem`](/Objects/fsitem) object of the moved item.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.move(dirname);
            puter.print(`Moved hello.txt into ${dirname}`);
        })();
    </script>
</body>
</html>
```

### `copy()`

Copies the item into another directory.

__Syntax__

```js
fsitem.copy(destinationDirectory)
fsitem.copy(destinationDirectory, autoRename)
fsitem.copy(destinationDirectory, autoRename, overwrite)
```

__Parameters__

- `destinationDirectory` (String) (required): The directory to copy the item into.
- `autoRename` (Boolean) (optional): Whether to pick a free name when an item with the same name already exists at the destination. Defaults to `false`, which makes the copy fail on a conflict.
- `overwrite` (Boolean) (optional): Whether to overwrite an item that already exists at the destination. Defaults to `false`.

__Return value__

A `Promise` that resolves to the [`FSItem`](/Objects/fsitem) object of the copied item.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.copy(dirname);
            puter.print(`Copied hello.txt into ${dirname}`);
        })();
    </script>
</body>
</html>
```

### `delete()`

Deletes the item.

__Syntax__

```js
fsitem.delete()
```

__Parameters__

None.

__Return value__

A `Promise` that resolves once the item has been deleted.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.delete();
            puter.print('File deleted');
        })();
    </script>
</body>
</html>
```

### `mkdir()`

Creates a new subdirectory inside the item. The item must be a directory, otherwise an error is thrown.

__Syntax__

```js
fsitem.mkdir(name)
fsitem.mkdir(name, autoRename)
```

__Parameters__

- `name` (String) (required): The name of the subdirectory to create.
- `autoRename` (Boolean) (optional): Whether to pick a free name when a directory with that name already exists. Defaults to `false`.

__Return value__

A `Promise` that resolves to the [`FSItem`](/Objects/fsitem) object of the created directory.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            const dir = await puter.fs.stat(dirname);
            await dir.mkdir('subdir');
            puter.print(`Created subdir inside ${dirname}`);
        })();
    </script>
</body>
</html>
```

### `readdir()`

Lists the contents of the item. The item must be a directory, otherwise an error is thrown.

__Syntax__

```js
fsitem.readdir()
```

__Parameters__

None.

__Return value__

A `Promise` that resolves to an array of [`FSItem`](/Objects/fsitem) objects, one for each item in the directory.

__Example__

```html
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            await puter.fs.write(dirname + '/hello.txt', 'Hello, world!');
            const dir = await puter.fs.stat(dirname);
            const children = await dir.readdir();
            children.forEach((child) => puter.print(child.name + '<br>'));
        })();
    </script>
</body>
</html>
```
