Posted: . At: 9:25 PM. This was 11 years ago. Post ID: 5924
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

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

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

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

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

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

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^^}
/HOME/HOMER/DOCUMENTS

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. http://www.atomicmpc.com.au
{
	DEFAULT=$(tput sgr0);
	FILELIST=.badlink.list
 
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
 
	function checklink()
	{
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
		done
	}
 
	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
		fi
	done
 
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
			-removed"
			echo
		done
		rm -fr $FILELIST
	else
		printf "Bad symlinks not found.\n\n"
	fi
} # 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}";
	fi
	unset WHOAMI _DISPLAY
fi

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.