How can I return to the previous working directory quickly in Bash? - bash

When I change into a directory with the cd command, I lose the previous working directory, unless I remember it in my memory. Is there some handy method to go back quickly?
Demo:
$ cd ~/some_path
$ cd /another_path
$ command_to_go_back_to_some_path

You can go back to the last dir with cd -

You can also do this
$ pushd ~/some_path
$ pushd /another_path
$ popd
$ popd

As mentioned you can use cd -. The shell internally does a cd $OLDPWD.

For usage in a script, you could use the OLDPWD shell variable: it contains the previous working directory.
$ pwd
/home/username
$ cd /usr/bin
$ pwd
/usr/bin
$ cd "$OLDPWD"
$ pwd
/home/username
I prefer this over cd - in scripts because I don't have to suppress any output.

If you want to use it in a script and suppress the output, do this:
cd - > /dev/null

Related

bash scripting - what does "ls .." (ls with double dot) do?

My homework asked me to find what ls .. does.
I tried to search the internet but I couldn't find any answer to the question.
Does someone know what this command does?
.. is an entry in the current directory that refers to the parent of the current directory. It's not a special convention used just by ls. For example:
$ cd /usr/bin
$ pwd
/usr/bin
$ cd ..
$ pwd
/usr
$ [ -d .. ] && echo "It's a directory"
It's a directory
$ stat ..
<output specific to your installed version of stat>

`cat ../test` does not find a file. How to force `cat` to not resolve symlink?

I create this file as:
$ echo "ABC" > /home/kes/test
I can cd to home and out of it:
kes#work ~/s $ cd ..
kes#work ~ $ cat test
ABC
kes#work ~ $ cd s
kes#work ~/s $
~/s is soft link:
kes#work ~/s $ pwd
/home/kes/s
kes#work ~/s $ pwd -P
/home/kes/work/projects/safevpn/repo2
But when I use relative path it does not work:
kes#work ~/s $ cat ../test
cat: ../test: No such file or directory
But while
I do not expect that cat try to open file in parent directory (resolves symlink) because cd does not resolve it:
$ rm /home/kes/test
$ mkdir /home/kes/test
$ cd ~/s
$ cd ../test
$ pwd
/home/kes/test
How to make these commands work consistent?
If you read the man page of cd, you will see that cd can or cannot resolve the soft-links, depends on which option do you give (-L or -P). If no option was given, cd uses default -L, which means, not resolving soft links.
If both −L and −P options are specified, the last of these options
shall be used
and all others ignored. If neither −L nor −P is specified, the operand shall be
handled dot-dot logically;
And
−L Handle the operand dot-dot logically; symbolic link
components shall
not be resolved before dot-dot components are processed (see steps 8.
and 9. in the DESCRIPTION).

Uninstalled Anaconda still shows up in PATH (Mac OS X)

I have installed Anaconda a few months ago but then uninstalled it and removed all anaconda files by using
rm -rf ~/anaconda
but when I run
echo $PATH
it still outputs a path that point to an Anaconda folder but when I search for it, it doesn't even exist, why is that happening?
What makes you think that non-existent directory are automatically
removed from $PATH? They are not. As an example I can make a new dir
and go there:
$ mkdir /tmp/new-path-dir && cd /tmp/new-path-dir
Add it to the $PATH:
$ PATH=/tmp/new-path-dir:$PATH
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
Make a new olleh.so (hello spelled backwards) executable inside
it:
$ echo 'echo hi' > olleh.so && chmod +x olleh.so
Then go back to ~:
$ cd ~
And start a olleh.so:
$ olleh.so
hi
Now I can safely remove /tmp/new-path-dir:
$ rm -r /tmp/new-path-dir/
And it still will be shown in my $PATH:
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
But I won't be able to run olleh.so any more:
$ olleh.so
bash: /tmp/new-path-dir/olleh.so: No such file or directory
And as paths to executables are cached by bash I can get rid of
olleh.so permanently like this:
$ hash -r
$ olleh.so
bash: olleh.so: command not found

Bash script get user and change dir

How can I combine these two examples with pushd and whoami to change the directory?
I know I can change the directory like this:
#!/bin/bash
pushd /home/mike/Pictures > /dev/null
# do something in the new dir
ls
popd > /dev/null
And I know I can get the username like this:
#!/bin/bash
theuser=`whoami`
echo $theuser
You're waaay overthinking it...
cd ~/Pictures
EDIT:
Actually, no. What you really want is:
cd "$(xdg-user-dir PICTURES)"
Those backticks can be used to interpolate the output of the command they contain into another:
pushd /home/`whoami`/Pictures
Much easier than using pushd and popd, run the command in a sub-shell:
(
cd /home/$(whoami)/Pictures &&
ls
)
The sub-shell changes directory, without affecting the main process - exactly as you wanted, but rather more reliably.
It's easier just to use cd to change the directory:
#!/bin/bash
cd ~/Pictures
Among other solutions:
pushd "$HOME/Pictures"
After all, nothing obliges the home directory to bear the user name!
Bash already has the $USER variable, no need to call an external binary
pushd /home/$USER/Pictures > /dev/null

Bash: in directory `directory_name.git`, how to cd to `../directory_name`?

I'm pretty new to Bash scripting and am looking to do the following:
The script's pwd is "/a/b/c/directory_name.git/" and I'd like to cd to "../directory_name" where directory_name could be anything. Is there any easy way to do this?
I'm guessing I'd have to put the result of pwd in a variable and erase the last 4 characters.
tmpd=${PWD##*/}
cd ../${tmpd%.*}
or perhaps more simply
cd ${PWD%.*}
Test
$ myPWD="/a/b/c/directory_name.git"
$ tmpd=${myPWD##*/}
$ echo "cd ../${tmpd%.*}"
cd ../directory_name
*Note: $PWD does not include a trailing slash so the ${param##word} expansion will work just fine.
Try:
cd `pwd | sed -e s/\.git$//`
The backticks execute the command inside, and use the output of that command as a command line argument to cd.
To debug pipelines like this, it's useful to use echo:
echo `pwd | sed -e s/\.git$//`
This should work:
cd "${PWD%.*}"
Didn't expect to get so many answers so fast so I had time to come up with my own inelegant solution:
#!/bin/bash
PWD=`pwd`
LEN=${#PWD}
END_POSITION=LEN-4
WORKING_COPY=${PWD:0:END_POSITION}
echo $WORKING_COPY
cd $WORKING_COPY
There's probably one above that's much more elegant :)
That's what basename is for:
cd ../$(basename "$(pwd)" .git)

Resources