cd is a shell built-in command and stands for "changes directory".
You can point to directories in two ways:
- Absolute Paths: Like /home/devtalks/lpic1/lesson1
- Relative Paths: Like lpic1/lesson1 In this case, we are not adding the / in the beginning so the bash will try to find lpic1 directory where we are (local / relative)
You can check your current working directory with the pwd command.
Use cd - to switch back to the previous directory.
cd /path/to/first-directory
cd /path/to/second-directory
cd - # returns to /path/to/first-directoryUse cd ~ or just cd to return to your home directory.
cd ~ # or just: cdUse cd .. to move up one level.
cd .. # moves up one directory level
cd ../../ # moves up two directory levelsUse cd / to go to the root directory.
cd / # navigates to the root directoryUse cd ~username to access another user's home directory
cd ~ali # navigates to Ali's home directoryType part of a directory name and press Tab to auto-complete it.
cd /usr/lo[Press Tab] # auto-completes to /usr/local/Use cd !$ to reuse the last argument from the previous command.
echo /some/directory/path
cd !$ # equivalent to: cd /some/directory/pathSet CDPATH for quick navigation to frequent directories.
export CDPATH=.:~/projects:/var/log # seperated with colon
cd myproject # jumps to ~/projects/myproject if it existsNote: For persistent usage, you can add this to your .bashrc file.(use absolute path)
Use the shopt command to enable or disable automatic directory navigation without explicitly typing cd.
shopt -s autocd # enable auto cd
/path/to/dir # automatically navigates to /path/to/dir
shopt -u autocd # disable auto cdNote: For persistent usage, you can add this to your .bashrc file.
Use pushd to save directories and popd to return.
pushd /path/to/directory1
pushd /path/to/directory2
popd # returns to /path/to/directory1Tired of typing out long directory paths over and over? Use zoxide it's a smarter cd command. It remembers the directories you visit most frequently, letting you "jump" to them with just a few keystrokes.
Check out this YouTube video for a great explanation of zoxide's features.