> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gbox.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rename box/dir

> Renames a file or a directory. If the target newPath already exists, the rename will fail.



## OpenAPI

````yaml post /boxes/{boxId}/fs/rename
openapi: 3.0.0
info:
  title: GBOX Open API
  description: GBOX Open API Documentation
  version: '1.0'
  contact: {}
servers:
  - url: https://gbox.ai/api/v1
    description: Production Server
security: []
tags: []
paths:
  /boxes/{boxId}/fs/rename:
    post:
      tags:
        - File System
      summary: Rename box/dir
      description: >-
        Renames a file or a directory. If the target newPath already exists, the
        rename will fail.
      operationId: FileSystemController_renameFile
      parameters:
        - name: boxId
          required: true
          in: path
          description: Box ID
          schema:
            example: c9bdc193-b54b-4ddb-a035-5ac0c598d32d
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RenameFile'
      responses:
        '200':
          description: Rename file
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/File'
                  - $ref: '#/components/schemas/Dir'
        '404':
          description: Old path not found
        '409':
          description: New path is already exists
      security:
        - bearer: []
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import GboxSDK from "gbox-sdk";

            const gboxSDK = new GboxSDK({
              apiKey: process.env["GBOX_API_KEY"] // This is the default and can be omitted
            });

            async function main() {
              const box = await gboxSDK.create({ type: "linux" });
              // prepare: write a file
              await box.fs.write({
                path: "/tmp/example.txt",
                content: "Hello, world!",
              });

              // Rename a file
              const renamedFile = await box.fs.rename({
                oldPath: "/tmp/example.txt",
                newPath: "/tmp/example2.txt",
              });

              console.log("File renamed successfully:", renamedFile.path);
            }

            main();
        - lang: Python
          source: |-
            import os
            from gbox_sdk import GboxSDK

            def main():
                gbox_sdk = GboxSDK(api_key=os.environ["GBOX_API_KEY"])

                box = gbox_sdk.create(type="linux")
                # prepare: write a file
                box.fs.write(
                    path="/tmp/example.txt",
                    content="Hello, world!"
                )

                # Rename a file
                renamed_file = box.fs.rename(
                    old_path="/tmp/example.txt",
                    new_path="/tmp/example2.txt"
                )

                print("File renamed successfully:", renamed_file.path)

            if __name__ == "__main__":
                main()
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/gbox/gbox-sdk-go\"\n)\n\nfunc main() {\n\tgboxSDK := gbox.NewGboxSDK(os.Getenv(\"GBOX_API_KEY\"))\n\n\tbox, err := gboxSDK.Create(context.Background(), gbox.CreateOptions{Type: \"linux\"})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to create linux box: %v\", err)\n\t}\n\n\t// prepare: write a file\n\t_, err = box.Fs.Write(context.Background(), &gbox.WriteFileRequest{\n\t\tPath:    \"/tmp/example.txt\",\n\t\tContent: \"Hello, world!\",\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to write file: %v\", err)\n\t}\n\n\t// Rename a file\n\trenamedFile, err := box.Fs.Rename(context.Background(), &gbox.RenameFileRequest{\n\t\tOldPath: \"/tmp/example.txt\",\n\t\tNewPath: \"/tmp/example2.txt\",\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to rename file: %v\", err)\n\t}\n\n\tfmt.Printf(\"File renamed successfully: %s\\n\", renamedFile.Path)\n}"
components:
  schemas:
    RenameFile:
      type: object
      properties:
        workingDir:
          type: string
          description: >-
            Working directory. If not provided, the file will be read from the
            `box.config.workingDir` directory.
          title: FileSystemBase
          example: /home/user/documents
        oldPath:
          type: string
          description: >-
            Old path in the box. If the path does not start with '/', the
            file/directory will be renamed relative to the working directory. If
            the oldPath does not exist, the rename will fail.
          title: RenameFile
          example: /home/user/documents/output.txt
        newPath:
          type: string
          description: >-
            New path in the box. If the path does not start with '/', the
            file/directory will be renamed relative to the working directory. If
            the newPath already exists, the rename will fail.
          title: RenameFile
          example: /home/user/documents/new-name.txt
      title: Rename File/Directory
      description: Request parameters for renaming a file/directory
      required:
        - oldPath
        - newPath
    File:
      type: object
      properties:
        type:
          type: string
          enum:
            - file
          description: File type indicator
          title: File
        name:
          type: string
          description: Name of the file
          title: File
          example: example.txt
        path:
          type: string
          description: Full path to the file in the box
          title: File
          example: /path/to/example.txt
        size:
          type: string
          description: Size of the file
          title: File
          example: 10MB
        mode:
          type: string
          description: File metadata
          title: File
          example: '755'
        lastModified:
          format: date-time
          type: string
          description: Last modified time of the file
          title: File
          example: '2021-01-01T00:00:00Z'
      title: File
      description: File system file representation
      required:
        - type
        - name
        - path
        - size
        - mode
        - lastModified
    Dir:
      type: object
      properties:
        type:
          type: string
          enum:
            - dir
          description: Directory type indicator
          title: Dir
        name:
          type: string
          description: Name of the directory
          title: Dir
          example: example
        path:
          type: string
          description: Full path to the directory in the box
          title: Dir
          example: /path/to/example/
        mode:
          type: string
          description: Directory metadata
          title: Dir
          example: '755'
        lastModified:
          format: date-time
          type: string
          description: Last modified time of the directory
          title: Dir
          example: '2021-01-01T00:00:00Z'
      title: Directory
      description: File system directory representation
      required:
        - type
        - name
        - path
        - mode
        - lastModified
  securitySchemes:
    bearer:
      scheme: bearer
      bearerFormat: JWT
      type: http
      description: >-
        Enter your API Key in the format: Bearer <token>. Get it from
        https://gbox.ai

````