Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, August 30, 2010

Change Default Linux Shell

Needs sudo privileges: to change the default shell:
chsh

http://forums.devshed.com/unix-help-35/how-to-change-default-shell-52749.html

Saturday, August 21, 2010

Bash customize autocomplete

http://www.linuxjournal.com/content/more-using-bash-complete-command

Friday, June 18, 2010

Recycle bin script


crm()
{
# pass this function the name of the directory that
# you want to backup and remove
if [ -d "$1" ]
then
cd "${1}/../"
else
echo "Directory does not exist"
return 1
fi

PWD=`pwd`
BKPFILES=`find $PWD -type -f`
BKPDIR="$HOME/.Trash"
daten=`date +%Y.%m.%d-%H.%M.%S`

for i in $BKPFILES
do
echo "$i"
DIRNAME="$BKPDIR"`echo ${i%/*}`
FILENAME=`echo ${i##*/}`
mkdir -p "$DIRNAME"
cp -pf "$i" "${DIRNAME}/${FILENAME}.${daten}"
#NEWFILE=`echo "$i" | sed 's|[ \/]|_|g'`
print "$i" "$NEWFILE" " $DIRNAME"
done
rm -rf "$1"
cd -
}

Tuesday, April 20, 2010

Bash String Operatinos

Remove the last four characters in a string
echo "somefile.txt" | awk 'sub("....$","")

Remove file extension from a string
ls -1 | sed 's/\(.*\)\..*/\1/'

Remove PREFIX from SOMEPATH
SOMEPATH="/home/myuser/usr/bin/"
PREFIX="/home/myuser/"
echo ${SOMEPATH#$PREFIX}
Returns: usr/bin/

Remove shortest match of PREFIX from SOMEPATH
SOMEPATH="/home/sub/subhome/myuser/usr/bin/"
PREFIX="/*/myuser/"
echo ${SOMEPATH#$PREFIX}
Returns: usr/bin/

Parsing parts of a file:
foo=/tmp/my.dir/filename.tar.gz
To get: /tmp/my.dir (like dirname)
path = ${foo%/*}
To get: filename.tar.gz (like basename)
file = ${foo##*/}
To get: filename
base = ${file%%.*}
To get: tar.gz
ext = ${file#*.}

Removing first 8 characters of a string:
echo $var | cut -c9-

http://unstableme.blogspot.com/2008/03/printremove-first-some-characters-of.html
http://tldp.org/LDP/LGNET/18/bash.html
http://docstore.mik.ua/orelly/unix/upt/ch09_07.htm
http://unstableme.blogspot.com/2007/12/removing-last-two-characters-bash.html
http://www.unix.com/shell-programming-scripting/40360-remove-file-extension.html

Friday, August 21, 2009

Relative to Absolute Path in Shell Script

Simple bash function to convert relative paths to absolute pats
fun_abs
{ echo “`cd \`dirname $1\`; pwd`/`basename $1`” }

http://www.robertpeaslee.com/index.php/converting-a-relative-path-to-an-absolute-path-in-bash/

UPDATE: unfortuately the above only works for directories that exist - the perl hack will get around this

fun_abs
{FILE=`$PERL -e "use File::Spec::Functions qw[rel2abs];print rel2abs('$1');"`; echo "$FILE"; }