How to Change the Directory in Linux
Learn how to navigate directories, create new directories, rename and delete directories, and change directory permissions in Linux. This comprehensive guide provides step-by-step instructions and practical examples for using the “cd”, “mkdir”, “mv”, “rmdir”, “rm”, and “chmod” commands to manage your directory structure. Whether you’re a beginner or an experienced user, you’ll find valuable tips and tricks to help you master the art of directory navigation and management in Linux.
Changing directories in Linux is a common task that users perform frequently. In Linux, directories organize files and provide a hierarchical structure to the file system. The command line interface is a powerful tool that allows users to navigate the file system efficiently. This article will cover the different ways to change directories in Linux using various commands and techniques.
Table of Content
- Introduction to Directories in Linux
- Changing Directories using the cd Command
- Ways to use cd Command
- Tab Completion
- Using Wildcards to Change the Directory
- Using the pushd and popd Commands to Change the Directory
Introduction to Directories in Linux
Directories in Linux are similar to folders in Windows or Mac OS. The directory organizes files and provides a hierarchical structure to the file system. Each directory can contain subdirectories and files. Directories are represented as a path in Linux, starting from the root directory (/) and going down to the desired directory. For example, the path to the Documents directory in a user’s home directory would be /home/user/Documents.
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
Changing Directories Using the cd Command
Changing directories using the cd (change directory) command is one of the most common and fundamental tasks in the Linux command line. The cd command allows you to move between directories in the file system and is used to change the shell’s working directory.
The syntax of the cd command is as follows:
$cd [options] [path]
In the above command:
- cd: stands for “change directory”
- options: refer to additional settings or parameters that can be used with the “cd” command to modify its behavior.
- path: refers to the path or name of the directory that you want to change to using the “cd” command.
To move to Downloads we can use the command:
$cd Downloads
The cd command in Linux provides several options to modify its behaviour. Here are some of the most commonly used options:
- -L: This option is enabled by default and instructs cd to follow symbolic links. If you want to explicitly force cd to follow symbolic links, use the -L option.
- -P: This option instructs cd not to follow symbolic links. Instead, it resolves the symbolic link to determine the parent directory before moving to the user-requested directory.
- -e: When using the -P option, this option causes cd to exit with a non-zero status if it cannot resolve the symbolic link.
- -@: This option presents a file with extended attributes as a directory containing the file attributes.
By using these options, you can customize the behaviour of the cd command to suit your needs when navigating through the file system in Linux.
Ways to Use cd Command
1. Changing Directory
To change to a specific directory, simply type cd followed by the name of the directory.
For example, to change to the directory “Sample”, type:
$cd Sample
2. Change to the previous directory
To change to a directory using an absolute path, we can use the cd command followed by the full path to the target directory. For example, if we want to change to the Downloads directory, which is located at /home/user/Downloads, we can use the following command
$cd -
3. Change to the Parent Directory
You can use two-period symbols (..) with the cd command to change to the parent of the current directory. This is a quick and convenient way to navigate up the directory tree. For example, here we are in the directory “Sample/project”, we can use the following command to change to the “Sample” directory:
$cd ..
You can use multiple “..” to go up more than one directory level.
For example:
$cd ../../
4. Changing Directory Using an Absolute Path
To change to a directory using an absolute path, we can use the cd command followed by the full path to the target directory. For example, if we want to change to the Downloads directory, which is located at /home/user/Downloads, we can use the following command:
$cd /home/<username>/Downloads
5. Changing Directory Using a Relative Path
A relative path is a path that is relative to the current directory, rather than an absolute path that specifies the complete path from the root directory.
For example, here we are current working directory is /home/user/Sample, and we want to change to the project directory, which is located in the same directory as Sample, we can use a relative path.
$cd project/
Relative paths are useful when you want to navigate to a directory that is located relative to the current directory, without specifying the full path. This is especially useful when you are working with deeply nested directories, and you don’t want to type out the full path every time you want to change directories.
6. Changing Back to the Home Directory
$cd
In Linux, the home directory is the default directory for each user. This directory is designated by the HOME environment variable and is usually located at /home/username. To change back to the home directory, you can use the cd command, which is a shorthand for the home directory.
7. Changing to the Root Directory
In Linux, the root directory is the top-level directory of the file system and is designated by the forward-slash (/) symbol. To change to the root directory, you can use the cd command followed by a single forward slash:
$cd /
Tab Completion
Tab Completion is a feature in Linux that allows you to quickly complete file and directory names by pressing the Tab key. When you start typing a file or directory name, the shell will attempt to complete the name for you based on the contents of the current directory. If there is only one possible completion, the shell will automatically complete the name for you. If there are multiple possible completions, you can press the Tab key again to see a list of all possible completions.
For example, here we have changed to the Sample directory. Instead of typing the full directory name, you can type the first few letters (Sa) and then press the Tab key. The shell will automatically complete the name for you if there is only one possible completion.
$cd Sa[Tab]
Using Wildcards
You can use wildcards to change the directory to a directory with a similar name to the one you want to access.
For example, let’s say we have two directories in our working directory, my_app, and my_website, and we want to change to the my_app directory. Instead of typing out the full name, we can use a wildcard to shorten the command:
$cd my_a*
Using wildcards to change directory can save you time when navigating through directories with similar names. However, it’s important to use the wildcard pattern carefully to avoid matching unintended directories. Always double-check the results of your command before executing it.
Using the pushd and popd Commands
The pushd and popd commands are used to create and manage a list of directories, also known as a directory stack. This feature allows you to easily switch between directories and return to previous directories without having to remember their paths.
Use the pushd command followed by the directory path to add a directory to the stack and change to that directory:
You can use the command to change the directory using pushd:
$pushd <path>
This command will add the specified directory to the top of the stack and change it to that directory.
Use the popd command to remove the top directory from the stack and change to the previous directory:
You can use the command to change the directory using popd:
$popd
This command will remove the top directory from the stack and change to the previous directory in the stack.
Use the dirs command to view the list of directories in the stack:
$dirs
This command will display a list of directories in the stack, with the top directory listed first.
To clear the stack you can use:
$dirs -c
You can also use the pushd command without a directory path to switch between the top two directories in the stack:
$pushd
This command will swap the top two directories in the stack and change to the new top directory.
Conclusion
In conclusion, changing directories is an essential task in Linux, and the cd command is the primary way to navigate between directories. Understanding how to use relative and absolute paths, tab completion, and wildcards can help you work more efficiently. Advanced techniques like using the pushd and popd commands to manage a directory stack and switching to another user’s home directory can also be useful. By mastering these techniques, you can work more effectively in the terminal and move between different projects and directories with ease.
Contributed By: Anish
FAQs
What is a directory in Linux, and how is it structured?
In Linux, a directory is a folder that contains files and other directories. Directories are organized in a hierarchical structure, with the root directory ("/") at the top and subdirectories below.
How do I view the contents of the current directory in Linux?
To view the contents of the current directory in Linux, you can use the "ls" command.
How do I change the current directory in Linux?
To change the current directory in Linux, you can use the "cd" command, followed by the name of the directory you want to navigate to.
How do I navigate to a directory that is not in the current directory in Linux?
To navigate to a directory that is not in the current directory in Linux, you can use the "cd" command, followed by the path to the directory.
How do I navigate to the parent directory in Linux?
To navigate to the parent directory in Linux, you can use the "cd" command, followed by two dots "..".
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio