User Tools

Site Tools


shell

Shell

Recursively search & replace

find ./ -type f -exec sed -i 's/foo/bar/g' {} \;
find ./ -type f | xargs sed -i 's/foo/bar/g'
grep -rl foo somedir/ | xargs sed -i 's/foo/bar/g'
rpl -x'.cpp' -x'.h' -pR "foo" "bar" *

Prepend line to file

sed -i '1i LINE_TO_PREPEND' /tmp/the_file
$ date +%s    # current timestamp
$ date -d "2012-09-09 08:35" +%s

Convert timestamp to date

$ date -d@1234567890
Sa 14. Feb 00:31:30 CET 2009

Bash

Extract parts of filename

# basename of script file
echo ${0##*/}
export testfile="/tmp/test/foo.bar.jpg"
 
# extract path + filename without extension ("/tmp/test/foo.bar")
echo ${testfile%.*}
 
# extract file extension ("jpg")
echo ${testfile##*.}
 
# extract basename (=filename only) ("foo.bar.jpg")
echo ${testfile##*/}
 
# extract dirname (=path only) ("/tmp/test")   [Warning: May differ from dirname]
echo ${testfile%/*}

Determine whether a function exists

declare -F my_funct >/dev/null && my_funct || echo "Function does not exist."

Determine script location path

echo $(dirname "$(readlink -f "$BASH_SOURCE")")

Clear execution path cache

$ hash      # show cache
$ hash -r   # clear
shell.txt · Last modified: 2013-02-22 23:20 CET by dominik