> ## 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.

# Managing Files With Command line Tools

> Guide to managing files from the command line, covering navigation, creating copying moving renaming deleting and viewing files with essential commands examples and a hands on demo

In this lesson you'll learn how to manage files from the command line — a fundamental skill for Red Hat system administrators. Working efficiently at the terminal for creating, copying, moving, renaming, and deleting files reduces errors and speeds up daily tasks. This guide covers the essential commands, a quick command summary table, and a hands-on demo transcript so you can follow along.

## Basic navigation

* `pwd` — print the current working directory.
* `cd` — change directory (e.g., `cd /var/log` or `cd ..` to go up one level).
* `ls` — list directory contents.

Common `ls` options:

* `-a` — include hidden files (names starting with a dot).
* `-l` — long listing format (shows permissions, ownership, size, timestamp).
* `-h` — human-readable sizes (e.g., `-lh`).

Tip: Combine options, e.g., `ls -alh` to see detailed listings including hidden files with readable sizes.

## Copying, moving, and deleting

* `cp` — copy files and directories. Use `-r` (or `-a`) to copy directories recursively.
* `mv` — move or rename files and directories.
* `rm` — remove files. Use `-r` to remove directories and their contents recursively; add `-f` to force.
* `rmdir` — remove empty directories only.

<Callout icon="warning" color="#FF6B6B">
  Be careful with `rm -r` (or `rm -rf`). It permanently deletes directories and their contents if used without caution.
</Callout>

<Callout icon="lightbulb" color="#1CB2FE">
  Safer deletion tips:

  * Use `rm -i` to prompt before each removal.
  * Run `ls -la` first to verify the files you plan to delete.
  * Consider moving important data to a temporary backup directory before deletion, e.g., `mv important/ ~/trash/`.
</Callout>

## Creating and updating files and directories

* `mkdir` — create a directory. Use `-p` to create parent directories as needed (e.g., `mkdir -p projects/data/reports`).
* `rmdir` — remove an empty directory.
* `touch` — update access and modification times; if the file does not exist, `touch` creates an empty file.

Example:

```bash theme={null}
mkdir -p projects/data/reports
touch projects/file1.txt
```

## Viewing file contents

* `cat` — concatenate and display files (prints entire contents).
* `head` — display the first 10 lines of a file by default; use `-n` to change the number of lines displayed (e.g., `head -n 5 file.txt`).
* `tail` — display the last 10 lines by default; use `-n` to change the number of lines (e.g., `tail -n 50 /var/log/messages`).
* `more` / `less` — view file contents one page at a time (`less` is generally preferred for navigation).

Use these to inspect logs and configuration files without opening a graphical editor.

## Commands at a glance

| Command | Description |
| -: | - |
| `pwd` | Display the current directory |
| `cd` | Change directory |
| `ls` | Display directory contents |
| `ls -a` | Include hidden files |
| `ls -l` | Long listing (permissions, owner, size, timestamp) |
| `cp` | Copy a file or directory (use `-r` to copy directories recursively) |
| `mv` | Move or rename a file or directory |
| `rm` | Remove a file (use `-r` to remove directories recursively) |
| `mkdir` | Create a directory (use `-p` to create parent directories) |
| `rmdir` | Remove an empty directory |
| `touch` | Update access/modification times; create file if it doesn't exist |
| `cat` | Concatenate and display files |
| `head` | Display first lines of a file (use `-n` to set lines) |
| `tail` | Display last lines of a file (use `-n` to set lines) |
| `more` / `less` | View file contents one page at a time |

## Hands-on demo

Below is a step-by-step demo transcript showing common file-management workflows: creating directories, creating files, copying/moving files, and removing files and directories safely.

```bash theme={null}
student@control:~$ pwd
/home/student
student@control:~$ mkdir projects
student@control:~$ cd projects/
student@control:~/projects$ mkdir -p data/reports
student@control:~/projects$ tree
.
└── data
    └── reports

3 directories, 0 files
student@control:~/projects$ touch file1.txt file2.txt file3.txt
student@control:~/projects$ ls
data  file1.txt  file2.txt  file3.txt
student@control:~/projects$ ls -l
total 0
drwxr-xr-x. 3 student student 21 Apr 15 15:54 data
-rw-r--r--. 1 student student 0 Apr 15 15:54 file1.txt
-rw-r--r--. 1 student student 0 Apr 15 15:54 file2.txt
-rw-r--r--. 1 student student 0 Apr 15 15:54 file3.txt
student@control:~/projects$
```

* Note: the leading `d` in `drwxr-xr-x` indicates a directory.

Continue the demo:

```bash theme={null}
student@control:~/projects$ clear
student@control:~/projects$ ls -a
.  ..  data  file1.txt  file2.txt  file3.txt
student@control:~/projects$ echo test > file1.txt
student@control:~/projects$ echo test2 > file2.txt
student@control:~/projects$ cat file1.txt
test
student@control:~/projects$ head file1.txt
test
student@control:~/projects$ tail file1.txt
test
```

* `echo ... > filename` writes a line to a file (overwrites if the file exists).
* `cat` prints the entire file; `head` prints the first lines; `tail` prints the last lines.

Copy a file into the `data` directory:

```bash theme={null}
student@control:~/projects$ cp file1.txt data/copiedfile.txt
student@control:~/projects$ cat data/copiedfile.txt
test
```

Copy a directory recursively (including its files and subdirectories):

```bash theme={null}
student@control:~/projects$ cp -r data/ data_backup
student@control:~/projects$ ls data_backup/
copiedfile.txt  reports
```

Rename (move) a file:

```bash theme={null}
student@control:~/projects$ mv file2.txt movedfile.txt
student@control:~/projects$ ls
data  data_backup  file1.txt  file3.txt  movedfile.txt
```

Remove a file:

```bash theme={null}
student@control:~/projects$ rm movedfile.txt
```

Attempt to remove a non-empty directory with `rmdir`:

```bash theme={null}
student@control:~/projects$ rmdir data
rmdir: failed to remove 'data': Directory not empty
```

`rmdir` fails because `data` contains files and/or subdirectories. Remove a non-empty directory with `rm -r`:

```bash theme={null}
student@control:~/projects$ rm -r data
student@control:~/projects$ clear
```

This completes the demo: navigating, creating, managing, and inspecting files. These simple commands form the core file-management workflow you'll use constantly as an administrator.

## Links and references

* [Red Hat Enterprise Linux Documentation](https://access.redhat.com/documentation/en-us/)
* [Linux coreutils — cp, mv, rm, ls, mkdir, touch](https://www.gnu.org/software/coreutils/manual/)
* [The Linux Command Line (book)](http://linuxcommand.org/)

Use these references to dive deeper into options, advanced usage, and best practices for safe file management.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bec5b574-0ba0-45f8-9ddf-03f7cc4f0dd6/lesson/fc420eb1-cc1e-4f31-818f-96c91496b23a" />
</CardGroup>


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