What does the command cd . mean? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I understand that cd .. goes up a directory, but what exactly does cd . do?
This is a question on my study guide, which is why I am asking.
Thanks!

As most people realize, cd . doesn't really do anything, because . means "the current directory". Changing to the current directory is seemingly pointless.
However, there is at least one interesting side effect:
When you cd to some directory, an environment variable OLDPWD is set, which allows you to execute cd - to return to the previous directory that you cd'ed from. When you execute cd ., OLDPWD actually gets set as the current directory, so it renders cd - ineffective in getting you back to the previous directory you were in.
Example:
$ cd /foo
$ cd /tmp
$ cd -
$ pwd
/foo
I'm in /foo just like I expected, but
$ cd /foo
$ cd /tmp
$ cd . # does nothing, right?
$ cd -
$ pwd
/tmp
Now I didn't return to /foo like I was hoping, due to this side effect!

Nothing. cd changes directory to the argument provided. And . means "the current directory" just as .. means "the parent of the current directory".

As others have said, cd . will change to the current directory, which has basically no effect in scripting/programming or to the operating system.
However that doesn't mean it will do nothing. In practice, you may be using a terminal to type this command. It will happily execute the command and - perhaps, though not likely - do something like echo the contents of the directory. If something else on your system (say a background process or another shell) have changed the contents of the directory, you may see modified output when the command echoes out the new directory listing.
In short: the cd . command doesn't have any real effect, but it could - in rare instances - have useful side effects that you could leverage in practical use.

Related

Why is the 'mv' command not working when moving a file up multiple levels [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
Operating system: SLES12 VM
So I start off in a directory:
DirA: /home/user/testA/testB
My goal is to move a file from this directory to a directory given by
DirB_rel: /home/user/testA/testB/../../deliverables/rpm/SOURCE
Note: testA is a symlink which is not included in DirB_abs
Which, when I cd to it, gives a pwd of
DirB_abs:/home/user/deliverables/rpm/SOURCE
The problem is, when I try move a file using mv (have tried tar.gz and .txt) from DirA to DirB_rel, the file is deleted from original location as expected, but it does not appear at new location and is therefore lost.
E.g. mv testFile.txt DirB_rel -> File disappears
However, when I use the absolute path for directory B, mv works correctly.
E.g. mv testFile.txt DirB_abs -> Success
Any idea whats going on here?
Thanks!
The problem is with the symlink. When you do user/testA/testB/../../ and testA is asymlink, you wont go back to user, but to the parent directory of the directory testA links to
the mv command will reference the directory you are currently in and not from where the file is. So if we are in home ~/ and want to move ~/A/file to ~/B/file you use mv as follows:
mv A/file B/
Note that if you use this
mv A/file ../B/
the command will look for B in /home/B and not ~/B since we are in the ~/ directory issuing the command.

What is the difference between . ./ ./* in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
when i copy files, execute the file in linux, I am Not quite understand the difference and how to use them.
please help. thanks.
. means current directory
./ means current directory, too
./* means all files in current directory
. means the current directory, and ./ is the same but more explicit, saying "Hey, I'm a directory!" It's like any other folder: TheFolder and TheFolder/ refer to the same object. One case where the meaning is different is when looking at a symlink pointing at a directory: TheLink can refer either to the link object or the directory that you pointed at, depending on the situation, while TheLink/ will always refer to the directory. Also, when you run the rsync command, it will treat TheFolder and TheFolder/ differently.
./* just means all the files in the current directory, same as *. Bash expands that asterisk before the command is run, so the program doesn't see the asterisk, instead seeing all the files as arguments. The difference between these two is that for the former, the command will see "./" prepended to each filename.
To see how these differ, echo is a safe command to run. It will just print the value of what you send it:
echo *
echo ./*
. and ./ both mean current working directory
../ means parent directory of the current working directory
./* means all files and directories in current working directory plus all files and directories in all directories in the current working directory.
./* is all files in current directory
./ is current directory

mv command creates directories [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am facing a very weird issue.
Consider an example I have these directories "/ten" and "/one/two/three/four" I have a few files in these directories.
When i execute the following command
mv /ten/ /one/two/three/four/five/six
it gives the output as
mv: cannot move '/ten/' to '/one/two/three/four/five/six' : No such file or directory. Which looks fine as it doesn't create directories.
But if I execute the following command
mv /one/two/three/four/ /one/two/five/six
the directories five/six get created inside /one/two. i.e. the mv command succeeds.
Can anyone please explain what is happening here ? Why doesn't it give an error No such file or directory ?
EDIT : Further Observation ..
Directories /one/two/three/four exists also directories /one/two/five exists.
Executing mv /one/two/three/four/ /one/two/five/six will succeed. Here directory six will get created even though it is not present.
This doesn't happen in the case when I execute mv /one/two/three/four /one/two/five/six and the "five" directory doesn't exists. In this case it will give error.
I thought mv will never create any directories.
Please let me know if I have missed something obvious.
Either you're executing another mv binary, executing another version of mv, or something is wrapping it up like a function, a script or perhaps an alias.
To know if you're really running the real mv or not, run
type mv
You should get
mv is /bin/mv
As suggested by Etan Reisner, you can also add -a to have more information:
type -a mv
UPDATE
Directories /one/two/three/four exists also directories /one/two/five
exists. Executing mv /one/two/three/four/ /one/two/five/six will
succeed. Here directory six will get created even though it is not
present. This doesn't happen in the case when I execute mv
/one/two/three/four /one/two/five/six and the "five" directory doesn't
exists. In this case it will give error.
Since /one/two/five existed it simply moved your directory /one/two/three/four as /one/two/five/six. That means /one/two/five/six is now the new name or pathname of the directory which was previously /one/two/three/four.
The problem in understanding you are having can be helped with a reference to the man page for mv and a few examples. From man 1 mv Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. What is not apparent is what is SOURCE and what is DEST and this is where your confusion arises. For example:
mv /ten/ /one/two/three/four/five/six
it gives the output as mv: cannot move '/ten/' to '/one/two/three/four/five/six' :
No such file or directory. Which looks fine as it doesn't create directories.
It doesn't. In you example, the SOURCE is /ten and, your DEST depends on whether /one/two/three/four/five exists and also whether /one/two/three/four/five/six exists.
If /one/two/three/four/five exists, then mv /ten /one/two/three/four/five will cause /ten to be moved and become a new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/ten.
If /one/two/three/four/five exists (but not ../six), then mv /ten /one/two/three/four/five/six will cause /ten to be moved and become six new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/six.
if however /one/two/three/four/five do not exist, then mv will fail because you have not provided a valid DEST.

Typed wrong mv command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Hi i worked on Linux server , and was running this command mv matter/*/* .
but instead i have typed this mv matter /*/* .
because of which some errors starts coming on the screen , and then i was not able to login and when we reboot the server its not coming up.
so can you please tell me what this command has done mv matter /*/* .
You can find out for yourself by inserting an echo at the beginning of the command line:
echo mv matter /*/* .
The expanded command looks like this:
mv matter /bin/ash /bin/bash /bin/echo /bin/false [...] /home/yourname [...] .
All files and directories from the top-level directories (echo /*/) have been moved to this one directory where you executed that command. It's hard to separate them from there, but you can try using a rescue CD:
move all executable files to /bin
make /sbin a symlink to /bin
move all files that look like configuration files to /etc
But since you couldn't find out for yourself what the mv command was doing exactly, you should rather ask someone who knows to fix it for you. It's a lot of work, though.

list a linux directory after erasing it [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm wondering if it's possible to list the content of a directory in linux after erasing it and recreating.
Explanation: I'm on a terminal in this particular directory. From another terminal, I erase it and recreate it and put some content inside. If I list this directory from the first terminal, it appears as empty. I need to cd .. and enter inside again to list it's content.
Is there another method who doesn't need to do that?
The only way, as far as I know, is to cd in it again. However, you can do it with one simple command. Select which one you like the most between the following four
cd ${PWD}
cd $PWD
cd $(pwd)
cd `pwd`
You can also add to your ~/.bashrc an alias like this:
alias refresh_dir="cd \$PWD"
and then call the refresh_dir command directly
Would ls ../{directory-name} work? So if the directory was called "test", and you were inside it, you'd use the command ls ../test/.
Nope. You can, of course,
cd "$PWD"
which is a quicker way in a sense.
Some shells might not cache the inode for the current working directory, but I have a sneaking suspicion that POSIX might require shells to. If you think about it, having the shell do this automatically might result in unintended loss of data (because a program/script might go and modify stuff in a directory that wasn't strictly it's working directory to begin with).
Also look at bash PROMPT_COMMAND for a hint on how to automate it, of you find yourself having to type cd "$PWD" more than you like
terminal 1:
mkdir dir/
cd dir/
touch foo/
ls
terminal 2:
rm -r dir/
mkdir dir/
touch dir/bar
terminal 1:
cd `pwd`
ls

Resources