How to switch to a different directory in the Linux terminal

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial will guide you through understanding the structure of the Linux file system, navigating between directories using the cd command, and exploring and managing files and directories. By the end of this tutorial, you'll have a solid grasp of how to effectively interact with the Linux file system and perform common file management tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/FileandDirectoryManagementGroup -.-> linux/locate("`File Locating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/cd -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/pwd -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/mkdir -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/find -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/locate -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/ls -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} linux/wildcard -.-> lab-417916{{"`How to switch to a different directory in the Linux terminal`"}} end

Understanding the Linux File System Structure

The Linux file system is the way in which files and directories are organized and stored on a Linux operating system. Understanding the structure of the Linux file system is crucial for navigating and managing files and directories effectively.

At the root of the Linux file system is the "/" (root) directory, which serves as the top-level directory. Underneath the root directory, there are several important directories that serve specific purposes:

graph TD root[/] bin[/bin] etc[/etc] home[/home] lib[/lib] opt[/opt] proc[/proc] root[/root] sbin[/sbin] tmp[/tmp] usr[/usr] var[/var] root --> bin root --> etc root --> home root --> lib root --> opt root --> proc root --> root root --> sbin root --> tmp root --> usr root --> var

The most commonly used directories in the Linux file system include:

Directory Purpose
/bin Contains essential user binary (executable) files.
/etc Contains system configuration files.
/home Contains user home directories.
/var Contains variable data files, such as logs and spool files.
/tmp Contains temporary files that are deleted on system reboot.

To interact with the Linux file system, you can use the cd command to navigate between directories. For example, to change to the /home directory, you would use the following command:

cd /home

By understanding the structure of the Linux file system and the purpose of its key directories, you can effectively manage and interact with files and directories on your Linux system.

One of the most fundamental commands for navigating the Linux file system is the cd (change directory) command. This command allows you to move between directories and explore the file system hierarchy.

To change to a directory, you can use the cd command followed by the path of the directory you want to access. For example, to change to the /home directory, you would use the following command:

cd /home

The path provided to the cd command can be either an absolute path or a relative path. An absolute path is a complete path starting from the root directory (/), while a relative path is a path relative to your current working directory.

Here are some examples of using the cd command with both absolute and relative paths:

Absolute path:

cd /etc/apache2

Relative path:

cd ../usr/bin

In the relative path example, ../ means "go up one directory", so the command will change to the /usr/bin directory relative to the current working directory.

You can also use the pwd (print working directory) command to display your current working directory. This can be helpful when navigating the file system to understand your current location.

pwd
/home/username

By mastering the cd command and understanding the concepts of absolute and relative paths, you can efficiently navigate the Linux file system and access the files and directories you need.

Exploring and Managing Files and Directories

Once you have a basic understanding of the Linux file system structure and how to navigate it using the cd command, the next step is to learn how to explore and manage files and directories.

The ls (list) command is one of the most commonly used commands for viewing the contents of a directory. You can use it to list the files and subdirectories within the current working directory or a specified directory.

ls
file1.txt  file2.txt  directory1/

To create a new directory, you can use the mkdir (make directory) command followed by the name of the new directory.

mkdir new_directory

To create a new file, you can use the touch command followed by the name of the file you want to create.

touch new_file.txt

To remove a file, you can use the rm (remove) command followed by the name of the file.

rm file1.txt

To copy a file, you can use the cp (copy) command followed by the source file and the destination file.

cp file1.txt file_copy.txt

To move or rename a file, you can use the mv (move) command followed by the source file and the destination file or new name.

mv file1.txt renamed_file.txt

By mastering these basic file and directory management commands, you can effectively navigate, explore, and manipulate the contents of the Linux file system to suit your needs.

Summary

In this tutorial, you learned about the structure of the Linux file system, including the root directory and key subdirectories. You also discovered how to use the cd command to navigate between directories and explored techniques for managing files and directories. With this knowledge, you can now confidently interact with the Linux file system and perform essential file management tasks.

Other Linux Tutorials you may like