Does cd . have use?
One of the tutorials I've been following briefly stated that cd . has no use. When trying to replicate issue shown by OP in Symbolic link recursion - what makes it “reset”?, I also tried cd ., which showed the same effect OP described (growing $PWD variable), which can be countered with cd -P.
This makes me wonder, is there any case where one does in fact would want to use cd . ?
cd-command
|
show 6 more comments
One of the tutorials I've been following briefly stated that cd . has no use. When trying to replicate issue shown by OP in Symbolic link recursion - what makes it “reset”?, I also tried cd ., which showed the same effect OP described (growing $PWD variable), which can be countered with cd -P.
This makes me wonder, is there any case where one does in fact would want to use cd . ?
cd-command
12
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually usecd .to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.
– Lie Ryan
Jan 22 at 6:46
5
Besides the (obvious) effect on$PWD,cd .also changes$OLDPWDto the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…
– Andreas Wiese
2 days ago
4
I don't think I've ever neededcd ., though seeing the answers below, I might in the future, but I have on occasion usedpushd .when I wanted to be able topopdback to this directory later. e.g. when running a build script that doesconfigure,cd output...andmake, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it aspushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to notpopdsometimes, and thenpopdlater instead.
– 3D1T0R
2 days ago
1
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
1
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of/bin/cdhere unix.stackexchange.com/q/50058/85039
– Sergiy Kolodyazhnyy
7 hours ago
|
show 6 more comments
One of the tutorials I've been following briefly stated that cd . has no use. When trying to replicate issue shown by OP in Symbolic link recursion - what makes it “reset”?, I also tried cd ., which showed the same effect OP described (growing $PWD variable), which can be countered with cd -P.
This makes me wonder, is there any case where one does in fact would want to use cd . ?
cd-command
One of the tutorials I've been following briefly stated that cd . has no use. When trying to replicate issue shown by OP in Symbolic link recursion - what makes it “reset”?, I also tried cd ., which showed the same effect OP described (growing $PWD variable), which can be countered with cd -P.
This makes me wonder, is there any case where one does in fact would want to use cd . ?
cd-command
cd-command
asked Jan 22 at 4:12
Sergiy KolodyazhnyySergiy Kolodyazhnyy
9,31222659
9,31222659
12
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually usecd .to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.
– Lie Ryan
Jan 22 at 6:46
5
Besides the (obvious) effect on$PWD,cd .also changes$OLDPWDto the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…
– Andreas Wiese
2 days ago
4
I don't think I've ever neededcd ., though seeing the answers below, I might in the future, but I have on occasion usedpushd .when I wanted to be able topopdback to this directory later. e.g. when running a build script that doesconfigure,cd output...andmake, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it aspushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to notpopdsometimes, and thenpopdlater instead.
– 3D1T0R
2 days ago
1
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
1
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of/bin/cdhere unix.stackexchange.com/q/50058/85039
– Sergiy Kolodyazhnyy
7 hours ago
|
show 6 more comments
12
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually usecd .to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.
– Lie Ryan
Jan 22 at 6:46
5
Besides the (obvious) effect on$PWD,cd .also changes$OLDPWDto the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…
– Andreas Wiese
2 days ago
4
I don't think I've ever neededcd ., though seeing the answers below, I might in the future, but I have on occasion usedpushd .when I wanted to be able topopdback to this directory later. e.g. when running a build script that doesconfigure,cd output...andmake, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it aspushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to notpopdsometimes, and thenpopdlater instead.
– 3D1T0R
2 days ago
1
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
1
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of/bin/cdhere unix.stackexchange.com/q/50058/85039
– Sergiy Kolodyazhnyy
7 hours ago
12
12
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually use
cd . to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.– Lie Ryan
Jan 22 at 6:46
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually use
cd . to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.– Lie Ryan
Jan 22 at 6:46
5
5
Besides the (obvious) effect on
$PWD, cd . also changes $OLDPWD to the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…– Andreas Wiese
2 days ago
Besides the (obvious) effect on
$PWD, cd . also changes $OLDPWD to the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…– Andreas Wiese
2 days ago
4
4
I don't think I've ever needed
cd ., though seeing the answers below, I might in the future, but I have on occasion used pushd . when I wanted to be able to popd back to this directory later. e.g. when running a build script that does configure, cd output... and make, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it as pushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to not popd sometimes, and then popd later instead.– 3D1T0R
2 days ago
I don't think I've ever needed
cd ., though seeing the answers below, I might in the future, but I have on occasion used pushd . when I wanted to be able to popd back to this directory later. e.g. when running a build script that does configure, cd output... and make, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it as pushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to not popd sometimes, and then popd later instead.– 3D1T0R
2 days ago
1
1
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
1
1
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of
/bin/cd here unix.stackexchange.com/q/50058/85039– Sergiy Kolodyazhnyy
7 hours ago
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of
/bin/cd here unix.stackexchange.com/q/50058/85039– Sergiy Kolodyazhnyy
7 hours ago
|
show 6 more comments
9 Answers
9
active
oldest
votes
I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution (think of any situation where you might cd to the directory containing a file, whose path is supplied by the user). Therefore, it doesn't have to have some specific use: as long as it fulfills the usual semantics of cd <some-path>, it is useful.
7
Agreed,.should be treated as a valid path specified bycdsyntax just fine.
– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
You can add the following example: Loops like whileIFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces.as path, so that the commandcd "$Dir"expands tocd .. So, in scripts, it is perfectly useful.
– rexkogitans
Jan 22 at 9:43
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put thecdand subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.
– roaima
Jan 22 at 11:39
2
For example, a script actually runscd ${path_to_directory}, but at some point it turns out the directory is the current directory, andpath_to_directory = .so you would needcd .to work just in case.
– Demis
2 days ago
2
In other words, its utility is in the fact that it makes extra code (ifchecks andelseclauses, any kind of special casing) unnecessary.
– jpmc26
20 hours ago
|
show 1 more comment
The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in the question, so calling cd . which makes the shell issue the getcwd() syscall will ensure your current path is still valid.
Steps to reproduce in bash:
- In a terminal tab issue
mkdir ./dir_no_1; cd ./dir_no_1
- In a different terminal tab issue
mv dir_no_1 dir_no_2
- In the first terminal tab issue
echo $PWDandpwd. Notice that the directory has been externally renamed; the shell's environment has not been updated. - Issue
cd .; pwd; echo $PWD. Notice the value has been updated.
ksh93, however, does not update the environment information, so cd . in ksh93 may in fact be useless. In /bin/dash on Ubuntu and other Debian-based systems, cd . returns dash: 3: cd: can't cd to . error, however cd -P . works (unlike in ksh93).
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
I think you can domv ../dir_no_1 ../dir_no_2in the same terminal/bash.
– ctrl-alt-delor
Jan 22 at 9:00
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible thatpwdmight be a shell builtin that bypasses the syscall togetcwd()and is basically an alias forecho $PWD- see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real"pwd, but you might want to explicitly specify/bin/pwdjust in case. Shells sure are fun, eh?
– ymbirtt
Jan 22 at 9:44
|
show 2 more comments
Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following -
- Create a directory
temp
cd tempand then do anls
- Open another terminal and delete and then recreate that directory
temp
- Back from the first terminal, try doing an ls. This would result in an error -
ls: cannot open directory .: Stale file handle
cd .and then doing an ls works fine
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
This doesn't always work. In dash, for example, you'll get:cd: can't cd to .Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)
– Olorin
Jan 22 at 7:16
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issuecd .to move to the new directory with the same name.
– HP Williams
Jan 22 at 10:22
1
I usecd .all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.
– jamesdlin
2 days ago
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content ofPWDenvironment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.
– Stéphane Gourichon
2 days ago
1
@Wildcard, ah. It's dependent on the shell being used. For examplebashtranslatescd .tocd "$PWD"but not every shell does this.
– roaima
yesterday
|
show 2 more comments
You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
add a comment |
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
ls -ld "$file"
With a path such as "fred.txt" the directory will become ., leading to cd .
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. Thedirnamecommand generates.where necessary to avoid breaking code that expects to be able to split a path.
– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important thatcd .be a no-op (that doesn’t throw an error) because a script might docd "$var"and$varmight be..” I believe that that’s not what the question is asking about.
– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these linescd .does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.
– roaima
yesterday
2
@G-Man question asks, "is there any case where one does in fact would want to usecd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today thatbash(for example) interpretscd .to meancd "$PWD".
– roaima
yesterday
|
show 4 more comments
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
add a comment |
Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name . may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So . must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd . must be a valid command.
Whether cd . is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh on FreeBSD and NetBSD) will convert cd "" into cd ., which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir(""), which POSIX mandates must fail.
Some other shells will replace the . with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
add a comment |
I used this command just today when I rebased the branch I was working on in Git, from within a directory which had first been created on that same branch. The rebase went fine but afterwards, git status threw an error. After cd . everything was normal.
(I was working in MobaXterm on Windows, incidentally. Just in case you're trying to reproduce this. It may not happen on other systems.)
I also have used this command in directories that are refreshed by an automated process that moves aside the old directory and replaces it with a new one (so it is as close to atomic as possible). Not a common situation but cd . is exactly what's needed.
After reading this excellent answer from Stephane Chazelas:
- https://unix.stackexchange.com/a/79621/135943
I now understand that my use cases above only work because I am using bash, in which cd . is equivalent to cd "$PWD". I highly recommend reading the linked answer.
add a comment |
No, it has no sense.
Neither in scripting , it just does nothing.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Depending on the shell, it would reset$PWD, and it would possibly call other shell functions if the user has provided their owncdfunction or alias to overload the built incd. It would also verify that the current directory is still valid and that the current use has permission to be there.
– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,cd .would complain.
– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution (think of any situation where you might cd to the directory containing a file, whose path is supplied by the user). Therefore, it doesn't have to have some specific use: as long as it fulfills the usual semantics of cd <some-path>, it is useful.
7
Agreed,.should be treated as a valid path specified bycdsyntax just fine.
– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
You can add the following example: Loops like whileIFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces.as path, so that the commandcd "$Dir"expands tocd .. So, in scripts, it is perfectly useful.
– rexkogitans
Jan 22 at 9:43
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put thecdand subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.
– roaima
Jan 22 at 11:39
2
For example, a script actually runscd ${path_to_directory}, but at some point it turns out the directory is the current directory, andpath_to_directory = .so you would needcd .to work just in case.
– Demis
2 days ago
2
In other words, its utility is in the fact that it makes extra code (ifchecks andelseclauses, any kind of special casing) unnecessary.
– jpmc26
20 hours ago
|
show 1 more comment
I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution (think of any situation where you might cd to the directory containing a file, whose path is supplied by the user). Therefore, it doesn't have to have some specific use: as long as it fulfills the usual semantics of cd <some-path>, it is useful.
7
Agreed,.should be treated as a valid path specified bycdsyntax just fine.
– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
You can add the following example: Loops like whileIFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces.as path, so that the commandcd "$Dir"expands tocd .. So, in scripts, it is perfectly useful.
– rexkogitans
Jan 22 at 9:43
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put thecdand subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.
– roaima
Jan 22 at 11:39
2
For example, a script actually runscd ${path_to_directory}, but at some point it turns out the directory is the current directory, andpath_to_directory = .so you would needcd .to work just in case.
– Demis
2 days ago
2
In other words, its utility is in the fact that it makes extra code (ifchecks andelseclauses, any kind of special casing) unnecessary.
– jpmc26
20 hours ago
|
show 1 more comment
I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution (think of any situation where you might cd to the directory containing a file, whose path is supplied by the user). Therefore, it doesn't have to have some specific use: as long as it fulfills the usual semantics of cd <some-path>, it is useful.
I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution (think of any situation where you might cd to the directory containing a file, whose path is supplied by the user). Therefore, it doesn't have to have some specific use: as long as it fulfills the usual semantics of cd <some-path>, it is useful.
answered Jan 22 at 4:33
OlorinOlorin
2,4441415
2,4441415
7
Agreed,.should be treated as a valid path specified bycdsyntax just fine.
– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
You can add the following example: Loops like whileIFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces.as path, so that the commandcd "$Dir"expands tocd .. So, in scripts, it is perfectly useful.
– rexkogitans
Jan 22 at 9:43
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put thecdand subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.
– roaima
Jan 22 at 11:39
2
For example, a script actually runscd ${path_to_directory}, but at some point it turns out the directory is the current directory, andpath_to_directory = .so you would needcd .to work just in case.
– Demis
2 days ago
2
In other words, its utility is in the fact that it makes extra code (ifchecks andelseclauses, any kind of special casing) unnecessary.
– jpmc26
20 hours ago
|
show 1 more comment
7
Agreed,.should be treated as a valid path specified bycdsyntax just fine.
– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
You can add the following example: Loops like whileIFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces.as path, so that the commandcd "$Dir"expands tocd .. So, in scripts, it is perfectly useful.
– rexkogitans
Jan 22 at 9:43
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put thecdand subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.
– roaima
Jan 22 at 11:39
2
For example, a script actually runscd ${path_to_directory}, but at some point it turns out the directory is the current directory, andpath_to_directory = .so you would needcd .to work just in case.
– Demis
2 days ago
2
In other words, its utility is in the fact that it makes extra code (ifchecks andelseclauses, any kind of special casing) unnecessary.
– jpmc26
20 hours ago
7
7
Agreed,
. should be treated as a valid path specified by cd syntax just fine.– Sergiy Kolodyazhnyy
Jan 22 at 4:52
Agreed,
. should be treated as a valid path specified by cd syntax just fine.– Sergiy Kolodyazhnyy
Jan 22 at 4:52
12
12
You can add the following example: Loops like while
IFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces . as path, so that the command cd "$Dir" expands to cd .. So, in scripts, it is perfectly useful.– rexkogitans
Jan 22 at 9:43
You can add the following example: Loops like while
IFS= read Dir; do cd "$Dir"; do_something; done < <(find . -type d). During its course, find produces . as path, so that the command cd "$Dir" expands to cd .. So, in scripts, it is perfectly useful.– rexkogitans
Jan 22 at 9:43
1
1
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put the
cd and subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.– roaima
Jan 22 at 11:39
@rexkogitans your illustration is clear but the code would fail on its second iteration. You would need to put the
cd and subsequent code in a subshell so that the relative directory path remained relative to the starting point rather than to the previous iteration.– roaima
Jan 22 at 11:39
2
2
For example, a script actually runs
cd ${path_to_directory}, but at some point it turns out the directory is the current directory, and path_to_directory = . so you would need cd . to work just in case.– Demis
2 days ago
For example, a script actually runs
cd ${path_to_directory}, but at some point it turns out the directory is the current directory, and path_to_directory = . so you would need cd . to work just in case.– Demis
2 days ago
2
2
In other words, its utility is in the fact that it makes extra code (
if checks and else clauses, any kind of special casing) unnecessary.– jpmc26
20 hours ago
In other words, its utility is in the fact that it makes extra code (
if checks and else clauses, any kind of special casing) unnecessary.– jpmc26
20 hours ago
|
show 1 more comment
The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in the question, so calling cd . which makes the shell issue the getcwd() syscall will ensure your current path is still valid.
Steps to reproduce in bash:
- In a terminal tab issue
mkdir ./dir_no_1; cd ./dir_no_1
- In a different terminal tab issue
mv dir_no_1 dir_no_2
- In the first terminal tab issue
echo $PWDandpwd. Notice that the directory has been externally renamed; the shell's environment has not been updated. - Issue
cd .; pwd; echo $PWD. Notice the value has been updated.
ksh93, however, does not update the environment information, so cd . in ksh93 may in fact be useless. In /bin/dash on Ubuntu and other Debian-based systems, cd . returns dash: 3: cd: can't cd to . error, however cd -P . works (unlike in ksh93).
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
I think you can domv ../dir_no_1 ../dir_no_2in the same terminal/bash.
– ctrl-alt-delor
Jan 22 at 9:00
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible thatpwdmight be a shell builtin that bypasses the syscall togetcwd()and is basically an alias forecho $PWD- see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real"pwd, but you might want to explicitly specify/bin/pwdjust in case. Shells sure are fun, eh?
– ymbirtt
Jan 22 at 9:44
|
show 2 more comments
The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in the question, so calling cd . which makes the shell issue the getcwd() syscall will ensure your current path is still valid.
Steps to reproduce in bash:
- In a terminal tab issue
mkdir ./dir_no_1; cd ./dir_no_1
- In a different terminal tab issue
mv dir_no_1 dir_no_2
- In the first terminal tab issue
echo $PWDandpwd. Notice that the directory has been externally renamed; the shell's environment has not been updated. - Issue
cd .; pwd; echo $PWD. Notice the value has been updated.
ksh93, however, does not update the environment information, so cd . in ksh93 may in fact be useless. In /bin/dash on Ubuntu and other Debian-based systems, cd . returns dash: 3: cd: can't cd to . error, however cd -P . works (unlike in ksh93).
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
I think you can domv ../dir_no_1 ../dir_no_2in the same terminal/bash.
– ctrl-alt-delor
Jan 22 at 9:00
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible thatpwdmight be a shell builtin that bypasses the syscall togetcwd()and is basically an alias forecho $PWD- see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real"pwd, but you might want to explicitly specify/bin/pwdjust in case. Shells sure are fun, eh?
– ymbirtt
Jan 22 at 9:44
|
show 2 more comments
The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in the question, so calling cd . which makes the shell issue the getcwd() syscall will ensure your current path is still valid.
Steps to reproduce in bash:
- In a terminal tab issue
mkdir ./dir_no_1; cd ./dir_no_1
- In a different terminal tab issue
mv dir_no_1 dir_no_2
- In the first terminal tab issue
echo $PWDandpwd. Notice that the directory has been externally renamed; the shell's environment has not been updated. - Issue
cd .; pwd; echo $PWD. Notice the value has been updated.
ksh93, however, does not update the environment information, so cd . in ksh93 may in fact be useless. In /bin/dash on Ubuntu and other Debian-based systems, cd . returns dash: 3: cd: can't cd to . error, however cd -P . works (unlike in ksh93).
The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in the question, so calling cd . which makes the shell issue the getcwd() syscall will ensure your current path is still valid.
Steps to reproduce in bash:
- In a terminal tab issue
mkdir ./dir_no_1; cd ./dir_no_1
- In a different terminal tab issue
mv dir_no_1 dir_no_2
- In the first terminal tab issue
echo $PWDandpwd. Notice that the directory has been externally renamed; the shell's environment has not been updated. - Issue
cd .; pwd; echo $PWD. Notice the value has been updated.
ksh93, however, does not update the environment information, so cd . in ksh93 may in fact be useless. In /bin/dash on Ubuntu and other Debian-based systems, cd . returns dash: 3: cd: can't cd to . error, however cd -P . works (unlike in ksh93).
edited 2 days ago
psmears
43928
43928
answered Jan 22 at 4:12
Sergiy KolodyazhnyySergiy Kolodyazhnyy
9,31222659
9,31222659
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
I think you can domv ../dir_no_1 ../dir_no_2in the same terminal/bash.
– ctrl-alt-delor
Jan 22 at 9:00
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible thatpwdmight be a shell builtin that bypasses the syscall togetcwd()and is basically an alias forecho $PWD- see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real"pwd, but you might want to explicitly specify/bin/pwdjust in case. Shells sure are fun, eh?
– ymbirtt
Jan 22 at 9:44
|
show 2 more comments
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
I think you can domv ../dir_no_1 ../dir_no_2in the same terminal/bash.
– ctrl-alt-delor
Jan 22 at 9:00
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible thatpwdmight be a shell builtin that bypasses the syscall togetcwd()and is basically an alias forecho $PWD- see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real"pwd, but you might want to explicitly specify/bin/pwdjust in case. Shells sure are fun, eh?
– ymbirtt
Jan 22 at 9:44
15
15
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
Good to know: I'll add that to my list of useless informations. ^^)
– jayooin
Jan 22 at 6:39
7
7
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
@jayooin Glad I could contribute to the list ;)
– Sergiy Kolodyazhnyy
Jan 22 at 7:53
4
4
I think you can do
mv ../dir_no_1 ../dir_no_2 in the same terminal/bash.– ctrl-alt-delor
Jan 22 at 9:00
I think you can do
mv ../dir_no_1 ../dir_no_2 in the same terminal/bash.– ctrl-alt-delor
Jan 22 at 9:00
2
2
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
@ctrl-alt-delor Confirmed, works :)
– Sergiy Kolodyazhnyy
Jan 22 at 9:12
It's also possible that
pwd might be a shell builtin that bypasses the syscall to getcwd() and is basically an alias for echo $PWD - see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real" pwd, but you might want to explicitly specify /bin/pwd just in case. Shells sure are fun, eh?– ymbirtt
Jan 22 at 9:44
It's also possible that
pwd might be a shell builtin that bypasses the syscall to getcwd() and is basically an alias for echo $PWD - see unix.stackexchange.com/questions/145479/…. I can't test that there's a difference since my shells use the "real" pwd, but you might want to explicitly specify /bin/pwd just in case. Shells sure are fun, eh?– ymbirtt
Jan 22 at 9:44
|
show 2 more comments
Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following -
- Create a directory
temp
cd tempand then do anls
- Open another terminal and delete and then recreate that directory
temp
- Back from the first terminal, try doing an ls. This would result in an error -
ls: cannot open directory .: Stale file handle
cd .and then doing an ls works fine
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
This doesn't always work. In dash, for example, you'll get:cd: can't cd to .Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)
– Olorin
Jan 22 at 7:16
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issuecd .to move to the new directory with the same name.
– HP Williams
Jan 22 at 10:22
1
I usecd .all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.
– jamesdlin
2 days ago
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content ofPWDenvironment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.
– Stéphane Gourichon
2 days ago
1
@Wildcard, ah. It's dependent on the shell being used. For examplebashtranslatescd .tocd "$PWD"but not every shell does this.
– roaima
yesterday
|
show 2 more comments
Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following -
- Create a directory
temp
cd tempand then do anls
- Open another terminal and delete and then recreate that directory
temp
- Back from the first terminal, try doing an ls. This would result in an error -
ls: cannot open directory .: Stale file handle
cd .and then doing an ls works fine
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
This doesn't always work. In dash, for example, you'll get:cd: can't cd to .Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)
– Olorin
Jan 22 at 7:16
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issuecd .to move to the new directory with the same name.
– HP Williams
Jan 22 at 10:22
1
I usecd .all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.
– jamesdlin
2 days ago
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content ofPWDenvironment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.
– Stéphane Gourichon
2 days ago
1
@Wildcard, ah. It's dependent on the shell being used. For examplebashtranslatescd .tocd "$PWD"but not every shell does this.
– roaima
yesterday
|
show 2 more comments
Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following -
- Create a directory
temp
cd tempand then do anls
- Open another terminal and delete and then recreate that directory
temp
- Back from the first terminal, try doing an ls. This would result in an error -
ls: cannot open directory .: Stale file handle
cd .and then doing an ls works fine
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following -
- Create a directory
temp
cd tempand then do anls
- Open another terminal and delete and then recreate that directory
temp
- Back from the first terminal, try doing an ls. This would result in an error -
ls: cannot open directory .: Stale file handle
cd .and then doing an ls works fine
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Jan 22 at 7:08
Sahil AgarwalSahil Agarwal
43912
43912
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Sahil Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
This doesn't always work. In dash, for example, you'll get:cd: can't cd to .Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)
– Olorin
Jan 22 at 7:16
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issuecd .to move to the new directory with the same name.
– HP Williams
Jan 22 at 10:22
1
I usecd .all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.
– jamesdlin
2 days ago
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content ofPWDenvironment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.
– Stéphane Gourichon
2 days ago
1
@Wildcard, ah. It's dependent on the shell being used. For examplebashtranslatescd .tocd "$PWD"but not every shell does this.
– roaima
yesterday
|
show 2 more comments
3
This doesn't always work. In dash, for example, you'll get:cd: can't cd to .Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)
– Olorin
Jan 22 at 7:16
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issuecd .to move to the new directory with the same name.
– HP Williams
Jan 22 at 10:22
1
I usecd .all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.
– jamesdlin
2 days ago
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content ofPWDenvironment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.
– Stéphane Gourichon
2 days ago
1
@Wildcard, ah. It's dependent on the shell being used. For examplebashtranslatescd .tocd "$PWD"but not every shell does this.
– roaima
yesterday
3
3
This doesn't always work. In dash, for example, you'll get:
cd: can't cd to . Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)– Olorin
Jan 22 at 7:16
This doesn't always work. In dash, for example, you'll get:
cd: can't cd to . Now that I look at it, this is already mentioned in Sergiy's answer (moving, deleting/recreating - essentially the same: the directory you're in is no longer what was in the original path)– Olorin
Jan 22 at 7:16
11
11
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issue
cd . to move to the new directory with the same name.– HP Williams
Jan 22 at 10:22
I use this a lot testing remote deploys. The directory I'm in will be deleted then recreated by some automation and I'll need to issue
cd . to move to the new directory with the same name.– HP Williams
Jan 22 at 10:22
1
1
I use
cd . all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.– jamesdlin
2 days ago
I use
cd . all the time when I have a shell whose current working directory was mounted with sshfs but the ssh session has been closed and reopened.– jamesdlin
2 days ago
3
3
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content of
PWD environment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.– Stéphane Gourichon
2 days ago
In such a case I do "cd $PWD". Other variants might work, but this one clearly expresses the intent: extract what is supposed to be my current path (i.e. read content of
PWD environment variable), then walk the filesystem hierarchy from the root, down to a directory that happens to be reachable via that path, whether it is actually the same directory or not. This fits exactly the use case in this answer.– Stéphane Gourichon
2 days ago
1
1
@Wildcard, ah. It's dependent on the shell being used. For example
bash translates cd . to cd "$PWD" but not every shell does this.– roaima
yesterday
@Wildcard, ah. It's dependent on the shell being used. For example
bash translates cd . to cd "$PWD" but not every shell does this.– roaima
yesterday
|
show 2 more comments
You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
add a comment |
You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
add a comment |
You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
answered Jan 22 at 11:38
souser12345souser12345
1,099815
1,099815
add a comment |
add a comment |
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
ls -ld "$file"
With a path such as "fred.txt" the directory will become ., leading to cd .
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. Thedirnamecommand generates.where necessary to avoid breaking code that expects to be able to split a path.
– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important thatcd .be a no-op (that doesn’t throw an error) because a script might docd "$var"and$varmight be..” I believe that that’s not what the question is asking about.
– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these linescd .does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.
– roaima
yesterday
2
@G-Man question asks, "is there any case where one does in fact would want to usecd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today thatbash(for example) interpretscd .to meancd "$PWD".
– roaima
yesterday
|
show 4 more comments
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
ls -ld "$file"
With a path such as "fred.txt" the directory will become ., leading to cd .
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. Thedirnamecommand generates.where necessary to avoid breaking code that expects to be able to split a path.
– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important thatcd .be a no-op (that doesn’t throw an error) because a script might docd "$var"and$varmight be..” I believe that that’s not what the question is asking about.
– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these linescd .does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.
– roaima
yesterday
2
@G-Man question asks, "is there any case where one does in fact would want to usecd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today thatbash(for example) interpretscd .to meancd "$PWD".
– roaima
yesterday
|
show 4 more comments
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
ls -ld "$file"
With a path such as "fred.txt" the directory will become ., leading to cd .
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
ls -ld "$file"
With a path such as "fred.txt" the directory will become ., leading to cd .
answered Jan 22 at 8:00
roaimaroaima
43.8k554117
43.8k554117
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. Thedirnamecommand generates.where necessary to avoid breaking code that expects to be able to split a path.
– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important thatcd .be a no-op (that doesn’t throw an error) because a script might docd "$var"and$varmight be..” I believe that that’s not what the question is asking about.
– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these linescd .does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.
– roaima
yesterday
2
@G-Man question asks, "is there any case where one does in fact would want to usecd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today thatbash(for example) interpretscd .to meancd "$PWD".
– roaima
yesterday
|
show 4 more comments
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. Thedirnamecommand generates.where necessary to avoid breaking code that expects to be able to split a path.
– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important thatcd .be a no-op (that doesn’t throw an error) because a script might docd "$var"and$varmight be..” I believe that that’s not what the question is asking about.
– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these linescd .does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.
– roaima
yesterday
2
@G-Man question asks, "is there any case where one does in fact would want to usecd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today thatbash(for example) interpretscd .to meancd "$PWD".
– roaima
yesterday
1
1
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
It's useful that it doesn't throw an error if you're already in the directory that you're navigating to, but I wouldn't say that it is useful as a no-op.
– Captain Man
Jan 22 at 14:05
2
2
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. The
dirname command generates . where necessary to avoid breaking code that expects to be able to split a path.– roaima
2 days ago
@CaptainMan not throwing an error if you're already in the directory is (effectively) a no-op. The
dirname command generates . where necessary to avoid breaking code that expects to be able to split a path.– roaima
2 days ago
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important that
cd . be a no-op (that doesn’t throw an error) because a script might do cd "$var" and $var might be ..” I believe that that’s not what the question is asking about.– G-Man
yesterday
I’m not sure I understand what you’re saying. I guess you’re saying “It’s important that
cd . be a no-op (that doesn’t throw an error) because a script might do cd "$var" and $var might be ..” I believe that that’s not what the question is asking about.– G-Man
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these lines
cd . does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.– roaima
yesterday
@G-Man question asks if it's any use. I've provided a SME that illustrates a scenario where it would be of use. In the particular situations where I've seen it used code that works along these lines
cd . does nothing useful other than having no practical effect without erroring. To my mind that's a no-op.– roaima
yesterday
2
2
@G-Man question asks, "is there any case where one does in fact would want to use
cd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today that bash (for example) interprets cd . to mean cd "$PWD".– roaima
yesterday
@G-Man question asks, "is there any case where one does in fact would want to use
cd .?". It doesn't restrict itself to interactive usage. My answer doesn't consider interactive use, because I didn't realise until today that bash (for example) interprets cd . to mean cd "$PWD".– roaima
yesterday
|
show 4 more comments
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
add a comment |
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
add a comment |
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
answered 2 days ago
user23013user23013
557311
557311
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
add a comment |
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
Won't that depend upon what sort of device, how it's being accessed, its filesystem, the OS, &c?
– gidds
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
OS, maybe. It's unlikely the filesystem is relevant, as long as the kernel can find its way unmounting it while it is being used. In any case, the command has its use in the exactly right situation.
– user23013
yesterday
add a comment |
Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name . may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So . must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd . must be a valid command.
Whether cd . is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh on FreeBSD and NetBSD) will convert cd "" into cd ., which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir(""), which POSIX mandates must fail.
Some other shells will replace the . with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
add a comment |
Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name . may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So . must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd . must be a valid command.
Whether cd . is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh on FreeBSD and NetBSD) will convert cd "" into cd ., which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir(""), which POSIX mandates must fail.
Some other shells will replace the . with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
add a comment |
Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name . may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So . must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd . must be a valid command.
Whether cd . is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh on FreeBSD and NetBSD) will convert cd "" into cd ., which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir(""), which POSIX mandates must fail.
Some other shells will replace the . with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name . may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So . must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd . must be a valid command.
Whether cd . is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh on FreeBSD and NetBSD) will convert cd "" into cd ., which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir(""), which POSIX mandates must fail.
Some other shells will replace the . with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
edited yesterday
Toby Speight
5,29211031
5,29211031
answered 2 days ago
Greg A. WoodsGreg A. Woods
44036
44036
add a comment |
add a comment |
I used this command just today when I rebased the branch I was working on in Git, from within a directory which had first been created on that same branch. The rebase went fine but afterwards, git status threw an error. After cd . everything was normal.
(I was working in MobaXterm on Windows, incidentally. Just in case you're trying to reproduce this. It may not happen on other systems.)
I also have used this command in directories that are refreshed by an automated process that moves aside the old directory and replaces it with a new one (so it is as close to atomic as possible). Not a common situation but cd . is exactly what's needed.
After reading this excellent answer from Stephane Chazelas:
- https://unix.stackexchange.com/a/79621/135943
I now understand that my use cases above only work because I am using bash, in which cd . is equivalent to cd "$PWD". I highly recommend reading the linked answer.
add a comment |
I used this command just today when I rebased the branch I was working on in Git, from within a directory which had first been created on that same branch. The rebase went fine but afterwards, git status threw an error. After cd . everything was normal.
(I was working in MobaXterm on Windows, incidentally. Just in case you're trying to reproduce this. It may not happen on other systems.)
I also have used this command in directories that are refreshed by an automated process that moves aside the old directory and replaces it with a new one (so it is as close to atomic as possible). Not a common situation but cd . is exactly what's needed.
After reading this excellent answer from Stephane Chazelas:
- https://unix.stackexchange.com/a/79621/135943
I now understand that my use cases above only work because I am using bash, in which cd . is equivalent to cd "$PWD". I highly recommend reading the linked answer.
add a comment |
I used this command just today when I rebased the branch I was working on in Git, from within a directory which had first been created on that same branch. The rebase went fine but afterwards, git status threw an error. After cd . everything was normal.
(I was working in MobaXterm on Windows, incidentally. Just in case you're trying to reproduce this. It may not happen on other systems.)
I also have used this command in directories that are refreshed by an automated process that moves aside the old directory and replaces it with a new one (so it is as close to atomic as possible). Not a common situation but cd . is exactly what's needed.
After reading this excellent answer from Stephane Chazelas:
- https://unix.stackexchange.com/a/79621/135943
I now understand that my use cases above only work because I am using bash, in which cd . is equivalent to cd "$PWD". I highly recommend reading the linked answer.
I used this command just today when I rebased the branch I was working on in Git, from within a directory which had first been created on that same branch. The rebase went fine but afterwards, git status threw an error. After cd . everything was normal.
(I was working in MobaXterm on Windows, incidentally. Just in case you're trying to reproduce this. It may not happen on other systems.)
I also have used this command in directories that are refreshed by an automated process that moves aside the old directory and replaces it with a new one (so it is as close to atomic as possible). Not a common situation but cd . is exactly what's needed.
After reading this excellent answer from Stephane Chazelas:
- https://unix.stackexchange.com/a/79621/135943
I now understand that my use cases above only work because I am using bash, in which cd . is equivalent to cd "$PWD". I highly recommend reading the linked answer.
edited 18 hours ago
answered yesterday
WildcardWildcard
22.7k963165
22.7k963165
add a comment |
add a comment |
No, it has no sense.
Neither in scripting , it just does nothing.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Depending on the shell, it would reset$PWD, and it would possibly call other shell functions if the user has provided their owncdfunction or alias to overload the built incd. It would also verify that the current directory is still valid and that the current use has permission to be there.
– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,cd .would complain.
– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
add a comment |
No, it has no sense.
Neither in scripting , it just does nothing.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Depending on the shell, it would reset$PWD, and it would possibly call other shell functions if the user has provided their owncdfunction or alias to overload the built incd. It would also verify that the current directory is still valid and that the current use has permission to be there.
– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,cd .would complain.
– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
add a comment |
No, it has no sense.
Neither in scripting , it just does nothing.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
No, it has no sense.
Neither in scripting , it just does nothing.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered yesterday
Federico Federico
1
1
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Federico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Depending on the shell, it would reset$PWD, and it would possibly call other shell functions if the user has provided their owncdfunction or alias to overload the built incd. It would also verify that the current directory is still valid and that the current use has permission to be there.
– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,cd .would complain.
– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
add a comment |
1
Depending on the shell, it would reset$PWD, and it would possibly call other shell functions if the user has provided their owncdfunction or alias to overload the built incd. It would also verify that the current directory is still valid and that the current use has permission to be there.
– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,cd .would complain.
– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
1
1
Depending on the shell, it would reset
$PWD, and it would possibly call other shell functions if the user has provided their own cd function or alias to overload the built in cd. It would also verify that the current directory is still valid and that the current use has permission to be there.– Kusalananda
yesterday
Depending on the shell, it would reset
$PWD, and it would possibly call other shell functions if the user has provided their own cd function or alias to overload the built in cd. It would also verify that the current directory is still valid and that the current use has permission to be there.– Kusalananda
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1) of course we are not talking about possible custom aliases of "cd" but the standard build 2) how the current use can be there if it had no permission? To make it simple I just say that in real world there is no reason to use it in my opinion .
– Federico
yesterday
1
1
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,
cd . would complain.– Kusalananda
yesterday
1) Aren't we? 2) The real world is not simple, and Unix is a multi-user operating system. A user may change permissions on directories, and if the script, or the interactive shell of another user, happens to have that directory (or a subdirectory thereof) as its working directory,
cd . would complain.– Kusalananda
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
I got your point about answer #2 and it's interesting but in that case almost every possible commands related to that folder would also fail like "ls" for instance, not just cd .
– Federico
yesterday
2
2
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
Federico, according to the rules of the site and my own personal rules, I should downvote your answer. However, you are new. Welcome! Please review some of the other answers. After that, if you think your answer is wrong, please delete it. Please enjoy giving other answers to this question and others.
– daveloyall
yesterday
add a comment |
12
I have a custom .zshrc that runs various checks on the directory when switching directory, for example one of the check is to automatically activate/deactivate a matching virtualenv when moving directories. Occasionally, I might start a new shell or whatever, and those checks don't run, and I usually use
cd .to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.– Lie Ryan
Jan 22 at 6:46
5
Besides the (obvious) effect on
$PWD,cd .also changes$OLDPWDto the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…– Andreas Wiese
2 days ago
4
I don't think I've ever needed
cd ., though seeing the answers below, I might in the future, but I have on occasion usedpushd .when I wanted to be able topopdback to this directory later. e.g. when running a build script that doesconfigure,cd output...andmake, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it aspushd .; ./BuildScriptName.sh; popd, and this also gives me the freedom to notpopdsometimes, and thenpopdlater instead.– 3D1T0R
2 days ago
1
Not to mention of course that '.' and '..' are not implemented in the cd command itself, so no-one set out to make that specific feature, it's just a combination of things that serves no real purpose.
– David S
2 days ago
1
@ruakh Nope, external programs should not affect shell execution environment. It's mostly for POSIX compliance which requires some of the utilities to exist outside of the shell, and evaluating exit status of external commands. You can read about the purpose of
/bin/cdhere unix.stackexchange.com/q/50058/85039– Sergiy Kolodyazhnyy
7 hours ago