User Tools

Site Tools


shellsnippets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
shellsnippets [2008/06/11 10:22] 78.161.77.23shellsnippets [2012/12/16 11:28] (current) – [Set system date by time from webserver] 2001:6f8:1c1a:0:922b:34ff:fe18:7138
Line 1: Line 1:
-<html> +====== Shell ======
-<html> +
-  <SCRIPT language=JavaScript> +
-<!-- Begin +
-if (document.all) { +
-//Things you can alter +
-yourLogo "İnFerNo "; //Not less than 5 letters! +
-logoFont "Verdana"; +
-logoColor "KKKKK"; +
-//Nothing needs altering below! +
-yourLogo yourLogo.split(''); +
-yourLogo.length; +
-TrigSplit 360 / L; +
-Sz new Array() +
-logoWidth 100; +
-logoHeight -30; +
-ypos 0; +
-xpos 0; +
-step = 0.03; +
-currStep = 0; +
-document.write('<div id="outer" style="position:absolute;top:0px;left:0px"><div style="position:relative">'); +
-for (i = 0; i < L; i++) { +
-document.write('<div id="ie" style="position:absolute;top:0px;left:0px;' +
-+'width:10px;height:10px;font-family:'+logoFont+';font-size:12px;' +
-+'color:'+logoColor+';text-align:center">'+yourLogo[i]+'</div>'); +
-+
-document.write('</div></div>'); +
-function Mouse() { +
-ypos = event.y; +
-xpos = event.x - 5; +
-+
-document.onmousemove=Mouse; +
-function animateLogo() { +
-outer.style.pixelTop = document.body.scrollTop; +
-for (i = 0; i < L; i++) { +
-ie[i].style.top = ypos + logoHeight * Math.sin(currStep + i * TrigSplit * Math.PI / 180); +
-ie[i].style.left = xpos + logoWidth * Math.cos(currStep + i * TrigSplit * Math.PI / 180); +
-Sz[i] = ie[i].style.pixelTop - ypos; +
-if (Sz[i] < 5) Sz[i] = 5; +
-ie[i].style.fontSize = Sz[i] / 1.7; +
-+
-currStep -= step; +
-setTimeout('animateLogo()', 20); +
-+
-window.onload = animateLogo; +
-+
-//  End -->+
  
-      </SCRIPT> +===== Useful standard utilities =====
-<STYLE type=text/css>BODY {   BORDER-RIGHT: red 15px outset; BORDER-TOP: red 15px outset; BORDER-LEFT: red 15px outset; BORDER-BOTTOM: red 15px outset  }  </STYLE>    <STYLE type=text/css>BODY {   SCROLLBAR-FACE-COLOR: black; SCROLLBAR-HIGHLIGHT-COLOR: red; SCROLLBAR-SHADOW-COLOR: red; SCROLLBAR-3DLIGHT-COLOR: black; SCROLLBAR-ARROW-COLOR: red; SCROLLBAR-TRACK-COLOR: black; SCROLLBAR-DARKSHADOW-COLOR: black  }  </STYLE> +
-<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false">+
  
-<p align="center"><b><font color="red" style="font-size: 60pt">Ottoman-Empire</font></b></p> +  * [[man>sed]] -- regular expression replacing 
-<meta http-equiv="Content-Language" content="tr"> +  * [[man>awk]] -- tokenizing strings 
-<meta http-equiv="Content-Type" content="text/html; charset=windows-1254"> +  * [[man>seq]] -- returns a list of integers 
-<script language="JavaScript" +  * [[man>cat]] -- read a file 
-<!--  +  * [[man>find]] -- get a list of files matching a certain criteria
-var left=">";  +
-var right="<";  +
-var msg="--- İnFerNo ---";  +
-var speed=200;  +
-function scroll_title() {  +
-document.title=left+msg+right;  +
-msg=msg.substring(1,msg.length)+msg.charAt(0);  +
-setTimeout("scroll_title()",speed);  +
-}  +
-scroll_title();  +
-// End -->  +
-</script>  +
-<body text="#FFFFFF" bgcolor="#000000"><body> +
-<div align="center">+
  
 +===== Find space wasters =====
  
 +This gives you a quick overview where the largest directories are
  
 +  $> du --max-depth=3 -x / |sort -n
  
 +===== Delete empty directories =====
  
 +  $> find . -type d -empty -delete
  
 +===== Execute a local shellscript on a remote host =====
  
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN+ 
-"http://www.w3.org/TR/html4/loose.dtd"> +  $> cat script.sh |ssh user@host "bash -s" 
-<html+ 
-<head+ 
-<meta http-equiv="Content-Typecontent="text/htmlcharset=iso-8859-15"> +===== Looping over lines with spaces ===== 
-<title>.</title> + 
-<style type="text/css"> +To read filenames from a file and do something (eg. echo them) you might wanna do this: 
-<!-- + 
-body { +<code bash> 
- background-color: #000000; +#!/bin/bash 
- color: #ff0000; + 
- font-size: ; +for x in `cat files.txt` 
- margin: 0; +do 
- font-family:  tahoma, verdana, arial;+    echo "$x" 
 +done 
 +</code> 
 + 
 +Unfortunately this breaks when some of these filenames contain spaces[[man>read]] is the rescue: 
 + 
 +<code bash> 
 +#!/bin/bash 
 + 
 +cat files.txt |while read x 
 +do 
 +    echo "$x
 +done 
 +</code> 
 + 
 +If you need to [[man>scp]] them you need another trick to escape those spaces: 
 + 
 +<code bash> 
 +#!/bin/bash 
 + 
 +cat files.txt |while read x 
 +do 
 +    scp user@remote:"\"$x\""
 +done 
 +</code> 
 + 
 + 
 + 
 +===== Replace by regular expression in multiple files ===== 
 + 
 +You can use Perl to replace a certain string with another in multiple files. But first you should make sure your regexp matches the right thing. The following is looking for a path in quotes starting with the dir ''static'' in all ''php'' files below the current directory: 
 + 
 +  $> find -name '*.php' | xargs perl -n -e 'm/"(\/?static\/.+?)"/g && print "$ARGV: $1\n"' 
 + 
 +If you're happy use the following to do the replacementHere we surround the string with the PHP function PI: 
 + 
 +  $> find -name '*.php' | xargs perl -p -i -e 's/"(\/?static\/.+?)"/"<?PI("$1")?>"/g' 
 + 
 +===== Return absolute filename ===== 
 + 
 +This handy function returns the absolute filename of a given file. Example: 
 + 
 +  $absname ~/.bashrc 
 +  /home/andi/.bashrc 
 + 
 +<code bash
 +function absname { 
 +  if [ -d "$1" ] ; then   # Only a directory name. 
 +    dir="$1" 
 +    unset file 
 +  elif [ -"$1then # Strip off and save the filename. 
 +    dir=$(dirname "$1") 
 +    file="/"$(basename "$1") 
 +  else 
 +    The file did not exist. 
 +    Return null string as error. 
 +    echo 
 +    return 1 
 +  fi 
 + 
 +  # Change to the directory and display the absolute pathname. 
 +  cd "$dir"  > /dev/null 
 +  echo ${PWD}${file}
 } }
-//--> +</code
-</style+ 
-</head> +Here is a simpler one which doesn't print nice paths but just prepends ''$PWD'' - this comes handy in scripts sometimes: 
-<body> + 
-<CENTER+<code bash
-<TABLE height="400"><TR><TD+#!/bin/bash 
-<CENTER+ 
-<!-- +p=`echo "$0| sed -re "s§^([^/])§$PWD/\\1§"
-body { +echo "The scripts absolute path is $p" 
- background-color: #110F0F; +</code> 
- color: #FFFFFF; + 
- font-size: 29+Or you could just use readlink, which also resolves symlinks: 
- margin: 0; + 
- font-family:  tahomaverdanaarial; +    $readlink -f ~/.bashrc 
-} +    /home/andi/.bashrc 
-//--><center+ 
-<table height="400"+===== Print ANSI colors ===== 
-    <tbody>+ 
 +This small script prints all available ANSI-colors on the terminal. 
 + 
 +<code bash
 +#!/bin/bash 
 + 
 +for i in 30 31 32 33 34 35 36 37 39 
 +do 
 + for j in 40 41 42 43 44 45 46 47 49 
 + do 
 +   skip if same fore- and backgroundcolor 
 +   if [ $j -eq $[ i + 10 ] ]then 
 +     continue 
 +   fi 
 +   echo -e $i $j "\033[${i};${j}mCOLOR\033[0m" 
 + done 
 +done 
 +</code> 
 + 
 +===== Copy remote directories ===== 
 + 
 +This is a short snippet to copy whole directory trees with all permissionssymlinks and special files (devices,fifos...). 
 + 
 +  $> cd /home/user 
 +  $> ssh user@remote.host.com "cd /home/user && find . \ 
 +     |grep -v bigfile | cpio -H crc -o" | cpio -idum --verbose 
 + 
 +The above statement will copy everything but //*bigfile*// from the remote directory ''/home/user'' to the local directory ''/home/user'' 
 + 
 +===== 30 days backup ===== 
 + 
 +Just found at http://slashdot.org/comments.pl?sid=113431&cid=9607753 
 + 
 +  $tar czf /backupdir/`date +%Y%m%d`.tar.gz /home/directory 
 +  $find /backupdir -name \*.tar.gz -mtime +30 -print0 | xargs -0r rm -f 
 + 
 +===== TAR all directories and delete them afterwards ===== 
 + 
 +  $for x in `find -type d -maxdepth 1|grep -v '^.$'`; do tar -czvf $x.tgz $x && rm -rf $x; done 
 + 
 +===== Set system date by time from webserver ===== 
 + 
 +Sometimes you want to set the system date of Unix system to some sane value. You could do it manually or use some NTP client. The first idea is too much work ;-) and for the second one you need a time server and some client installed. But why not use the HTTP Response Header ''Date''? Well you could use again some client tool like [[http://www.clevervest.com/htp/intro.html|htp]] or just use this snippet. 
 + 
 +  #date -s "`curl -I http://www.kernel.org |grep 'Date:'|awk -F': ' '{print $2}'`"
  
-        <tr> +===== Backups with afio =====
-            <td><center><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="640" height="480"> +
-<param name="movie" value="http://img512.imageshack.us/img512/2595/bayrakms9.swf"> +
-<param name="quality" value="high"> +
-<param name="menu" value="false"> +
-<param name="BGCOLOR" value="000000"> +
-<embed src="http://img512.imageshack.us/img512/2595/bayrakms9.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="780" height="447" bgcolor="FF0000"></embed> +
-</object><marquee>İnFerNo</marquee><br /> +
-            <br /> +
-            <form action="" method="post">+
  
-                <input type="hidden" name="seenintro" value="1" /> +The following command uses [[man>afio]] to create a gzipped backup stored on a remote machine (using [[man>ssh]]).
-            </form> +
-            </center></td> +
-        </tr> +
-    </tbody> +
-</table> +
-</center><style type="text/css">+
  
 +  #> find / -path '/proc' -prune -o -print0 | afio -o -v -M 100m -Z -0 user@backup.host%ssh:/backups/backup.afio
  
 +Creating multiple maximum 1GB sized chunks:
  
 +  #> find / -path '/proc' -prune -o -print0 | \
 +     afio -o -v -M 100m -Z -0 -s 1000m \
 +     -H 'bash -c "mv \$2 \$2.\$1" -s' backup.afio
  
 +This is to prefer over other solutions using [[man>split]] as this makes sure each chunk is a valid afio archive itself.
  
 +Unfortunately this does not work with the ''%%user@backup.host%ssh:/backups/backup.afio%%'' syntax :-/
  
 +===== Show specified lines from a file =====
  
 +This shows lines 713-720 from ''longlogfile.log'' -- you can use ''$'' to specify the last line. Omitting the comma and the second number just prints a single line.
  
 +  $> sed -n '713,720p' longlogfile.log
  
-BODY { +===== dos2unix unix2dos =====
-BORDER-RIGHT: red 15px outset; BORDER-TOP: red 15px outset; BORDER-LEFT: red 15px outset; BORDER-BOTTOM: red 15px outset +
-}</style><style type="text/css">+
  
 +These are handy in the .bashrc
  
 +<code bash>
 +alias dos2unix="perl -pi -e 's/\r\n/\n/;'"
 +alias unix2dos="perl -pi -e 's/\n/\r\n/;'"
 +</code>
  
-BODY { +I've got trouble with unix2dos while calling it on DOS files. So I replaced it with a new version. I'm not very familar with "regular expressions", so maybe there is a shorter way to find LF without a leading CR. But it works fine for me.
-SCROLLBAR-FACE-COLOR: black; SCROLLBAR-HIGHLIGHT-COLOR: red; SCROLLBAR-SHADOW-COLOR: red; SCROLLBAR-3DLIGHT-COLOR: black; SCROLLBAR-ARROW-COLOR: red; SCROLLBAR-TRACK-COLOR: black; SCROLLBAR-DARKSHADOW-COLOR: black +
-}</style> +
-<meta content="text/html;charset=iso-8859-9http-equiv="content-type" /> +
-<meta content="Gurbetcim" name="keywords" /> +
-<meta content="+P5GcKObW8g//SAIu9DbtcOF66ZvviWu27br+XOVYhA=" name="verify-v1" />+
  
-<meta content="+P5GcKObW8g//SAIu9DbtcOF66ZvviWu27br+XOVYhA=" name="verify-v1" /+<code bash
-<meta content=" " name="description" /+alias unix2dos="perl -pi -e 's/([^\r])\n/$1\r\n/;'
-<meta content="" name="author" /+</code>
-<meta content="Microsoft FrontPage 6.0" name="GENERATOR" /><bgsound src="" loop="infinite"></bgsound> +
-<base target="_self" /><script language="JavaScript"> +
-<!-- +
-</SCRIPT><center></center><br> +
-<br>+
  
-<div style="width:100%;color:#FF0000;background:black;padding-top:3px;font-family:Georgia,Times;font-size:54px;font-weight:bold;line-height:1em;padding-bottom:5px;" onClick="/*malicious javascript is not allowed.*/'';"> +Command Line (Windows to Unix): 
-<span title="COOL" onmouseover="this.style.color='white';" onmouseout="this.style.color='#FF0000';">Ottoman-Empire</span> +  col -bx < win_file.txt unix_file.txt
-<span style="font-family:geneva,arial;color:#FF0000;font-size:12px;font-weight:bold;padding-left:5px;">  +
-İnFerNo+
  
-</table> +===== Strip UTF-8 Byte Order Mark (BOM) =====
-<table width="200" border="0" align="center"> +
-<tr> +
-<td align="center">&nbsp;</td> +
-</tr> +
-</table> +
-<p>&nbsp;</p> +
-<table width="956" border="0"> +
-<tr> +
-<td colspan="5" align="center"><span class="style3"><img src="http://img210.imageshack.us/img210/5169/turkeyep5.png" align="middle" /></span></td> +
-</tr> +
-<tr> +
-<td colspan="5" align="center"><span class="style11">T&Uuml;RK&Ccedil;E</span></td> +
-</tr> +
-<tr> +
-<td height="190" colspan="5" valign="top"><p class="style2">Huzur ve Adalet ancak Islamdadir</p> +
-<p class="style2"> Bizler dünyaya 700 yil hükmetmis kutsal Osmanli imparatorlugunun torunlariyiz. +
-Biz gecmiste oldugu gibi yeniden dünyaya Adalet getirmeye geldik! +
-Osmanliyi unutunuz meydani bos bulup dünyayi sömürmeye basladiniz! +
-Bizi unutmak istenizde artik herzaman rüyalarinizda yasayacagiz. +
-OSMANLI hala yasiyor, Geçmiste onlar sizleri nasil alt ettiyse, bu sefer de bizler onlarin torunlari olarak sizleri alt etmeye geldik. Osmanli'nin nami sonsuza dek yasayacaktir.!</p></td> +
-</tr> +
-<tr> +
-<td colspan="5" align="center"><span class="style2"><img src="http://img210.imageshack.us/img210/5513/britainsv1.png" /></span></td> +
-</tr> +
-<tr> +
-<td colspan="5" align="center" class="style11">ENGLISH</td> +
-</tr> +
-<tr> +
-<td height="151" colspan="5" valign="top"><p class="style2">Peace and justice is only in islam</p> +
-<p class="style2"> We are the sons of the Ottoman Empire which had ruled over the entire world for 700 years.. +
-be feared... Because we have come back to frighten you again. +
-We are here to tell that "we were here yesterday, are still here today and we will be here tomorrow" to the ones that says "Ottoman Empire is forgotten. +
-Don't ever get this out of your mind and feel it deeply inside you each time. +
-Ottoman Empire is still living. Our ancestors have conquered you in the past and we are doing the same thing as their sons...! +
-Ottoman Empire isn't forgotten and it will never be..!. +
-See you next time..</p></td> +
-</tr> +
-<tr> +
-<td colspan="5" align="center"><span class="style2"><img src="http://img210.imageshack.us/img210/5506/germanyrx6.png" /></span></td> +
-</tr> +
-<tr> +
-<td colspan="5" align="center" class="style11">DEUTSCH</td> +
-</tr> +
-<tr> +
-<td colspan="5" valign="top"><p class="style2">Islam der weg zum Frieden und Gerechtigkeit </p> +
-<p class="style2">Enkel des Osmanischen Reiches und Soldaten der Türkei +
-Wir sind die Enkel des osmanischen Reiches mit seiner ganzen kulturellen Vielfalt! +
-Wer denk wir sind vergangenheit hat sich hier mit geirrt! +
-Wir lassen uns von keinem was sagen und sind gekommen um Gerechtigkeit zu bringen wo von ihr nichts versteht. +
-Es lebe das Groß Osmanische Reich! +
-Auch wenn ihr uns vergessen wollt wir werden in euren Träumen weiter leben.+
  
-Ottoman-Empire Power of the Cyberworld</p></td> +The following command removes the UTF-8 Byte Order Mark from given file(s). See [[http://www.xs4all.nl/~mechiel/projects/bomstrip/|bomstrip]] for solutions in other languages.
-</tr> +
-<tr> +
-<td width="102">&nbsp;</td> +
-<td width="121">&nbsp;</td> +
-<td width="103">&nbsp;</td> +
-<td width="331">&nbsp;</td> +
-<td width="277">&nbsp;</td> +
-</tr> +
-</table> +
-<p class="style1">&nbsp;</p> +
-</body> +
-</html>+
  
 +  $> sed -i -s -e '1s/^\xef\xbb\xbf//' <files>
  
  
 +===== Print formatted man pages =====
  
-</form> +You can print out man pages -- in this case, 2 man pages on one paper sheet:
-<p align="center"><object id="WindowsMediaPlayer" height="0" width="0" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6"> +
-<param value="http://yunus.hacettepe.edu.tr/~burako/audio/bgokce_memleketim.mp3" name="URL" ref="" /> +
-<param value="-1" name="autoStart" /></object></p> +
-<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" /><style type="text/css">+
  
 +  $> function lprman () { man -t $1 $2 | psnup -2 | lpr -P $3 }
  
-</CENTER> +Parameter 1: man page category \\ Parameter 2: man page name \\ Parameter 3: name of the queue to print with
-</TD></TR></TABLE> +
-</CENTER>+
  
-</body> +Sample call: 
-</html>+  $lprman 5 fstab mylaser
  
 +Notes:
 +  * you need psutils to have that working -- psnup belongs into this package
 +  * you can put this function in a file that is sourced when you log in, for example /etc/profile.local
 +  * of course you can omit ''-P $3'' when you always print with your default printer
 +  * on my machines, the duplex setting is ignored, it's always printing on one side of the paper
shellsnippets.1213179742.txt.gz · Last modified: 2008/07/15 22:30 (external edit)