Linux/Unix shell tricks, you might not know...
The Unix shell is powerful. By writing shell scripts you can create a little toolbox - or even really advanced utilities to accomplish complex tasks. Anyway, here are some tricks, you might not know…
Go back to last directory (cd -
)
To change a directory, cd
is used. Its probably one of first commands you learned. But what about going back to the last directory, you were in? Well, there is a neat trick to do that:
cd -
You can use this in scripts to go into a directory and switch back without storing a variable with the current path:
cd /tmp && ./run-script.sh && cd -
But wait… what if a directory is called -
, how can I change into it? Here’s the answer:
cd ./-
Shortcut for repeating the last command (!!
)
!!
is substituted with the last command, which is very useful under specific circumstances. Here is an example:
ls /root
# permission denied
sudo !!
# sudo ls /root
# files in root are listed
Shortcut for repeating the last argument (!$
)
!$
is substituted with the last parameter, which is very useful under specific circumstances. Here is an example:
ls /tmp
# files in tmp
cd !$
# cd /tmp
Watching a commands output interval wise (watch <command>
)
Often you would like to watch
the output of a command lets say every 2 seconds. Just install the tool watch
and
use it…
# watch the output of ls /tmp every 2 seconds
watch ls /tmp
Position cursor at the beginning (CTRL+A
) or end (CTRL+E
)
Sometimes you might have forgotten a sudo
before your command and thankfully you noticed it before execution. Use CTRL+A
, type sudo
then CTRL+E
and go on with your command.
CTRL+A
: Go to the beginning of the lineCTRL+E
: Go to the end of the line
For more shortcuts, see the shell shortcuts cheatsheet
Aliases and functions
You can use aliases and functions, to make your life easier. Here are some examples to get the idea:
cd..="cd .."
grep='grep --color=auto'
egrep='egrep --color=auto'
fgrep='fgrep --color=auto'
l='ls -CF'
la='ls -A'
ll='ls -alF'
ls='ls --color=auto'
mkdir='mkdir -p'
open=xdg-open
vi=vim
mkcd() { mkdir -p "$@" && cd "$1"; }
..
helper
Often you have to type cd ..
a lot of times or maybe cd ../../../
- with shopt -s autocd
you can leave out the cd
. Maybe this little helper function is useful for you…
function ..() {
for i in $(seq 1 $1); do cd ..; done
}
# cd ..
..
# cd ../../../
.. 3
Directory bookmarks
Using functions and variables you can create a little bookmark system for your shell. Heres a little script with explanations:
# choose a directory where bookmarks are stored
FAV_DIR="$HOME/.fav"
# create the directory, if it does not exist
test -d "$FAV_DIR" || mkdir -p "$FAV_DIR"
# make cd lookup bookmark directory by default
export CDPATH=".:$FAV_DIR"
## create a bookmark name for current directory (e.g. favmk @dotfiles)
function favmk {
mkdir -p "$FAV_DIR";
[ -d "${FAV_DIR}" ] && (ln -s "$(pwd)" "$FAV_DIR/$1") || (echo "fav directory ${FAV_DIR} could not be created")
}
## remove a bookmark (e.g. favrm @dotfiles)
function favrm {
rm -i "$FAV_DIR/$1"
}
## goto bookmark or list existing bookmarks (e.g. fav or fav @dotfiles)
function fav {
if [ ! -z "$1" ]; then
[ -e "$FAV_DIR/$1" ] && cd -P "$FAV_DIR/$1" && return 0
echo "No such fav: $1"
echo "Would you like to create one? [y/N]"
read RESPONSE
if [ "$RESPONSE" = "y" ]; then
favmk "$1"
fi
fi
ls -l "$FAV_DIR" | sed 's/ / /g' | cut -d' ' -f9-
}
If you use @
as a prefix, it will become very handy:
cd ~/dotfiles
favmk @dotfiles
fav
# @dotfiles -> /home/username/dotfiles
cd /tmp
fav @dotfiles
favrm @dotfiles