More awesome shell tricks for the Linux command line. This is using the bash shell.

Posted: July 3, 2013. At: 9:25 PM. This was 4 years ago. Post ID: 5924

This is another way to see where you are in the directory stack.

homer@deep-thought ~/Documents/basenew $ echo $DIRSTACK

Here is another way to show the $PWD value one directory down from where you are now.

homer@deep-thought ~/Documents/basenew $ echo $OLDPWD

This example shows how to print a $PWD value in uppercase. If you pine for the old ways of DOS.

homer@deep-thought ~/Documents/basenew $ echo ${OLDPWD^^}

Here is how to change your shell prompt to better suit your workflow.

homer@deep-thought ~/Documents/basenew $ export PS1="\t \u@\h \$ "
21:04:03 homer@deep-thought $

This far more comprehensive bash shell prompt is another good one to have. This shows the RAM free as well as other cool things.

PROMPT_COMMAND='history -a;echo -en "\033[m\033[38;5;2m"$(( `sed -nu "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo`/1024))"\033[38;5;22m/"$((`sed -nu "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo`/1024 ))MB"\t\033[m\033[38;5;55m$(< /proc/loadavg)\033[m"'
 PS1='\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;32m\]\w\[\e[0;34m\] \n($SHLVL:\!)\e[0m\]\$ '

This function will scan a folder for any broken symbolic links. Put this code into your .bashrc and then type badlink. This is good for cleaning up a directory.

function badlink()
# From Atomic magazine #43 August 2004.
	DEFAULT=$(tput sgr0);
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
	function checklink()
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
		rm -fr $FILELIST
		printf "Bad symlinks not found.\n\n"
} # End Atomic function.

This is the output you get after running this function.

[18411:18406 0:5] 09:16:21 Wed Jul 03 [homer@deep-thought: +1] ~/Desktop 
(1:5)$ badlink 
"/home/homer/Desktop/home.desktop" 			-removed
"/home/homer/Desktop/root.desktop" 			-removed

Finally; this is some more code to put into your .bashrc file to set the value of the $DISPLAY variable.

# try to set DISPLAY smart (from Hans) :)
# From a SUSE .bashrc file.
if test -z "$DISPLAY" -a "$TERM" = "xterm" -a -x /usr/bin/who ; then
	WHOAMI="`/usr/bin/who am i`"
	_DISPLAY="`expr "$WHOAMI" : '.*(\([^\.][^\.]*\).*)'`:0.0"
	if [ "${_DISPLAY}" != ":0:0.0" -a "${_DISPLAY}" != " :0.0" \
		-a "${_DISPLAY}" != ":0.0" ]; then
		export DISPLAY="${_DISPLAY}";

No comments have been made. Use this form to start the conversation :)

Leave a Reply