What is the cd command in Linux?

Overview

The cd command in Linux is used to navigate directories.

Syntax


cd folder_path

Parameters

folder_path: This is the path to the folder which we want to navigate to.

Options

  • cd ..: This moves one directory up.

  • cd: This moves to the home folder.

  • cd ~: This moves to a previous directory.

Code

Example 1

# print current directory
pwd
echo ............
# navigate to current directory
cd /usercode
# list files in current directory
ls

Explanation

  • Line 2: We print the current directory to the console.

  • Line 6: We navigate to the /usercode directory.

  • Line 8: We print the files in the /usercode directory to the console.

Example 2

# print the current folder
echo "print current folder..."
pwd
# list files in curren folder
ls
# move to a directory one level up
echo "changing folder..."
cd ..
# print files in the navigated directory
ls

Explanation

  • Line 3: We print the current directory.

  • Line 6: The files in the current directory are printed to the console.

  • Line 10: We move to a directory one level up.

  • Line 13: We print all the files present in the directory.

Free Resources