Unix / Linux - Directories
Unix / Linux - Directory Management
Section titled “Unix / Linux - Directory Management”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.
The Home Directory
Section titled “The Home Directory”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$ cdTo navigate to another user’s home directory (if you have permissions):
$ cd ~usernameAbsolute vs. Relative Pathnames
Section titled “Absolute vs. Relative Pathnames”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/python3Examples 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)Finding Your Current Directory: pwd
Section titled “Finding Your Current Directory: pwd”The pwd (print working directory) command displays the absolute path of your current directory.
$ pwd/home/alice/projects/website$Listing Directory Contents: ls
Section titled “Listing Directory Contents: ls”The ls command lists the files and subdirectories within a directory. By default, it lists the contents of the current directory.
$ lsindex.html styles.css images scriptsTo list the contents of a specific directory:
$ ls /etcacpi cron.d hosts lvm profileadobe cron.hourly hosts.allow machine-id protocolsalternatives 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 -alhtotal 20Kdrwxr-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.htmldrwxr-xr-x 2 alice alice 4.0K Jul 25 14:26 imagesdrwxr-xr-x 2 alice alice 4.0K Jul 25 14:27 scripts-rw-r--r-- 1 alice alice 850 Jul 25 14:30 styles.cssCreating Directories: mkdir
Section titled “Creating Directories: mkdir”The mkdir (make directory) command creates new directories.
$ mkdir backups$ mkdir documents reports presentationsThe first command creates a directory named backups in the current location. The second creates three separate directories.
Creating Parent Directories (-p)
Section titled “Creating Parent Directories (-p)”If you try to create a directory where the parent directory doesn’t exist, mkdir will fail.
$ mkdir /tmp/myproject/datamkdir: cannot create directory ‘/tmp/myproject/data’: No such file or directoryUse the -p (parents) option to create any necessary parent directories automatically:
$ mkdir -p /tmp/myproject/data$ # Success! Both myproject and data are created.Removing Directories: rmdir and rm -r
Section titled “Removing Directories: rmdir and rm -r”The rmdir (remove directory) command deletes empty directories.
$ rmdir old_backups$ rmdir temp_dir project_archiveIf a directory contains any files or subdirectories, rmdir will fail.
$ rmdir documentsrmdir: failed to remove 'documents': Directory not emptyTo 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.
Changing Directories: cd
Section titled “Changing Directories: cd”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.
Renaming or Moving Directories: mv
Section titled “Renaming or Moving Directories: mv”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_nameExample: Rename temp_files to archive.
$ mv temp_files archiveSyntax for moving:
$ mv directory_to_move destination_directory/Example: Move the archive directory into the backups directory.
$ mv archive backups/Special Directories: . and ..
Section titled “Special Directories: . and ..”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 .