Linux · Files and Directories

Linux · Lesson

Files and Directories

Linux filesystem structure and navigation. Permissions, ownership and ACL concepts. Copy, move and delete operations. Important system directories explained.

File and directory operations

Linux organizes data in files and directories. Understanding how to create and remove them is essential for system navigation and administration.

Directories

Used to organize files in a hierarchical structure using mkdir and rmdir.

Files

Created using touch and managed using rm and other utilities.

Deletion safety

Linux provides powerful deletion tools like rm -rf which must be used carefully.

Step-by-step file and directory commands

1. Create a new directory

Use mkdir to create a single directory in Linux.

terminal — bash
linux-lab
[root@localhost ~]# mkdir dir-1
[root@localhost ~]# ls -l
drwxr-xr-x. 2 root root 6 Dec 19 16:25 dir-1
[root@localhost ~]#

2. Create multiple directories

You can create more than one directory in a single command.

terminal — bash
linux-lab
[root@localhost ~]# mkdir dir2 dir3 dir4
[root@localhost ~]# ls
dir2 dir3 dir4
[root@localhost ~]#

3. Create nested directories

The -p option creates parent directories if they do not exist.

terminal — bash
linux-lab
[root@localhost ~]# mkdir -p outer/inner
[root@localhost ~]# ls outer
inner
[root@localhost ~]#

4. Create a new file

The touch command creates an empty file.

terminal — bash
linux-lab
[root@localhost ~]# touch file
[root@localhost ~]# ls -l
-rw-r--r--. 1 root root 0 Dec 19 16:33 file
[root@localhost ~]#

5. Create multiple files

Multiple files can be created at once using touch.

terminal — bash
linux-lab
[root@localhost ~]# touch file-1 file-2 file-3
[root@localhost ~]# ls
file file-1 file-2 file-3
[root@localhost ~]#

6. Remove empty directory

rmdir deletes only empty directories.

terminal — bash
linux-lab
[root@localhost ~]# rmdir dir-1
[root@localhost ~]# ls
[root@localhost ~]#

7. Remove files

rm is used to delete files in Linux.

terminal — bash
linux-lab
[root@station1 ~]# rm file-1 file-2 file-3
[root@station1 ~]# ls
[root@station1 ~]#

8. Remove files and directories (options)

rm supports multiple options for safe and force deletion.

terminal — bash
linux-lab
-i → interactive (ask before delete)
-r → recursive (delete directories)
-v → verbose (show output)
-f → force (no prompts)

9. Forcefully remove files/directories

rm -rf deletes files and directories without confirmation.

terminal — bash
linux-lab
[root@station1 ~]# rm -rf dir-1 file-1
[root@station1 ~]# ls
[root@station1 ~]#

Best practices

Safe usage

  • Always verify files before using rm -rf.
  • Prefer interactive mode (-i) in production systems.
  • Avoid recursive deletion unless necessary.

Good habits

  • Organize files using meaningful directory structures.
  • Use touch for file creation instead of editors when testing.
  • Double-check directory paths before deletion.

Practice tasks

  • Create a directory structure using mkdir and mkdir -p.
  • Create multiple files using touch.
  • Delete empty directories using rmdir.
  • Practice rm and rm -rf carefully in a test environment.

Next, you will learn how to navigate and inspect files in Linux.