Why does "cd -" behave as it does? - linux

What does cd - exactly do ? ( the change directory command, plus a dash )
I noticed that if I run it in my /home/user folder repeatedly it outputs either /home/user or /home, this changes if I run it from a different folder.

cd -
pop the last directory you were from the stack of directory. It's like hitting "back" on the browser.
Exemple :
you are in /user/alex
you can test that with :
pwd
that give you
/user/alex
then if you do
%cd project1/subfolder
%pwd
/user/alex/project1/subfolder
%cd subsubfolder
%pwd
/user/alex/project1/subfolder/subsubfolder
%cd -
pwd
/user/alex/project1/subfolder
cd -
pwd
/user/alex
NB : it's not going back a level upper in the folder hierarchy. it's going to the previous current folder. ( a level upper is cd .. ).

The syntax
cd -
allows you to switch back to the "last directory you were in when you changed to the current directory". Running the command twice allows you to switch back to the current directory (since the current directory would then become the "last directory you were in when you changed to the current directory").
This is very useful if you are in a very long directory which you don't want to type out over and over, and you go to another lengthy named directory. Instead of retyping it, you can just do a 'cd -', which is similar to how some people use the alt-tab (or command-tab) to switch between applications. This key combo shortcut lets you essentially toggle between the last two applications.

cd stands for Change Directory
It is used to navigate arond your folders
So if you're in /home/user and have a folder in that directory called 'top-secret', you would access it by typing:
cd /top-secret

Related

am in the Desktop but I cd is not see the desktop

How can I fixs that problem meanwhie I fix last code Selenim
Selenium but I am take same error. And cd is not see the Desktop but I am in the Desktop.
Here ~ is equal to /home/levidonates so when you type cd ~/Selenium it's actually cd /home/levidonates/Selenium but the folder Selenium is in your folder Desktop, so you need to type :
cd ~/Desktop/Selenium
~ is a shorthand for your home directory, /home/levidomates. The directories you're accessing are under there, not under the root, /. That is, ~/Desktop is not the same as /Desktop.
Firstly I see which folder do you try to enter / at the beginning? this is not true method.
/ characters means top of all folders root level.
You should not confuse it with the root user's home folder.
cd: change directory
cd .. : Back to the previous folder or up folder.
Firstly You can check your home folder
echo $HOME
then you must be enter pwd
if the your home folder /home/levidomates/
you must only cd ~ command
than cd Desktop
You shouldn't use / at the beginning of folders
Your absolute way to Deskotp is not correct...You forgot /home/levidomates
So then it should be cd /home/levidomates/Desktop instead of cd /Desktop

Go back to current directory from any directory in mac os

I have a terminal open from one of my projects. Let's say at that time I was in /Users/test_user/Desktop/Project/test_project.
Now in that same terminal, I changed the directory to home(~). Now I wanted to go back to the directory from which I opened the terminal initially. Is there a way for me to do that?
You can use cd - to go back to your previous directory which is inside OLDPWD environment variable.
If you want to go, to another directory (which is not the previous directory you are in), there are multiple ways to do it. One way is, you can see the directory stack by the command dirs -v. Sample output wil be like this:
0 ~/workspace/stack/c
1 ~/Downloads
2 ~
Then you can use cd ~$INDEX, here the INDEX being the number shown in the dirs -v output. So cd ~1 will cd into ~/Downloads in the above example.
Another interesting commands to take a look at it are pushd and popd.

How to go to another directory sharing same parent directory?

I am beginner in linux and wondering about a shorter way to go a directory having same parent directory. Here I elaborate that.
dir1
- dir11
- dir12
- dir13
- dir14
Directory dir1 has sub-directories dir11,...,dir14. I am at directory dir13 and want to go to dir12. What is direct way to do this?
I can do
cd..
cd dir12/
But I am wondering whether I can do this in single step. Any ideas?
cd doesn't only take one directory to change to. You can provide a complete path to which you want to go.
You can simply type cd ../dir12
You can also go all nuts with cd ../dir12/../dir13/../dir14/..
Try also the following: type cd ..<press Tag twice>. This will list you all directories in the path you have given.
There are wildcards that cd can use. For example ~ is your home directory. cd ~ or just cd will change into your $HOME.
See man cd (in a terminal) for more.

A term for a config dir that starts with a dot under home dir?

It is a common practice to create a local dir for your application on the user's machine under their $HOME, i.e. ~/.myapp/ and then put some configs inside. So the question is: is there a term for such a "dot-started config-dir under user's home"?
No, the most recommendable is make the folder for example with
1 ) mkdir -p $HOME/.config/myapp
2 ) mkdir -p ~/.config/myapp
That is the default path for storage configs, but some developers just use ~/.myapp or $HOME/.myapp
If you wanna see the system variables (Under GNU/Linux) for example, open a terminal , and write $ when appears, press tab twice

How do I change the directory in Git Bash with Git for Windows?

How would I change to the directory C:/Users/myname/project name in Git Bash?
cd /c/users/myname/project\ name
Beware that ls /, or typing cd / followed by Tab-completion, might not show the existence of this folder, but cd /c will still work. Also note that pwd (to print the current working directory) might show something like /bin, but this might not be the actual working folder for commands such as git clone, which might use the folder from which Git Bash was started.
If the you know how many levels up from your current working directory, you could use cd ../project/name to avoid writing the entire directory path.
The .. represents moving 1 directory up.
You will need to use quotes in your directory name, or the short version of the filename.
You can find the short version of the file name by issuing the command:
dir /x
If I remember correctly. I do not have a windows machine.
It is a version of bash shell though, so you should be able to simply quote it. (And the dir /x may or may not work.)
If you are at the a directory and wanna switch to sub directory use :
cd "project name"
If you wanna go to a different path use the whole path :
cd "C:/Users/myname/project name"
But you can avoid use white spaces in project files and folders and instead use underscore
An alternative that worked for me (Windows 10 x64) is putting the full address in quotes:
cd "D:\BWayne\Documents\- School\Developer\-- Backend\Test for GitBash"
I could then do like mkdir, touch, etc and it successfully put them in the Test for GitBash folder.

Resources