# Filesystem Commands | Cycle.io

Canonical URL: https://cycle.io/learn/linux-filesystem-commands

## Filesystem Commands.

[The Linux filesystem](https://cycle.io/learn/linux-file-system) is a foundational aspect of the operating system, providing the structure for data storage and organization. Understanding how to navigate and manage the filesystem using command-line tools is essential for system administrators, developers, and anyone working with Linux environments. This guide covers some of the most important filesystem commands that help you interact with files, directories, and storage devices.

### Navigating the Filesystem

#### `pwd` - Print Working Directory

The `pwd` command prints the full path of the current working directory. It's a simple but essential command for confirming your location within the filesystem.

```bash
pwd
```

This command is particularly useful when navigating deeply nested directories, helping you keep track of where you are in the directory structure.

#### `ls` - List Directory Contents

The `ls` command lists the contents of a directory, providing information about files and subdirectories.

```bash
ls
```

-   **Options**:

-   `-l`: Displays a detailed list with file permissions, ownership, size, and modification date.
-   `-a`: Includes hidden files (those starting with a dot `.`).
-   `-h`: Shows file sizes in human-readable format (e.g., KB, MB).

These options are often combined (e.g., `ls -lah`) to get a comprehensive view of directory contents.

#### `cd` - Change Directory

The `cd` command changes the current working directory, allowing you to move around the filesystem.

```bash
cd /path/to/directory
```

-   **Shortcuts**:

-   `cd ~`: Moves to your home directory.
-   `cd ..`: Moves up one directory level.
-   `cd -`: Switches to the previous directory.

These shortcuts are handy for quickly navigating to commonly used locations.

### Managing Files and Directories

#### `mkdir` - Make Directories

The `mkdir` command creates one or more directories.

```bash
mkdir directory_name
```

-   **Options**:

-   `-p`: Creates parent directories as needed (e.g., `mkdir -p /parent/child/grandchild`).

This is especially useful when you need to create nested directories in a single command.

#### `rmdir` - Remove Empty Directories

The `rmdir` command removes empty directories.

```bash
rmdir directory_name
```

If the directory contains files or other directories, use `rm -r` instead.

#### `rm` - Remove Files or Directories

The `rm` command removes files or directories. This command is powerful and should be used with caution.

```bash
rm file_name
```

-   **Options**:

-   `-r`: Recursively removes directories and their contents.
-   `-i`: Prompts before each removal, providing an additional layer of safety.

**Caution**: The `rm` command is irreversible. Double-check your command before execution, especially when using the `-r` option.

#### `cp` - Copy Files and Directories

The `cp` command copies files or directories from one location to another.

```bash
cp source_file destination_file
```

-   **Options**:

-   `-r`: Recursively copies entire directories.
-   `-i`: Prompts before overwriting existing files.

This command is useful for duplicating files or creating backups.

#### `mv` - Move or Rename Files and Directories

The `mv` command moves or renames files and directories.

```bash
mv old_name new_name
```

-   **Use cases**:

-   Renaming a file: `mv old_name new_name`
-   Moving a file to a different directory: `mv file_name /path/to/directory`

This command is commonly used for organizing files or restructuring directory layouts.

#### `ln` - Create Links

The `ln` command creates links between files, either as hard links (which point directly to the file data) or symbolic (soft) links (which point to another file).

```bash
ln source_file link_name
ln -s source_file symbolic_link_name
```

-   **Hard Link**: `ln source_file link_name` creates a direct reference to the original file.
-   **Symbolic Link**: `ln -s source_file symbolic_link_name` creates a pointer to the original file, allowing for more flexible file management.

### Viewing and Manipulating File Content

#### `cat` - Concatenate and Display File Content

The `cat` command displays the content of a file.

```bash
cat file_name
```

-   **Options**:

-   `-n`: Numbers the output lines, which is helpful for viewing line numbers in a file.

#### `less` and `more` - View File Content

`less` and `more` allow you to view large files one page at a time.

```bash
less file_name
more file_name
```

-   **Navigation**:

-   `Space`: Move to the next page.
-   `q`: Quit the viewer.

These commands are essential when dealing with large log files or lengthy documents.

#### `head` and `tail` - View Beginning or End of Files

`head` shows the first few lines of a file, while `tail` shows the last few lines.

```bash
head file_name
tail file_name
```

-   **Options**:

-   `-n X`: Displays the first/last `X` lines (e.g., `head -n 10 file_name`).

`tail` is particularly useful with the `-f` option to monitor files in real-time (e.g., `tail -f logfile`).

#### `touch` - Update File Timestamps or Create New Files

The `touch` command changes the access and modification times of a file. If the file doesn't exist, `touch` creates it.

```bash
touch file_name
```

This command is commonly used to create empty files or update timestamps.

### Filesystem Information and Management

#### `df` - Disk Space Usage

The `df` command displays disk space usage for the filesystem.

```bash
df -h
```

-   **Options**:

-   `-h`: Displays sizes in a human-readable format (e.g., GB, MB).

This command is critical for monitoring available disk space, especially on servers or systems with limited storage.

#### `du` - Estimate File or Directory Space Usage

The `du` command shows disk usage of files and directories.

```bash
du -h directory_name
```

-   **Options**:

-   `-h`: Displays sizes in a human-readable format.
-   `-s`: Summarizes the total space used by a directory.

`du` is helpful for identifying which directories are consuming the most space.

#### `mount` - Mount Filesystems

The `mount` command attaches a filesystem to the directory tree.

```bash
mount /dev/sda1 /mnt
```

-   **Unmounting**: To detach the filesystem, use `umount`:
    
    ```bash
    umount /mnt
    ```
    

Mounting is essential for accessing different filesystems, such as external drives or network shares.

#### `fsck` - Filesystem Check

The `fsck` command checks and repairs filesystem inconsistencies.

```bash
fsck /dev/sda1
```

This command is crucial for maintaining filesystem integrity, especially after improper shutdowns or power failures.

#### `mkfs` - Make Filesystems

The `mkfs` command formats a partition with a specified filesystem.

```bash
mkfs.ext4 /dev/sda1
```

-   **Common Filesystems**:

-   `ext4`: The most common Linux filesystem.
-   `xfs`: A high-performance filesystem, often used in enterprise environments.

Formatting partitions is a fundamental task when setting up new storage devices.

#### `find` - Search for Files

The `find` command searches for files in a directory hierarchy.

```bash
find /path/to/search -name "file_name"
```

-   **Options**:

-   `-name`: Search by file name.
-   `-type f`: Search for files only.
-   `-type d`: Search for directories only.

`find` is incredibly powerful for locating files based on various criteria, such as name, type, or modification time.

#### `locate` - Quickly Find Files

The `locate` command searches a pre-built database for files, making it much faster than `find` but slightly less up-to-date.

```bash
locate file_name
```

Run `updatedb` to refresh the database, ensuring `locate` returns the most accurate results.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
