пятница, 23 сентября 2011 г.

bash howto's 2

#extract from tar.xz
tar -xpJf archive.tar.xz

#setup environment variable
export VAR_NAME="value"
#somewhere use it
VAR_NAME=${VAR_NAME:-"default value"}
echo "$VAR_NAME"

#Запуск команд от имени системных пользователей
#Иногда требуется запустить некую команду, программу или просто проверить права #доступа от имени системных пользователей nobody, apache, и т.д. Простой запуск через
#su - nobody
#Даёт
#This account is currently not available.
#Это происходит из-за того, что у этих пользователей в качестве шела указан /sbin/nologin. #Можно воспользоваться параметром -s для указания другого шела:
su - nobody -s /bin/sh
#И получить нужный результат.

#how to list all disk drives ?
fdisk -l

#Copy a directory (and subdirectories) without .svn files.
rsync -a --exclude=.svn source destination

#Copy directory structure (without files)
find /where/to/search -type d | cpio -pvdm /where/to/copy

#find string in files
grep -lir "qwe"
grep -lir "mod.*" ./log*
find ./ -name log* -exec grep -l qwe {} \;

#bash filename extraction
FILENAME="/some/path/my.file.ext"
echo "Path: ${FILENAME%/*}"
echo "Filename: ${FILENAME##*/}"
echo "Extension: ${FILENAME##*.}"
BASENAME=${FILENAME##*/}
echo "Filename w/o extension: ${BASENAME%.*}"

#find the ten biggest files on your file system linux command
du -a /var | sort -n -r | head -n 10

#Show disk usage under Linux for folder
du -h -s /some/folder/path/*

# delete empty lines
sed "/^$/d" input
# remove spaces
sed 's/ //g'
# remove newline chars
sed 's/\n//g'

# search and replace
sed "s/searchpattern/replacepattern/" input

# find commandline options using sed and grep
URL=$(echo $@ | sed 's:\ :\n:g' | grep "url" | sed 's:\-\-url=::')

#Random string
openssl rand -base64 30

#Returns a list of the number of connections by IP.
netstat -an | grep :80 | awk '{print $5}' | cut -f1 -d":" | sort | uniq -c | sort -n
#Print out a list of open connections on your box and sorts them by according to IP address
netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n
#Print the number of established and listening connections to your server on port 80 per each IP address
netstat -plan|grep :80|grep -E "(EST|LIST)"|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1

#Get Public IP and copy it to Clipboard
#!/bin/bash
MYIP=`curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.\.]*\).*/\1/g'`
echo $MYIP | pbcopy
/usr/local/bin/growlnotify 'IP address copied' -m "Address was: $MYIP"

#backup
sudo su
cd /
tar cvpjf /tmp/backup.tar.bz2 --exclude=/proc --exclude=/tmp --exclude=/dev --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /

#http://www.catonmat.net/blog/sed-one-liners-explained-part-one/
#http://sed.sourceforge.net/sedfaq4.html
#http://sed.sourceforge.net/sed1line.txt
#http://tdistler.com/2009/04/17/sed-regular-expression-find-lines-not-matching-a-string
#Substitute (find and replace) only the last occurrence of "foo" with "bar".
sed 's/\(.*\)foo/\1bar/'
#Substitute all occurrences of "foo" with "bar" on all lines that DO NOT contain "baz".
sed '/baz/!s/foo/bar/g'
#Substitute all occurrences of "foo" with "bar" on all lines that contain "baz".
sed '/baz/s/foo/bar/g'

#extract language
cat $F | grep "Stream" | sed "s/ //g" | cut -f 1 -d ":" | sed "/.*(.*).*/!s/.*/und/g" | sed "s/^.*(\(.*\))$/\1/"

#echo full path to command
echo $0
echo "$0" | sed "s|^./|$(pwd)/|"
which ffmpeg
which ffmpeg | sed "s|^./|$(pwd)/|"

#/usr/local/bin/get-ip-address
/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{ print $2 }' | awk -F: '{ print $2 }'