what is the difference between pwd and cd ?

25 views (last 30 days)
Leonardo Wayne
Leonardo Wayne on 21 Mar 2016
Edited: Walter Roberson on 10 Jul 2025
what is the difference between pwd and cd ? I get the same folder when I tyoe command in MATLAB.

Answers (3)

Stephen23
Stephen23 on 21 Mar 2016
Edited: Stephen23 on 21 Mar 2016
Actually pwd just calls cd internally, so there is no real difference.
However I would suggest using pwd: Changing directories is a very slow way to write code (it is much faster to pass relative/absolute paths to any functions that access files of any kind), so using pwd has the advantage that it stylistically make it clear "this does NOT change the directory, we just need the path...".
In other words, using pwd encourages better programming habits.
Some faster alternatives to cd / pwd:
  • relative path names
  • the '.' notation refers to the current directory.

Nina
Nina on 10 Jul 2025
pwd stands for "Print Working Directory", while cd stands for "Change Directory". Use pwd to see what folder you are currently in and use cd to go to a different folder. For example use cd .. to go up one folder or cd Desktop/ to go to your desktop.
  2 Comments
Walter Roberson
Walter Roberson on 10 Jul 2025
Edited: Walter Roberson on 10 Jul 2025
cd Desktop/
will attempt to switch directories to the directory named Desktop underneath the current directory.
For MacOS and Linux,
cd ~/Desktop
which change directories to your desktop
As far as I know, there is no Windows equivalent to the ~ shortcut.
Walter Roberson
Walter Roberson on 10 Jul 2025
Edited: Walter Roberson on 10 Jul 2025
if ispc()
cd( fullfile(getenv('userprofile'), 'Desktop'))
else
cd ~/Desktop
end

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Jul 2025
Both cd and pwd come from Unix.
Historically cd in Unix was used to change directories, and had no ability to return the name of the current directory. Historically cd in Unix with no parameters changed directories to the "home" directory.
Historically pwd in Unix was used to return the name of the current directory.
As implemented in MATLAB, cd is primarily used to change directories, and primarily returns the old directory, but cd with no parameters returns the current directory.
As implemented in MATLAB, pwd returns the current directory, with no option for changing directory.
Functionally, in MATLAB, you can use cd with no parameters interchangeably with pwd with no parameters. The reason pwd still exists is a convenience

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!