Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Navigating Directories

Introduction

In this tutorial, we'll explore how to navigate directories using the command line. This is an essential skill for anyone working with a terminal or command prompt, allowing you to move through the file system efficiently.

Current Working Directory

The first step in navigating directories is understanding your current location in the file system. You can use the pwd command, which stands for "print working directory".

Example:

pwd
/home/user

Listing Directory Contents

To see the contents of a directory, use the ls command. This will list all files and subdirectories in your current directory.

Example:

ls
Documents Downloads Music Pictures Videos

Changing Directories

To move to a different directory, use the cd command followed by the directory's path. For example, to move into the "Documents" directory:

Example:

cd Documents

To move up one level in the directory hierarchy, use cd ..:

Example:

cd ..

Absolute vs Relative Paths

Paths can be either absolute or relative. An absolute path starts from the root directory (e.g., /home/user/Documents), while a relative path is relative to the current directory (e.g., Documents).

Example of using an absolute path:

cd /home/user/Documents

Example of using a relative path:

cd Documents

Creating and Removing Directories

You can create a new directory using the mkdir command followed by the name of the new directory:

Example:

mkdir new_directory

To remove an empty directory, use the rmdir command:

Example:

rmdir new_directory

Conclusion

By mastering these basic commands for navigating directories, you'll be able to efficiently move through the file system, manage directories, and understand your current location within the terminal. Practice these commands to become more comfortable with command-line navigation.