How to find out the path that will be used in `cd -`? - bash

I know that it is possible to use cd - to switch between 2 paths:
user#server:~$ cd a
user#server:~/a$ cd ~/b
user#server:~/b$ cd -
/home/user/a
user#server:~/a$ cd -
/home/user/b
user#server:~/b$
I want to use this feature to do something with the previous path. Maybe there is some variable that points to the previous path so I can do thins like:
user#server:~/a$ cd ~/b
user#server:~/a$ ls -d $PREVIOUS_PATH
/home/user/a
user#server:~/a$ cp file $PREVIOUS_PATH # will copy file to /home/user/a
user#server:~/b$ cd -

Old working directory is stored in OLDPWD environment variable. This variable is updated every time we change directory. This also means that it is not set when we launch terminal.
user#server:~/a$ cd ~/b
user#server:~/a$ ls -d "$OLDPWD"
/home/user/a
user#server:~/a$ cp file "$OLDPWD" # will copy file to /home/user/a, ""
user#server:~/b$ cd -

Related

-bash: ls: command not found on macOS Big Sur 11.0.1

Did I touch something wrong on the terminal?
I'll tell you the details.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
export PATH="$HOME/.rbenv/shims:~/.rbenv/bin"
And from here, the 'ls' didn't work in Bash.
Can I know the solution to the problem?
Please! I'm panicking.
I entered the command below because I was embarrassed, but I hope it will be a reference...
cd sws
cd ..
ls
cd ..
ls
cd ..
pwd
ls
pwd
cd home
ls
pwd
cd sws
cd user
ls
ls -al
cd ..
pwd
cd ..
ls
pwd
cd ~/.rbenv && src/configure && make -C src
ls
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
ls
echo 'export PATH="$HOME/.rbenv/bin:~/.rbenv/bin"' >> ~/.bash_profile
ls
What I've been following:
https://ernestojeh.com/fix-jekyll-on-macos-big-sur
https://github.com/rbenv/rbenv#basic-github-checkout

How do I use cp -r to overwrite existing files on Windows Subsystem for Linux (Ubuntu)

Running the following commands in Ubuntu WSL leads to strange behavior - the file text.txt within the dst folder is not overwritten even though it should be.
mkdir temp
mkdir temp/src
mkdir temp/dst
echo "src" > ./temp/src/text.txt
echo "dst" > ./temp/dst/text.txt
cp -r ./temp/src/ ./temp/dst/
cat ./temp/dst/text.txt
Output
dst
The output SHOULD be src.
I've also tried the below:
sudo cp -Rfv ./temp/src/ ./temp/dst/
cat ./temp/dst/text.txt
Output
'./temp/src/text.txt' -> './temp/dst/src/text.txt'
dst
Directory structure:
--temp
--src
--text.txt
--dst
--text.txt
After cp -r ./temp/src/ ./temp/dst/
Directory structure becomeļ¼š
--temp
--src
--text.txt
--dst
--text.txt
--src
--text.txt
so cat ./temp/dst/text.txt
ouput: dst

Linux symbolic links how to make target file in currently directory an automatic absolute path?

Here is the story:
cd ~
mkdir bin
export PATH=$PATH:bin
mkdir -p projects
cd projects
echo 'hello world' > hello.sh
chmod +x hello.sh
ln -s hello.sh ~/bin/hello
hello
output:
-bash: hello: command not found
How I changed it:
ln -s hello.sh ~/bin
hello.sh
The output is more weird:
-bash: /home/qht/bin/hello.sh: Too many levels of symbolic links
I ls it to see what happened:
ls -l ~/bin/hello.sh
/home/qht/bin/hello.sh -> hello.sh
I figure it out, hello.sh reference itself. And hello before reference hello.sh which doesn't exist.
I fix it by:
ln -sf $PWD/hello.sh ~/bin/hello
ls ~/bin/hello
/home/qht/bin/hello -> /home/qht/projects/hello.sh
and it works, I also man ln to see if there is a convenient option to do that, this is what I found:
ln -sfr hello.sh ~/bin/hello
ls -l ~/bin/hello
/home/qht/bin/hello -> ../projects/hello.sh
And it works, the -r option did the work.
But I'm curious, if ln -r can automatically write the relative path data into symbolic links, Why doesn't there an option maybe -a to do the absolute path work.
Or, is relative path for links is more practical than absolute path?
Try this:
cd ~
mkdir bin
export PATH=$PATH:~/bin # Need absolute path to bin
mkdir -p projects
cd projects
echo 'echo "hello world"' > hello.sh # If the script is just hello world
# this will become an infinite loop
chmod +x hello.sh
ln -s "$PWD/hello.sh" ~/bin/hello # the symbolic link in this case
# needs to be a absolute path
hello

mkdir always creates a file instead a directory

First I want to say that I don't really know what I should look for, here in Stack Overflow and what could be a good query for my problem.
In simple words I want to create a new directory and than do some file operations in it. But with the script that I have crafted I got always a file instead of a directory. It seems to be absolutely regardless how I stick the code together there is always the same result. I hope tat masses can help me with their knowledge.
Here is the script:
#!/bin/bash
DLURL=http://drubuntu.googlecode.com/git'
d7dir=/var/www/d7/'
dfsettings=/var/www/d7/sites/default/default.settings.php
settings=/var/www/d7/sites/default/settings.php
#settiing up drush
drush -y dl drush --destination=/usr/share;
#Download and set up drupal
cd /var/www/;
drush -y dl drupal;
mkdir "$d7dir"; #this is the line that always produces a file instead a directory
# regardless if it is replaced by the variable or entered as
# /var/www/d7
cd /var/www/drup*;
cp .htaccess .gitignore "$d7dir";
cp -r * "$d7dir";
cd "$d7dir";
rm -r /var/www/drup*;
mkdir "$d7dir"sites/default/files;
chmod 777 "$d7dir"sites/default/files;
cp "$dfsettings" "$settings";
chmod 777 "$settings";
chown $username:www-data /var/www/d7/.htaccess;
wget -O $d7dir"setupsite $DLURL/scripts/setupsite.sh; > /dev/null 2>&1
chmod +x /var/www/setupsite;
echo "Login Details following...";
read -sn 1 -p "Press any key to continue...";
bash "$d7dir"setupsite;
chown -Rh $username:www-data /var/www;
chmod 644 $d7dir".htaccess;
chmod 644"$settings";
chmod 644"$dfsettings";
exit
I hope someone got the reason for that.
There are many way to debug a shell-scripting.
Add set -x in your beginning script
Get the return value.
mkdir 'the-directory'
ret=$?
if test $ret -eq 0; then
echo 'Create success.'
else
echo 'Failed to create.'
fi
Set to verbose mode $ mkdir -v 'the-directory'
Try this command $ type mkdir, to checking mkdir command.

Downloading and automatically installing a tgz file

#!/bin/bash
mkdir /tmp
curl -O http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz /tmp/mucommander.tgz
tar -xvzf /tmp/mucommander.tgz */mucommander.app/*
cp -r /tmp/mucommander.app /Applications
rm -r /tmp
I'm trying to create a shell script to download and extract muCommander to my applications directory on a Mac.
I tried cd into the tmp dir, but then the script stops when I do that.
I can extract all using the -C argument, but the current tgz path is muCommander-0_9_0/mucommander.app, which could change on later builds, so I'm trying to keep it generic.
Can anyone give me pointers where I'm going wrong?
Thanks in advance.
Strip the first path component when you untar the archive, from tar(1):
--strip-components count
(x mode only) Remove the specified number of leading path ele-
ments. Pathnames with fewer elements will be silently skipped.
Note that the pathname is edited after checking inclusion/exclu-
sion patterns but before security checks.
Update
Here is a working bash example of how to, fairly generically, copy the contents of the tgz file to /Applications.
shopt -s nocaseglob
TMPDIR=/tmp
APP=mucommander
TMPAPPDIR=$TMPDIR/$APP
mkdir -p $TMPAPPDIR
curl -o $TMPDIR/$APP.tgz http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz
tar --strip-components=1 -xvzf $APP.tgz -C $TMPAPPDIR
mv $TMPAPPDIR/${APP}* /Applications
# rm -rf $TMPAPPDIR $TMPDIR/$APP
The rm command is commented out for now, verify that it does no harm before you use it.
The following will update your muCommander.
#for the safety, remove old temporary extraction from the /tmp
rm -rf /tmp/muCommander.app
#kill the running mucommander - you dont want replace the runnung app
ps -ef | grep ' /Applications/muCommander.app/' | grep -v grep | awk '{print $2}' | xargs kill
#download, extract, remove old, move new, open
#each command run only when the previous ended with success
curl http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz |\
tar -xzf - -C /tmp --strip-components=1 '*/muCommander.app' && \
rm -rf /Applications/muCommander.app && \
mv /tmp/muCommander.app /Applications && \
open /Applications/muCommander.app
Beware, after the '\' must following new line, and not any spaces...

Resources