bash: is there any kind of buffer for last visited paths? - linux

look at this steps:
:~$ cd programacion/
:~/programacion$ cd sports/
:~/programacion/sports$ cd src/
:~/programacion/sports/src$ cd Sports/
:~/programacion/sports/src/Sports$ cd PlatformBundle/
:~/programacion/sports/src/Sports/PlatformBundle$ cd Entity/
:~/programacion/sports/src/Sports/PlatformBundle/Entity$ cd /
:/$
being in this moment in the last prompt, is there any way to go directly to programacion/sports/src/Sports/PlatformBundle/Entity since it is the last path i have visited?

You can use the following shortcut to the last directory:
cd -
For example:
kbrandt#alpine:~$ pwd
/home/kbrandt
kbrandt#alpine:~$ cd src
kbrandt#alpine:~/src$ cd -
/home/kbrandt
kbrandt#alpine:~$ cd -
/home/kbrandt/src
kbrandt#alpine:~/src$
Another option would be to use the $OLDPWD shell variable.

bash's pushd and popd do what you want, and more.
$ help pushd
$ help popd

You can also download the Teleport script, analogous to bash history, but for directories.

Related

Directory structure variations in bash script

I have a shell script myautoappupgrade.sh where I automate a process of application upgrade. The script has to be run on few different servers. Unfortunately, the application is located in slightly different directory on each server - the number for parent directory varies between 1-20. How I can modify the script, so that the directory can be replaced by some sort of variable? I don't want to edit the script for each server because there are many directory queries in the automation script.
example:
cd /ae1/apps/myapp/upgradefiles/
unzip file.zip
./install.sh
the directory slightly changes on another server:
cd /ae2/apps/myapp/upgradefiles/
unzip file.zip
./install.sh
and another..
cd /ae3/apps/myapp/upgradefiles/
unzip file.zip
./install.sh
Try something like this:
#!/bin/bash
num=$1
cd /ae${num}/apps/myapp/upgradefiles/file.zip
unzip file.zip
./install.sh
Then call the script with the number as first argument:
myautoappupgrade.sh 1
The simple and obvious solution is to not hard-code the directory at all. Modify the script so it accepts the parent directory as an argument, or just cd into the parent directory before running the script.
Perhaps something like this:
while read server dir; do
ssh "$server" "cd '$dir' && unzip apps/myapp/upgradefiles/file.zip/file.zip && ./install.sh"
done <<\:
ernie /ae1
bert /ae2
cookiemonster /home/cmonster/anN
:
It would probably be even better if you unzipped into a temporary directory, but hopefully this should get you moving in the right direction.
Of course, if you can be sure that /ae[0-1] is always there and there is only one match,
cd /ae[0-9]/apps/myapp/upgradefiles/file.zip
would do what you are asking.
(Do you really have a file named file.zip inside a directory also named file.zip? I'm guessing actually take away the file.zip from the end of the cd path.)
By simply using:
cd /ae*/apps/myapp/upgradefiles/
The * will expand any character.

In MacOS terminal, is it possible to go back to the previous directory (Multiple times). (e.g. like cd - but going back multiple times)

I know I can use cd - to go back to the previous location. Is there any way to do this multiple times?
If I try this again it toggles back to the next location - useful, but would be great if I could also keep using a command to move back (not up) the directories I have been in.
You can use pushd and popd commands to maintain a stack of visited directories. Example:
[~]$ mkdir -p x/y/z
[~]$ pushd x
~/x ~
[~/x]$ pushd y
~/x/y ~/x ~
[~/x/y]$ pushd z
~/x/y/z ~/x/y ~/x ~
[~/x/y/z]$ popd
~/x/y ~/x ~
[~/x/y]$ popd
~/x ~
[~/x]$ popd
~
[~]$
Before issuing a cd, if you enter into a subshell by issuing sh, bash or whatever, and then run cd, when you will exit from this subshell, you will return to the directory where you were before, and this recursively.

"No such file or directory" while in link path

When compiling, I always place the build in a separate directory. For example:
mkdir build
cd ./build
(cd ..; ./bootstrap)
../configure
make
Since I have plenty of RAM the aim is to compile on a TMPFS.
The script gets the name of the project, uses it for the name for the directory created in $XDG_RUNTIME_DIR/build and finally links it.
# setup-build.sh
#!/usr/bin/bash
set -e
my_project_name=$(basename $(pwd))
my_project_build_dir="$XDG_RUNTIME_DIR/build/$my_project_name"
mkdir -p $my_project_build_dir
ln -s "$my_project_build_dir" "$(pwd)/build"
The script runs without a problem. But, when I do cd ./build; ../configure it returns an error: bash: ../configure: No such file or directory. The file most certainly does exist, but Bash can't find it!
I altered the script to this:
#!/usr/bin/bash
set -e
my_project_src_dir="$(pwd)"
my_project_name="$(basename $(pwd))"
my_project_build_dir="$XDG_RUNTIME_DIR/build/$my_project_name"
mkdir -p "$my_project_build_dir"
ln -s "$my_project_build_dir" "$(pwd)/build"
cd "$my_project_build_dir"
echo "$my_project_src_dir" > "./project-src-dir.txt"
To compile I have to type cd ./build; $(cat ./project-src-dir.txt)/configure; make. This causes Bash complete to partial break, though. As in I can't TAB complete file names from $my_project_src_dir with this method, but TAB completion for arguments works fine. Ifautoconf is needed: (cd $(cat ./project-src-dir.txt); ./bootstrap). If anyone has any other ideas I would still prefer to be able to just do ../configure, although this will have to do for now.
Edit: Had to change my_project_name="$(basename '$my_project_src_dir') to my_project_name="$(basename $(pwd))" as it was taking '$my_project_src_dir' literally.

How to run a Linux shell command from a different directory without getting there?

How to run a Linux shell command from a different directory without actually getting there?
In the following, I want to run a make command, but without getting into the source code directory, i.e., from my home directory:
me#mypc:~$ ~/my/source/code/directory/make #This is wrong!
I have seen some examples which suggest as:
me#mypc:~$ cd ~/my/source/code/directory; make
But this ends up taking me into that source code directory, which I want to avoid.
There could the be the other option:
me#mypc:~$ cd ~/my/source/code/directory; make; cd ~
But it becomes complicated in cese.
I am wondering if there could be some way nicer and simpler than these?
You can try:
me#mypc:~$ (cd ~/my/source/code/directory; make)
Parentheses tell your shell to spawn a separate subshell, with its own current directory, and run the commands inside that subshell. After the command finishes, the subshell is closed, and you're back to your main shell, whose current directory never changed.
Do it in a subshell, e.g.
(cd ~/my/source/code/directory; make)
Alternately, you can use make's -C option, e.g.
make -C ~/my/source/code/directory
You can also use
pushd ~/my/source/code/directory; make; popd
or
current=`pwd`; cd ~/my/source/code/directory; make; cd "$current"
$ (cd directory && make)
You need to use && instead of ;. Consider something like
cd junk && rm -rf *
With &&, bash will abort if the directory junk does not exist. If you try cd junk; rm -rf * and junk doesn’t exist, you’ll delete everything in the current directory :(

What does 'cd -' stand for?

In a bash shell script today I noticed the below command at the end of the script. I know what is cd but I am unaware of the significance of a dash after it.
cd -
What does this mean? Google naively truncates the - so I am unable to find its answer.
If a single dash is specified as the argument, it will be replaced by the value of OLDPWD.
The OLDPWD is set by cd command and it is the previous working directory.
cd - returns to the directory you were previously.
For instance:
marcelo#marcelo:~$ cd /opt
marcelo#marcelo:/opt$ cd /usr/bin
marcelo#marcelo:/usr/bin$ cd -
/opt
marcelo#marcelo:/opt$
I was in /opt, changed to /usr/bin, and then went back to /opt with cd -
cd - brings you back to the last directory.
$ cd ~/Desktop
$ pwd
/Users/daknok/Desktop
$ cd /
$ pwd
/
$ cd -
$ pwd
/Users/daknok/Desktop
cd - returns to the previous directory you were in.
Say I'm in /usr/ and I type cd /var/local/someplace/else
Then I use cd - I'll return to /usr
From the manual
An argument of - is equivalent to $OLDPWD. If a non-empty directory
name from CDPATH is used, or if - is the first argument, and the
directory change is successful, the absolute pathname of the new
working directory is written to the standard output. The return
value is true if the directory was successfully changed; false
otherwise
Therefore the - is equivalent to the $OLDPWD, which holds the last directory which the shell was in, and is set by the previous cd invocation.
From the man found here : http://ss64.com/bash/cd.html
Quickly get back
$ cd -
cd - bring you back to the last directory you were.
e.g.
cd ~/Documents
cd ~
cd /
Now you are in '/', and if you run 'cd -' you will be in '~'. BTW, run 'cd -' once again, you will return to '/' but not '~/Documents'
“ Current Directory “ Is what the bash cd terminal command means. It means “ keep me in this directory “

Resources