- relative path names
- the '.' notation refers to the current directory.
what is the difference between pwd and cd ?
24 次查看(过去 30 天)
显示 更早的评论
what is the difference between pwd and cd ? I get the same folder when I tyoe command in MATLAB.
0 个评论
回答(3 个)
Stephen23
2016-3-21
编辑:Stephen23
2016-3-21
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:
0 个评论
Nina
2025-7-10
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 个评论
Walter Roberson
2025-7-10
编辑:Walter Roberson
2025-7-10
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
2025-7-10
编辑:Walter Roberson
2025-7-10
if ispc()
cd( fullfile(getenv('userprofile'), 'Desktop'))
else
cd ~/Desktop
end
Walter Roberson
2025-7-10
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!