Unix / Linux - Directories

Welcome, aspiring programmers! Today, we're diving into the fascinating world of Unix and Linux directories. As your friendly neighborhood computer teacher, I'm here to guide you through this journey, step by step. So, grab a cup of coffee (or tea, if that's your thing), and let's explore the directory structure of these powerful operating systems!

Unix / Linux - Directories

Linux Directory Structure

Imagine your computer as a big tree. The root of this tree is, well, the root directory (/). From there, branches (subdirectories) spread out, creating a hierarchical structure. This structure is the backbone of Unix and Linux systems.

Here's a simplified view of the Linux directory structure:

/
├── bin
├── boot
├── dev
├── etc
├── home
├── lib
├── media
├── mnt
├── opt
├── proc
├── root
├── sbin
├── tmp
├── usr
└── var

Each of these directories has a specific purpose. For example, /bin contains essential user commands, /etc stores system configuration files, and /home is where user home directories reside.

Home Directory

Speaking of home, let's talk about your personal space in the Linux world - the home directory. It's like your bedroom in the big Linux house.

To navigate to your home directory, you can use the following command:

cd ~

Or simply:

cd

Here, cd stands for "change directory," and ~ is a shortcut for your home directory.

Absolute/Relative Pathnames

Now, let's discuss how to navigate this tree-like structure. We use pathnames, which come in two flavors: absolute and relative.

Absolute Pathnames

An absolute pathname starts from the root directory (/). It's like giving someone your full address, including the country, city, and street name.

Example:

cd /home/username/Documents

This command will take you to the Documents folder in your home directory, no matter where you currently are in the file system.

Relative Pathnames

A relative pathname, on the other hand, is based on your current location. It's like giving directions from where you are.

Example:

cd Documents

If you're already in your home directory, this will take you to the Documents folder.

Listing Directories

Now that we know how to navigate, let's see what's inside these directories. The ls command is your best friend here.

ls

This will list the contents of the current directory. Want more details? Try:

ls -l

The -l option gives you a long listing format, showing permissions, owner, size, and modification date.

Creating Directories

Time to build our own branches on this Linux tree! We use the mkdir command to create directories.

mkdir MyNewFolder

This creates a new directory named "MyNewFolder" in your current location.

Creating Parent Directories

What if you want to create a directory within a directory that doesn't exist yet? No worries! The -p option has got you covered.

mkdir -p Projects/WebDevelopment/HTML

This creates the entire path, even if the parent directories don't exist.

Removing Directories

Sometimes, we need to do some pruning. To remove an empty directory, use the rmdir command:

rmdir MyNewFolder

Be careful, though! If you want to remove a directory and all its contents, use rm -r, but use this with caution:

rm -r Projects

This will delete the Projects directory and everything inside it. It's like using a chainsaw instead of pruning shears!

Changing Directories

We've already seen the cd command, but let's recap:

cd /path/to/directory    # Change to a specific directory
cd ..                    # Move up one level
cd ~                     # Go to home directory
cd -                     # Go to the previous directory

Renaming Directories

In Unix/Linux, we don't have a separate "rename" command. Instead, we use the mv (move) command:

mv oldname newname

For example:

mv MyProjects MyAwesomeProjects

This renames the "MyProjects" directory to "MyAwesomeProjects".

The directories . (dot) and .. (dot dot)

Let's finish with two special directory names:

  • . (single dot) represents the current directory
  • .. (double dot) represents the parent directory

These are incredibly useful in relative pathnames. For example:

cp ../file.txt .

This copies a file named "file.txt" from the parent directory to the current directory.

Here's a table summarizing the main directory-related commands we've covered:

Command Description Example
cd Change directory cd /home/user
ls List directory contents ls -l
mkdir Create a new directory mkdir NewFolder
rmdir Remove an empty directory rmdir OldFolder
rm -r Remove a directory and its contents rm -r Projects
mv Move/rename a directory mv OldName NewName
cp Copy files or directories cp -r SourceDir DestDir

Remember, practice makes perfect! Don't be afraid to experiment in your terminal. Linux is very forgiving (except when you use rm -r carelessly!). Happy exploring, and may the force of Linux be with you!

Credits: Image by storyset