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

# Create Files and Directories Part 2

> Guide to common Unix file and directory operations including creating files and directories, copying, moving and renaming, deleting, and using absolute or relative paths with practical examples.

This guide covers the most common file and directory operations on Unix-like systems: creating files, creating directories, copying, moving (including renaming), and deleting. Every command accepts absolute or relative paths — examples show both forms where helpful.

## Creating files

Use `touch` to create an empty file or update an existing file's timestamp.

Create a file named `Receipt.pdf` in the current directory:

```bash theme={null}
$ touch Receipt.pdf
```

Create the same file at a different location using an absolute path:

```bash theme={null}
$ touch /home/jane/Receipt.pdf
```

Or using a relative path (one directory up, then into `jane`):

```bash theme={null}
$ touch ../jane/Receipt.pdf
```

Both absolute and relative paths work the same; choose the form appropriate to your current working directory.

## Creating directories

Use `mkdir` (make directory) to create a folder:

```bash theme={null}
$ mkdir Receipts
```

To create nested directories in one command, use `-p`:

```bash theme={null}
$ mkdir -p 2026/April/Receipts
```

## Copying files and directories

The `cp` command copies files and directories. General form:

```bash theme={null}
# cp [source] [destination]
```

Copy a file into a directory:

```bash theme={null}
$ cp Receipt.pdf Receipts/
```

Common conventions:

* A trailing slash on a directory (`Receipts/`) is not required but makes it clear you mean a directory.
* To copy and rename in a single step, specify a different destination filename:

```bash theme={null}
$ cp Receipt.pdf Receipts/Receipt-copy.pdf
```

To copy a directory and everything inside it, use the recursive `-r` (or `-a` to preserve attributes) option:

```bash theme={null}
$ cp -r Receipts "Backup of Receipts"
```

Notes about destination behavior:

* If the destination exists and is a directory, `cp` places the source inside that directory (e.g., copying `Receipts` into an existing `Backup of Receipts` yields `Backup of Receipts/Receipts`).
* If the destination path does not exist, `cp -r` will create a new directory at the destination name and copy the contents there.

## Moving and renaming

Use `mv` to move or rename files and directories:

```bash theme={null}
# mv [source] [destination]
$ mv Receipt.pdf Receipts/
```

This moves `Receipt.pdf` into the `Receipts` directory — the file is removed from the original location and appears only in the destination.

Rename a file:

```bash theme={null}
$ mv Receipt.pdf OldReceipt.pdf
```

Rename a directory:

```bash theme={null}
$ mv Receipts/ OldReceipts/
```

You do not need `-r` with `mv` to move directories; `mv` moves directories and their contents automatically.

## Deleting files and directories

Remove a single file with `rm`:

```bash theme={null}
$ rm Invoice.pdf
```

Remove a directory and all of its contents recursively:

```bash theme={null}
$ rm -r Invoices/
```

<Callout icon="warning" color="#FF6B6B">
  `rm -r` will permanently delete the directory and everything inside it. This action is typically irreversible — double-check paths before running recursive deletes.
</Callout>

## Quick reference table

| Command | Purpose | Example |
| - | - | - |
| `touch` | Create an empty file or update its timestamp | `touch Receipt.pdf` |
| `mkdir` | Create a directory | `mkdir Receipts` |
| `mkdir -p` | Create nested directories | `mkdir -p 2026/April/Receipts` |
| `cp` | Copy a file | `cp Receipt.pdf Receipts/` |
| `cp -r` | Copy a directory and its contents recursively | `cp -r Receipts "Backup of Receipts"` |
| `mv` | Move or rename files/directories | `mv Receipt.pdf Receipts/` |
| `rm` | Remove a file | `rm Invoice.pdf` |
| `rm -r` | Remove a directory and its contents recursively | `rm -r Invoices/` |

<Callout icon="lightbulb" color="#1CB2FE">
  Paths can be absolute (for example, `/home/jane/Receipt.pdf`) or relative (for example, `../jane/Receipt.pdf`). Using a trailing slash on a directory name (for example, `Receipts/`) is a helpful convention to indicate a directory.
</Callout>

## Examples: absolute vs relative paths

| Scenario | Absolute | Relative |
| - | - | - |
| Create a file in your home | `/home/jane/Receipt.pdf` | `~/Receipt.pdf` |
| Create a file in a sibling directory | `/home/jane/../otherdir/Note.txt` | `../otherdir/Note.txt` |

## Links and references

* [GNU coreutils — cp, mv, rm, mkdir, touch documentation](https://www.gnu.org/software/coreutils/manual/coreutils.html)
* [Linux filesystem hierarchy and paths](https://www.linuxfoundation.org/resources/open-source-guides/linux-filesystem-hierarchy)

This covers the fundamental file and directory commands you'll use daily. Practice with safe targets (temporary directories) until you’re comfortable, especially when using `-r` with `cp` or `rm`.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/crash-course-linux-for-absolute-beginners/module/ad9775dd-41c2-4d59-ae9a-6b18672ecbac/lesson/3f359f6e-e26f-4d59-92bc-a0bcf6bec2bf" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/crash-course-linux-for-absolute-beginners/module/ad9775dd-41c2-4d59-ae9a-6b18672ecbac/lesson/1bad5e96-b7b8-4389-bfd7-9f6bfe19954e" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.