Skip to content

Unix / Linux - Directories

In Unix-like systems, files are organized within directories (often called folders in graphical environments). Directories themselves can contain other directories, forming a hierarchical structure known as the filesystem tree.

Understanding how to navigate and manage directories is fundamental to using the command line effectively.

The filesystem starts at the root directory, represented by a single forward slash (/). All other files and directories reside under this root.

When you log in, you are typically placed in your personal home directory. This is where you store your files and create your own subdirectory structure. It’s your personal space within the filesystem.

The tilde character (~) is a shortcut for your home directory path.

To navigate to your home directory from anywhere:

$ cd ~ # or simply
$ cd

To navigate to another user’s home directory (if you have permissions):

$ cd ~username

A file or directory’s location is specified by its pathname.

  • Absolute Pathname: Specifies the location starting from the root directory (/). It’s unambiguous, regardless of your current location. Always starts with /.
  • Relative Pathname: Specifies the location relative to your current working directory. Does not start with /.

Examples of absolute pathnames:

/home/alice/documents/report.txt
/etc/ssh/sshd_config
/usr/bin/python3

Examples of relative pathnames (assuming your current directory is /home/alice):

documents/report.txt # Refers to /home/alice/documents/report.txt
../bob/pictures # Refers to /home/bob/pictures (.. goes up one level)

The pwd (print working directory) command displays the absolute path of your current directory.

$ pwd
/home/alice/projects/website
$

The ls command lists the files and subdirectories within a directory. By default, it lists the contents of the current directory.

$ ls
index.html styles.css images scripts

To list the contents of a specific directory:

$ ls /etc
acpi cron.d hosts lvm profile
adobe cron.hourly hosts.allow machine-id protocols
alternatives cron.monthly hosts.deny magic pulse
...

Common useful options for ls include:

  • -l: Long format (shows permissions, owner, size, modification date).
  • -a: All files (includes hidden files starting with .).
  • -h: Human-readable sizes (e.g., 4.0K, 1.2M). Often used with -l.
  • -t: Sort by modification time, newest first.
  • -r: Reverse order while sorting.
$ ls -alh
total 20K
drwxr-xr-x 4 alice alice 4.0K Jul 25 14:30 .
drwxr-xr-x 5 alice alice 4.0K Jul 25 14:25 ..
-rw-r--r-- 1 alice alice 1.5K Jul 25 14:28 index.html
drwxr-xr-x 2 alice alice 4.0K Jul 25 14:26 images
drwxr-xr-x 2 alice alice 4.0K Jul 25 14:27 scripts
-rw-r--r-- 1 alice alice 850 Jul 25 14:30 styles.css

The mkdir (make directory) command creates new directories.

$ mkdir backups
$ mkdir documents reports presentations

The first command creates a directory named backups in the current location. The second creates three separate directories.

If you try to create a directory where the parent directory doesn’t exist, mkdir will fail.

$ mkdir /tmp/myproject/data
mkdir: cannot create directory ‘/tmp/myproject/data’: No such file or directory

Use the -p (parents) option to create any necessary parent directories automatically:

$ mkdir -p /tmp/myproject/data
$ # Success! Both myproject and data are created.

The rmdir (remove directory) command deletes empty directories.

$ rmdir old_backups
$ rmdir temp_dir project_archive

If a directory contains any files or subdirectories, rmdir will fail.

$ rmdir documents
rmdir: failed to remove 'documents': Directory not empty

To remove a directory and all its contents (files and subdirectories), use the rm command with the -r (recursive) option. Use rm -r with extreme caution! There is no undo.

$ rm -r documents # Deletes the documents directory and everything inside it!

Adding the -i (interactive) flag (rm -ri) can provide some safety by prompting before each deletion.

The cd (change directory) command navigates the filesystem tree. You provide the path (absolute or relative) of the directory you want to move into.

# Go to the backups directory (relative path)
$ cd backups
# Go to the system's configuration directory (absolute path)
$ cd /etc
# Go back to the previous directory
$ cd -
# Go up one level
$ cd ..
# Go up two levels
$ cd ../..

Tip: Most modern shells support Tab Completion. Type the beginning of a directory name and press the Tab key; the shell will attempt to complete the name or show possible matches. This saves typing and avoids errors.

The mv (move) command is used both to move directories (and files) to different locations and to rename them.

Syntax for renaming:

$ mv old_directory_name new_directory_name

Example: Rename temp_files to archive.

$ mv temp_files archive

Syntax for moving:

$ mv directory_to_move destination_directory/

Example: Move the archive directory into the backups directory.

$ mv archive backups/

Every directory contains two special entries:

  • . (dot): Represents the current directory itself.
  • .. (dot dot): Represents the parent directory (the directory one level up).

These are crucial for relative path navigation, especially .. for moving up the hierarchy.

# List contents of the current directory using .
$ ls .
# List contents of the parent directory using ..
$ ls ..
# Copy a file from the parent directory to the current directory
$ cp ../important_file.txt .