Renaming a directory in Linux uses the same `mv` command you would use for files. But if you are new to Linux, understanding how to change directory in linux is the very first skill you need to master. Moving around the file system is essential for running commands, editing files, and managing your projects.
Think of the Linux terminal as a map. You start at a home location, and every command you run happens from that spot. To work in a different folder, you must navigate there first. This guide will show you exactly how to do that, step by step.
How To Change Directory In Linux
The main tool for moving between folders is the `cd` command. It stands for “change directory.” You type `cd` followed by the path to the folder you want to enter. It is simple, fast, and works on every Linux distribution.
For example, if you are in your home folder and want to go into a folder named “Documents,” you type:
cd Documents
Press Enter, and you are now inside the Documents folder. Your terminal prompt usually updates to show your new location. This is the core of how to change directory in linux.
Understanding Your Current Directory
Before you move, you need to know where you are. The command pwd prints your current working directory. Type it anytime you feel lost.
pwdshows the full path, like/home/username/Documents.- Your prompt often shows the folder name, but not always the full path.
- Use
pwdto confirm your location before running other commands.
Knowing your current spot prevents mistakes. If you try to move to a folder that does not exist, you will see an error message like “No such file or directory.”
Absolute Paths Vs. Relative Paths
When you use cd, you can specify the path in two ways. Absolute paths start from the root directory, which is the top of the file system. They always begin with a forward slash /.
For instance, cd /home/username/Documents is an absolute path. It works no matter where you are currently located.
Relative paths start from your current directory. They do not begin with a slash. If you are in /home/username and type cd Documents, that is a relative path. It only works if the “Documents” folder exists inside your current location.
Both methods are valid. Use absolute paths when you want to be sure of the destination. Use relative paths for quick navigation within nearby folders.
Going Up One Directory Level
To move to the parent folder, use two dots: cd ... This is one of the most common commands in Linux.
Example: If you are in /home/username/Documents/Work, typing cd .. takes you to /home/username/Documents. Type cd .. again to reach /home/username.
You can also chain multiple levels. Use cd ../.. to go up two directories at once. This saves time when you need to move up several levels.
Going To Your Home Directory
Your home directory is your personal space. It is usually /home/yourusername. To go there from anywhere, just type cd with no arguments.
cd
That is it. Press Enter, and you are back home. You can also use cd ~ because the tilde ~ is a shortcut for your home directory.
This is handy when you are deep in a folder structure and want to return to your starting point quickly.
Going To The Root Directory
The root directory is the top of the entire file system. It is represented by a single forward slash /. To go there, type:
cd /
From here, you can see all system folders like /etc, /var, /usr, and /home. Be careful when working in the root directory because changes here affect the whole system.
Using Tab Completion
Typing long folder names is tedious and error-prone. Linux has a feature called tab completion. Start typing a folder name, then press the Tab key.
If the name is unique, the terminal fills it in for you. If there are multiple matches, press Tab twice to see a list. This speeds up navigation and reduces typos.
For example, type cd Doc and press Tab. The terminal completes it to cd Documents/ if that folder exists.
Navigating To Folders With Spaces
Folder names with spaces require special handling. If you type cd My Documents, Linux sees two separate arguments and gives an error.
To fix this, enclose the path in quotes:
cd "My Documents"
Or use a backslash before the space:
cd My\ Documents
Both methods work. Quotes are often easier to read. Always check for spaces when you copy folder names from other sources.
Using The Previous Directory
Sometimes you need to jump back to the folder you just left. The command cd - does exactly that. It toggles between your current and previous directory.
Example: You are in /home/username/Documents and run cd /var/log. Now you are in /var/log. Type cd - and you return to /home/username/Documents. Type cd - again to go back to /var/log.
This is a huge time saver when you are switching between two locations frequently.
Changing To A Directory With A Symbolic Link
Symbolic links are shortcuts to other folders. When you use cd on a symbolic link, you usually go to the target directory. But the behavior can vary depending on your shell settings.
By default, cd follows the link. So if /home/username/link points to /var/www, typing cd link takes you to /var/www.
If you want to stay in the link location, use the -P option: cd -P link. This tells the shell to use the physical path of the link itself.
Using CDPATH For Faster Navigation
CDPATH is an environment variable that defines a list of directories to search when you use a relative path. If you set CDPATH to include /home/username, then typing cd Documents works even if you are not inside /home/username.
To set it temporarily:
export CDPATH=/home/username
Add this to your .bashrc file to make it permanent. This is an advanced trick but can speed up your workflow.
Common Mistakes And How To Avoid Them
New users often forget that Linux is case-sensitive. “Documents” and “documents” are different folders. Always match the exact case.
Another mistake is typing cd with a space before the path. That is fine, but do not add extra spaces inside the path. Also, remember that cd only works with directories, not files.
If you get “Permission denied,” you do not have access to that folder. Use sudo only if you understand the risks.
Practical Examples
Here are some real-world scenarios to practice.
- You are in
/home/usernameand want to go to/etc/nginx. Typecd /etc/nginx. - You are in
/var/www/htmland want to go up two levels. Typecd ../..to reach/var. - You want to go to your Downloads folder from anywhere. Type
cd ~/Downloads. - You are in
/tmpand want to return to your previous folder. Typecd -.
Practice these until they become second nature. The more you use the terminal, the faster you will navigate.
Using Pushd And Popd For Stack Navigation
If you need to remember multiple directories, use pushd and popd. These commands maintain a stack of directories.
pushd /var/log changes to that folder and adds it to the stack. pushd /etc changes to /etc and pushes the previous location onto the stack.
Type popd to return to the last directory in the stack. This is useful for complex workflows where you jump between many folders.
Checking Directory Contents Before Changing
Before you move into a folder, you might want to see what is inside. Use ls to list files and folders. Combine it with cd for efficient browsing.
Example: ls /var shows the folders inside /var. Then cd /var/log moves you into the log folder.
You can also use ls -la to see hidden files and detailed information.
Using Find With CD
Sometimes you do not know the exact path. The find command can locate folders, but you cannot directly pipe it into cd. Instead, use command substitution.
cd $(find / -type d -name "target_folder" 2>/dev/null)
This searches the entire system for a folder named “target_folder” and changes to it. The 2>/dev/null suppresses error messages.
Automating Navigation With Aliases
If you frequently go to the same folders, create an alias. Add this to your .bashrc file:
alias docs='cd ~/Documents'
Now typing docs takes you directly to Documents. You can create aliases for any long path.
Handling Long Paths
For very deep folder structures, consider using the CDPATH trick or symbolic links. You can also create a bookmark system using scripts.
Another option is to use the autojump tool. It learns which folders you visit most and lets you jump to them with a partial name.
Summary Of Key Commands
cd– Go to home directorycd /path– Go to absolute pathcd folder– Go to relative pathcd ..– Go up one levelcd -– Go to previous directorycd ~– Go to home directorycd /– Go to root directory
Master these, and you will navigate the Linux file system with confidence.
Frequently Asked Questions
What Is The Command To Change Directory In Linux?
The command is cd. Type cd followed by the path to the directory you want to enter.
How Do I Go Back To The Previous Directory?
Use cd - to return to the directory you were in before your last cd command.
Can I Change To A Directory With Spaces In Its Name?
Yes. Enclose the path in quotes, like cd "My Folder", or use a backslash before the space, like cd My\ Folder.
Why Does Cd Not Work For A Folder?
Check if the folder exists, if you have permission, and if you typed the name correctly including case. Use ls to verify.
What Is The Difference Between Absolute And Relative Paths?
Absolute paths start from the root directory (/). Relative paths start from your current directory. Both work with cd.
Now you have a solid understanding of how to change directory in linux. Practice these commands in your terminal, and soon you will navigate like a pro. Remember to use tab completion, check your current directory with pwd, and don’t be afraid to experiment. The terminal is your friend, and every command you learn makes you more efficient.