Bash
Some one liners and stuff for bash.
Config Files without comments or blank lines
alias catconf="grep -v '^$\|^\s*\#'"
DateStamp
date +%Y_%m_%d
tar czvf Ansible_`date +%Y_%m_%d`.tgz /home/ansible/ --exclude 'ansible.log'
tar czvf `hostname`_`date +%Y_%m_%d`.tgz /home/ansible/ --exclude 'ansible.log'
Uptime as a datestamp
date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%Y/%m/%d %H:%M"
#Determine if it is an odd or even week
weeknumber=$(echo "(`date +%d` / 7 + 1)" | bc )
if [ $weeknumber == 1 ]; then
echo "don't run on first week of month. exiting"
exit
fi
echo $week
if [ $(echo $(( $weeknumber % 2 ))) == 0 ]; then
week=even
else
week=odd
fi
Backspace
stty -a
Set backspce
stty erase ^?
History
C-r #Ctrl R
Reverse search through history
Brace Expansion
touch {jan,feb,mar}-{report,expense}.txt
Create 2 files for each of the 3 months.
Linux Performance tools
http://www.youtube.com/watch?v=t0XDEAECObA&feature=player_embedded
apt-get install sysstat
CPU
vmstat
mpstat -P ALL
top
sar
iostat
ps
Disk
apt-get install bonnie++
prompt
vi ~/.bash_profile
PS1="\[\033[01;37m\]\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\];)\"; else echo \"\[\033[01;31m\];(\"; fi) $(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;36m\]\u@\h'; fi)\[\033[01;35m\] \w \$\[\033[00m\] "
Tar
Create a gziped tar file
tar czvf blah.tgz /home/kevin/
tar czvf Ansible_`date +%Y_%m_%d`.tgz /home/kevin/ --exclude 'ansible.log' --exclude 'sendmailanalyzer-9.0.tar.gz' --exclude '/home/kevin/sendmailanalyzer-9.0'
List the contents of a tarball
tar -tvf blah.tgz
Untar
tar -xvf blah.tgz
Sed
find and replace in multiple files
find . -name "run_*" -exec sed -i "s/chmod a/chmod -R a/g" '{}' \;
Find a matching line in a file and append the contents of another file to that line
for f in *
do
echo "Processing $f file..."
#http://www.grymoire.com/Unix/Sed.html
#get the contents of the file and remove spaces to make the following sed work properly
filecontents=$(cat $f | sed 's/ //g')
#sed -i for inplace
#broken into multiple quoted or double quoted sections to handle variables properly
#the first backslash is to denote the next character will be the delimiter or underscore in this case
#find a line that starts with $f
#then add the contents of the file to the end of that line
sed -i '\_^'$f"_ s_\$_"$filecontents'_' $tmpserverlist
filecontents=""
cat $f
done
Sort
Sort on the first column 15th character
sort -k 1.15
Capturing Time to run a command into a variable
https://bbs.archlinux.org/viewtopic.php?id=155044
t=$({ time sleep 1 >/dev/null 2>&1;} 2>&1 )
echo $t
split substring
http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
Split string based on delimiter in shell
But if you would write something useable under many shells, you have to not use bashisms.
There is a syntax, used in many shells, for splitting a string accros first or last occurence of a substring:
${var#*SubStr} # will drop begin of string upto first occur of `SubStr`
${var##*SubStr} # will drop begin of string upto last occur of `SubStr`
${var%SubStr*} # will drop part of string from last occur of `SubStr` to the end
${var%%SubStr*} # will drop part of string from first occur of `SubStr` to the end
reverse diff
find matching lines
fgrep -xf file1 file2
netcat nc
http://www.rackspace.com/knowledge_center/article/testing-network-services-with-netcat
nc -vz machine port