Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the shell working directory in Linux?
To change the shell working directory, we use the cd command in the Linux system. The cd (change directory) command is one of the most fundamental navigation commands that allows users to move between different directories in the filesystem.
cd (change directory)
The cd command is used to change the current working directory in Linux/Unix operating systems. It is equivalent to the cd or chdir command in Windows. The command is also available in the EFI shell (Extensible Firmware Shell). By default, when you open a new terminal session, the current working directory is set to your home directory.
Syntax
The general syntax of the cd command is as follows −
cd [-L| [-P [-e]] [-@]] [directory]
Options
The following table describes the options available with the cd command −
| Option | Description |
|---|---|
| -L | Force symbolic links to be followed. Resolve symbolic links in the directory after processing instances of '..' |
| -P | Use the physical directory structure without following symbolic links |
| -e | If the -P option is used and the current working directory cannot be determined successfully, exit with a non-zero status |
| -@ | On systems that support it, present a file with extended attributes as a directory containing the file attributes |
| --help | Display help information and exit |
Common Usage Examples
Basic Directory Navigation
To change from the current directory to a specific directory, use −
$ cd Documents
This changes the working directory to the "Documents" folder in the current location.
Navigate to Home Directory
To change the current working directory to your home directory, use the tilde (~) symbol −
$ cd ~
Alternatively, you can use cd without any arguments −
$ cd
Navigate to Parent Directory
To move up one level to the parent directory, use two dots −
$ cd ..
Navigate Using Absolute Paths
To navigate to a directory using its absolute path −
$ cd /usr/local/bin
Navigate Using Relative Paths
To navigate to a subdirectory within the current directory −
$ cd shadow/snow
This navigates to the "snow" directory inside the "shadow" directory.
Checking Current Directory
To verify your current working directory, use the pwd (print working directory) command −
$ pwd
If you are in your home directory, the output will be similar to −
/home/username
Getting Help
To view detailed information about the cd command and its options −
$ cd --help
Key Points
The
cdcommand without arguments takes you to your home directoryUse
cd ..to move to the parent directoryBoth absolute paths (starting with /) and relative paths are supported
The
pwdcommand helps verify your current locationTab completion can help you navigate directories more efficiently
Conclusion
The cd command is essential for navigating the Linux filesystem. Understanding its basic usage patterns?moving to specific directories, returning home, and navigating parent directories?forms the foundation of effective command-line navigation in Linux systems.
