Below you'll find a collection of bash one liner examples, some short and some very long :D.
Add
Expand all
Collapse all
.
Misc
.
Database
.
Date
.
Sed
.
Network
.
CSV
.
Tar
.
Files
.
Resource .
Misc
Cloning identical machines HD over SSH 02 Jul 2008, ShoKo
dd bs=1M if=/dev/cciss/c0d0 | gzip -1 | ssh 10.10.10.5 'gunzip | dd bs=1M of=/dev/cciss/c0d0' To see what is going on with the transfer on the same machine on a different console:
while true; do killall -USR1 dd; sleep 10; done (and check the original console)
Link monitor 25 Mar 2008, ShoKo
j=0; d=( '/' '-' '\' '|' ); i=0; while true; do echo -en "${d[j++%4]}^[[D"; ping google.com -W 1 -c 1 &> /dev/null && i=0 || echo -ne "$((++i))" > /dev/null; [ $i == 5 ] && i=0 && echo -e "`date` -- LINK DOWN" && ./internet &> /dev/null; sleep 0.5; done
Rotating cursor 25 Mar 2008, ShoKo
d='-/|\' ; while (( t+=1 )); do echo -ne "\b${d:t%4:1}"; sleep 0.25; done
Remote Backup 06 Apr 2007, ShoKo
ssh 10.200.1.180 "tar -c -f - /var/twiki | gzip -" > /var/backup/twiki.back.`date +%Y%m%d`.tgz
View MBR 17 Mar 2007, ShoKo
dd if=/dev/hda of=mbr.bin bs=512 count=1; od -xa mbr.bin;
or
dd if=/dev/sda bs=512 count=1 | od -xa
Find all active chroots (ilya) 16 Jan 2007, ShoKo
ls -d /proc/*/root | xargs -n1 readlink | grep -Fxv / | sort | uniq
ls -l conversion to chmod numeric notation 03 Jan 2007, ShoKo
echo drwxr-x--- | tr "rwx" 1 | tr "-" 0 | sed "s#^.##" | while read -n 3 a; do echo "ibase=2; $a" | bc; done | xargs | sed "s# ##g" for a simpler solution use stat.
create chown script (backup ownership) 03 Jan 2007, ShoKo
ls -lad `find /var/hobbit/` | awk '{print "chown "$3"."$4" "$9}'
or
find /var/hobbit/ -printf 'chown %u.%g "%p"\n'
Windows telnet 06 Dec 2006, ShoKo
(sleep 2; echo "username^M"; sleep 2; echo 'pasword^M'; sleep 2; echo "command1^M" sleep 2; echo "command2^M") | telnet 1.2.3.4The ^M are enters ('ctrl-v + enter' when you type it in shell).
Unattended CLI Commands 06 Dec 2006, ShoKo
(sleep 1; echo "select table_name from user_tables;"; sleep 1; echo "quit";) | sqlplus mozes/******@mozes
Sorted local mounts and memory on remote machine 02 Dec 2006, ShoKo
ssh ptsl656 "df -lh | sort -t a -k 2 -n; free -m"
Set xterm title 02 Dec 2006, ShoKo
PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME//.*} ${USER} ${PWD} \007"'
Database
Mysql show command to bash vars 27 Aug 2008, ShoKo
for lin in `echo "SHOW MASTER STATUS\G" | mysql 2>/dev/null | sed "1d; s#: #='#; s#^ *##; s# #_#g; s#\\\$#'#;"`; do eval $lin ; done After running this line all the values are available as vars (try echo $Position)
Mysql dump for all tables, minimal resource usage, gzipped 25 Mar 2008, ShoKo
ionice -c3 nice -n 19 mysqldump -A -R --skip-opt --add-drop-table --add-locks --create-options --disable-keys --extended-insert --single-transaction --quick --set-charset | nice -n 19 gzip -3 > /var/mysql_dump.gz Requires ionice. On debian: apt-get install schedutils
Mysql dump for a specific table 13 Feb 2008, ShoKo
/usr/local/mysql/bin/mysqldump -p<password> -R --opt -e -B <database>
Row number for each table in a databse 13 Feb 2008, ShoKo
for i in `echo "show tables" | mysql <database> | sed "1d"`; do c=`echo "select count(*) from $i" | mysql <database> | sed "s#(.*)##; 1d"`; printf "%-16s %s\n" $i $c; done
Mysql dump for all tables 18 Jan 2007, ShoKo
/usr/local/mysql/bin/mysqldump -A -p<password> -R --opt -e complete dump with all standard options
Date
Timestamp to date, another option 20 Oct 2008, ShoKo
date -d "1970-01-01 1184521826 sec GMT" Converts 1184521826 timestamp to date
Standard date strings for 5 days back (perl for solaris) 04 Jan 2007, ShoKo
/usr/local/bin/perl -e "use Time::ParseDate; print scalar(localtime(parsedate(scalar(localtime($NOW)))-(60*24*60*60)))"; Requires perl with Time::ParseDate
Standard date strings for 5 days back - Simpler (perl for solaris) 16 Jan 2007, ShoKo
/usr/local/bin/perl "print scalar localtime time - 5*86400;"
Timestamp to date 27 Dec 2006, ShoKo
echo $((`date +%s`-35*24*60*60)) | awk '{print strftime("%c",$1)}' The date 35 days ago
numeric only date syntax 06 Dec 2006, ShoKo
date +%G%m%d%H%M%S output: <year><month><day><hour><minute><second>, 20061206135408
Sed
Sed - shrink lines 06 Dec 2006, ShoKo
cat /u01/app/oracle/product/9.2.0/network/admin/tnsnames.ora | sed -n "/^$/{ :a; N; s#10\.0#10\.0#; T a; s#\n# #g; s#^ \([^ ]*\).*\(10.0.0.[0-9]*\).*#\1=\2#; p;}"input file:
# TNSNAMES.ORA Network Configuration File: /u01/app/oracle/product/9.2.0/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
MOZES =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.10)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = mozes)
)
)
MOZES2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.18)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = mozes)
)
)
Network
Send SMS via clickatell 10 Mar 2008, IlyaSher
curl 'https://api.clickatell.com/http/sendmsg?api_id=FILL&user=FILL&password=FILL&to=FILL_PHONE&text=FILL_TEXT
IPs that made most requests 06 Dec 2006, ShoKo
awk '{ x[$1]++; } END { for(k in x) {print x[k] " " k} }' var/log/httpd/access_log | sort -n | tail -10
All Local IPs 06 Dec 2006, ShoKo
ifconfig -a | grep "inet addr" | sed "s#[^:]*:\([^ ]*\).*#\1#" | grep -v 127.0.0.1 | xargs
CSV
Print specific values from CSV 06 Dec 2006, ShoKo
ifs=$IFS; IFS='$'; sed "1d" nodes | while read -a w; do echo ${w[0]} ${w[4]} ${w[20]}; done; The csv is with $dollar delimiter
Get CSV var positions numbers by header 06 Dec 2006, ShoKo
head -1 nodes | tr "$" "\n" | cat -n
Tar
Tar whole file systems with exclusion over ssh 28 Sep 2008, ShoKo
tar zvf - --one-file-system --preserve --exclude="LogAnalysisInfo" --exclude="mysql.prod" --create / /tmp /var /var/log | ssh 1.1.1.1 "cat > /var/backup.mg1.tgz" Also good for excluding remote mounts like NFS.
Tar a file list 02 Dec 2006, ShoKo
rm f-sec.tar; rpm -q --filesbypkg f-secure-ssh-server-3.3.2-21.sles9 | awk '$2 !~ /\/usr\/share\/|rc\.d/ {print $2}' | xargs /usr/intel/bin/gtar rvf f-sec.tar
Check existing files from tar 02 Dec 2006, ShoKo
for file in `tar tf f-sec.tar`; do [ -e "/$file" ] && echo "EXIST: /$file"; done
Files
Compare local and remote directories 25 Oct 2008, ShoKo
vimdiff <(find /var/www/ | sort) <(ssh remote "find /var/www/" | sort)
Delete older than x days 27 Aug 2008, ShoKo
find /etc/ -type f -mtime -x -exec rm {} \;
Conditional operation if file is older than 27 Aug 2008, ShoKo
[ $((`/bin/date +%s`-`/usr/bin/stat -c "%Y" /file`)) -ge 20 ] && ls -la /etc/
Resource
Complete apache memory usage 24 Oct 2008, ShoKo
memstat -w | egrep "`ps awux | grep apache | grep -v grep | awk '{print $2}' | xargs | tr ' ' '|'`" | awk '{gsub("k:",""); sum+= $1}; END {print sum"k"}' This can be used to many other apache like procs
Cumulative CPU with indented child procs 25 Mar 2008, ShoKo
Cumulative CPU, sorted by CPU usage 25 Mar 2008, ShoKo
Track a specific command execution information 02 Dec 2006, ShoKo
while true; do echo; /usr/intel/bin/gtime -v ls -laR ~/ >/dev/null; for ((i=5;i--;i>0)); do echo -n "."; sleep 1; done; done
Feel free to send me interesting one liners to shokoe at gmail
and I'll add it to this page