what is the difference between pwd and cd ?

26 vues (au cours des 30 derniers jours)
Leonardo Wayne
Leonardo Wayne le 21 Mar 2016
what is the difference between pwd and cd ? I get the same folder when I tyoe command in MATLAB.

Réponses (3)

Stephen23
Stephen23 le 21 Mar 2016
Modifié(e) : Stephen23 le 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 le 10 Juil 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 commentaires
Walter Roberson
Walter Roberson le 10 Juil 2025
Modifié(e) : Walter Roberson le 10 Juil 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 le 10 Juil 2025
Modifié(e) : Walter Roberson le 10 Juil 2025
if ispc()
cd( fullfile(getenv('userprofile'), 'Desktop'))
else
cd ~/Desktop
end

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 10 Juil 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

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by