Some Ubuntu and Linux Mint tips and tricks.

Posted: January 20, 2012. At: 9:49 PM. This was 6 years ago. Post ID: 2491

Apt errors.

To deal with the message you get sometimes telling you that some packages have been kept back when you type sudo apt-get upgrade, just type sudo apt-get dist-upgrade. This will pull down the rest of the packages and fully upgrade your system.

Uncompressing rar files.

This is very easy with the unrar command. Just type unrar x myfile.rar and it will unpack the files therein to the current folder.

[email protected]:~/Downloads$ unrar x Monster.rar 

UNRAR 4.00 beta 3 freeware      Copyright (c) 1993-2010 Alexander Roshal


Extracting from Monster.rar

Extracting  Monster.wad                                               OK 
All OK

Adding all files in current folder to a zip file.

This command is using a wildcard to include all files in the folder into the zipfile.

[email protected]:~/Documents/java$ zip -9 java.zip *
  adding: a.out (deflated 72%)
  adding: Lincoln.java (deflated 31%)
  adding: my.java~ (deflated 29%)
[email protected]:~/Documents/java$ ls
a.out  java.zip  Lincoln.java  my.java~

Mounting an improperly shut down Windows XP installation with Knoppix 6.4 Linux.

I recently found an old hard disk with a Windows XP installation on it and I was trying to mount it with the GUI file manager, but I kept getting errors that the disk had a hibernation file on it and I could not mount the drive. I found out that I had to use this command to mount the disk.

[email protected]:/home/knoppix# mount -t ntfs-3g -o remove_hiberfile /dev/sdd1 /media/sdd1

Reading just the start or ending of a file.

When you just want to see the start or ending of a file this command will help.

head -n 40 data.txt

This will output the first 40 lines of the file.

tail -n 40 data.txt

will output the last 40 lines.

Using strings to search elf binary executables.

Using strings is very easy, you can just use it with grep(1) to search through binaries and other non-text files to find various strings. This is useful for finding illegal content on a hard drive partition or searching through an executable file to find text strings.

strings -a ./doom | grep "final"

The above command searching a linuxxdoom binary gives us this output. It has found all instances of the word final in the linuxxdoom executable. See how useful this is? Try it yourself! Try searching MP3 files for the ID3 tag data.

Now, it's on to the final chapter of
final confrontation with that terrible
final cleaning-up ...
level 24: the final frontier

Using this command on the pagefile.sys file that Windows uses, you can find all sorts of things that the user has been accessing on the Internet and find E-Mail addresses and URL strings that will be recorded in this file. This is a good indication that if you are using Windows, you should try and set up your machine so that the pagefile is erased when you shut down. If you find an old junk computer laying around, you can use this command to search the pagefile.sys on the hard drive and find out what the previous owner was up to.

Another use of strings and grep.

[email protected] Elements $ strings linuxmint-12-gnome-dvd-64bit.iso | grep Ubuntu
3.0.0-12-generic ([email protected]) #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011
# Install the Ubuntu desktop and LTSP server.
# Install the Ubuntu desktop.
[email protected] Elements $

Some useful bash aliases.

# My .bash_aliases file. You need this to go with the
# .bashrc below.

alias cls='clear'
alias tarunpack='tar -zxvf'
alias bz2unpack='tar -jxvf'
alias me='who am i'
alias verbosetree='tree -A -s -p -f --dirsfirst'
alias psverbose='ps u a x f g'
alias dsize="du -ack | sort -nr | head -n 20"
alias psview="pstree -a -u -G -l -h -p"

And my .bashrc file. This provides tab completion of the command line for ./configure, so this is perfect for those of you who are wanting to compile a lot of software from source.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# User specific aliases and functions

PS1="\u@\h \W \$ "

unset HISTFILE
OSRELEASE=`uname -r`;
DOOMWADDIR=/usr/local/share/games/doom
PATH=$PATH:~/bin:/usr/games:/usr/local/games
LANG=en_US

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

export EDITOR=mcedit
export TIME_STYLE=+" %d-%m-%y %I:%M %P"
# These two examples from bash-doc.
export PAGER="/usr/bin/less"
export VIEWER="mcedit"
export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
shopt -s checkwinsize

[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"

alias verbosetree='tree -A -s -p -f --dirsfirst'
alias psverbose='ps u a x f g'
alias dsize="du -ack | sort -nr | head -n 20"
alias psview="pstree -a -u -G -l -h -p"
alias ls="ls -hula --color=yes"

# These following functions from
# http://www.die.net/doc/linux/abs-guide/sample-bashrc.html
function my_ip() # get IP adresses. Bracket on next line C style...
{
    MY_IP=$(/sbin/ifconfig wlan0 | awk '/inet/ { print $2 } ' | sed -e s/addr://)
    MY_ISP=$(/sbin/ifconfig wlan0 | awk '/P-t-P/ { print $3 } ' | sed -e s/P-t-P://)
}

function lowercase()  # move filenames to lowercase.
{
    for file ; do
        filename=${file##*/}
        case "$filename" in
        */*) dirname==${file%/*} ;;
        *) dirname=.;;
        esac
        nf=$(echo $filename | tr A-Z a-z)
        newname="${dirname}/${nf}"
        if [ "$nf" != "$filename" ]; then
            mv "$file" "$newname"
            echo "lowercase: $file --> $newname"
        else
            echo "lowercase: $file not changed."
        fi
    done
}

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.

# Setting the value of the Xterm title.
# Only if we are in X...
if [ $DISPLAY ] ; then
	PROMPT_COMMAND='echo -ne "\033]0;${OSRELEASE}: ${PWD}\007"'
fi

if [ -x /usr/bin/finger ] ; then
   INFO=$(finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since" )
else
   INFO=$(uname -msov)
fi

if [ "${BASH_VERSION%.*}" \< "2.05" ]; then
	echo "You will need to upgrade to version 2.05 for programmable completion"
	return
fi

shopt -s extglob        # necessary
set +o nounset	    # otherwise some completions will fail

complete -A hostname   rsh rcp telnet rlogin r ftp ping disk
complete -A command    nohup exec eval trace gdb
complete -A command    command type which
complete -A export     printenv
complete -A variable   export local readonly unset
complete -A enabled    builtin
complete -A alias      alias unalias
complete -A function   function
complete -A user       su mail finger

complete -A helptopic  help     # currently same as builtins
complete -A shopt      shopt
complete -A stopped -P '%' bg
complete -A job -P '%'     fg jobs disown

complete -A directory  mkdir rmdir
complete -A directory   -o default cd

complete -f -d -X '*.gz'  gzip
complete -f -d -X '*.bz2' bzip2
complete -f -o default -X '!*.gz'  gunzip
complete -f -o default -X '!*.bz2' bunzip2
complete -f -o default -X '!*.pl'  perl perl5
complete -f -o default -X '!*.ps'  gs ghostview ps2pdf ps2ascii
complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.pdf' acroread pdf2ps
complete -f -o default -X '!*.+(pdf|ps)' gv
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(jpg|gif|xpm|png|bmp)' xv gimp
complete -f -o default -X '!*.mp3' mpg123
complete -f -o default -X '!*.ogg' ogg123

#=====================================================================================#
# This is a 'universal' completion function - it works when commands have
# a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
_universal_func ()
{
    case "$2" in
	-*)	;;
	*)	return ;;
    esac

    case "$1" in
	\~*)	eval cmd=$1 ;;
	*)	cmd="$1" ;;
    esac
    COMPREPLY=( $("$cmd" --help | sed  -e '/--/!d' -e 's/.*--\([^ ]*\).*/--\1/'| \
grep ^"$2" |sort -u) )
}
complete  -o default -F _universal_func ldd wget bash id info


_make_targets ()
{
    local mdef makef gcmd cur prev i

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    # if prev argument is -f, return possible filename completions.
    # we could be a little smarter here and return matches against
    # `makefile Makefile *.mk', whatever exists
    case "$prev" in
        -*f)    COMPREPLY=( $(compgen -f $cur ) ); return 0;;
    esac

    # if we want an option, return the possible posix options
    case "$cur" in
        -)      COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
    esac

    # make reads `makefile' before `Makefile'
    if [ -f makefile ]; then
        mdef=makefile
    elif [ -f Makefile ]; then
        mdef=Makefile
    else
        mdef=*.mk               # local convention
    fi

    # before we scan for targets, see if a makefile name was specified
    # with -f
    for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
        if [[ ${COMP_WORDS[i]} == -*f ]]; then
            eval makef=${COMP_WORDS[i+1]}       # eval for tilde expansion
            break
        fi
    done

        [ -z "$makef" ] && makef=$mdef

    # if we have a partial word to complete, restrict completions to
    # matches of that word
    if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi

    # if we don't want to use *.mk, we can take out the cat and use
    # test -f $makef and input redirection
    COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.#   ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
}

complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake

_configure_func ()
{
    case "$2" in
        -*)     ;;
        *)      return ;;
    esac

    case "$1" in
        \~*)    eval cmd=$1 ;;
        *)      cmd="$1" ;;
    esac

    COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
}

complete -F _configure_func configure

# cvs(1) completion
_cvs ()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then
	COMPREPLY=( $( compgen -W 'add admin checkout commit diff \
	export history import log rdiff release remove rtag status \
	tag update' $cur ))
    else
	COMPREPLY=( $( compgen -f $cur ))
    fi
    return 0
}
complete -F _cvs cvs

_killall ()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    # get a list of processes (the first sed evaluation
    # takes care of swapped out processes, the second
    # takes care of getting the basename of the process)
    COMPREPLY=( $( /usr/bin/ps -u $USER -o comm  | \
        sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
        awk '{if ($0 ~ /^'$cur'/) print $0}' ))

    return 0
}

complete -F _killall killall killps
3 responses to “Some Ubuntu and Linux Mint tips and tricks.

How is your .bashrc completion different/better than the bash-completion package?

It is just a .bashrc addition I got off a bash tips website, it is just very convenient to have that code in my .bashrc and use that. I guess the the bash-completion package can do that as well, I always have used the code in my .bashrc instead.

Try bash-completion, it does heaps more. It’s one of the first packages I install when I do a new Linux install.

Leave a Reply