Latest 100 public
snipts » bash
showing 21-40 of 578 snipts for bash
-
∞ Tar gz (ok compression, fast)
tar -zcvf archive_name.tar.gz directory_to_compress
-
∞ Untar to directory
tar -xvf archive_name.tar -C /tmp/extract_here/
-
∞ Untar
tar -xvf archive_name.tar.gz
-
∞ Tar
tar -cvf archive_name.tar directory_to_compress
-
∞ Unzip
unzip archive_name.zip
-
∞ Zip
zip -r archive_name.zip directory_to_compress
-
∞ Validate IP in bash
#!/bin/bash # Test an IP address for validity: # Usage: # valid_ip IP_ADDRESS # if [[ $? -eq 0 ]]; then echo good; else echo bad; fi # OR # if valid_ip IP_ADDRESS; then echo good; else echo bad; fi # function valid_ip() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then OIFS=$IFS IFS='.' ip=($ip) IFS=$OIFS [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] stat=$? fi return $stat } # If run directly, execute some tests. if [[ "$(basename $0 .sh)" == 'valid_ip' ]]; then ips=' 4.2.2.2 a.b.c.d 192.168.1.1 0.0.0.0 255.255.255.255 255.255.255.256 192.168.0.1 192.168.0 1234.123.123.123 ' for ip in $ips do if valid_ip $ip; then stat='good'; else stat='bad'; fi printf "%-20s: %s\n" "$ip" "$stat" done fi
-
∞ Bulk File Rename
# Bulk File Rename # 'sed' command is used here to replace the filename with 'mv' command # which is then piped to Shell - # Search for '*' and replace with 'mv "<FILENAME>" <FILENAME>.old' # and pipe it to Shell ls *.* | sed -e 's/\(.*\)/mv "\1" "\1.old"/' | sh # To rename *.php3 files to *.php3.old ls *.php3 | sed -e 's/\(.*\)/mv "\1" "\1.old"/' | sh # To rename *.php3 files to *.php ls *.php3 | sed -e 's/\(.*\).php3/mv "\1.php3" "\1.php"/' | sh
-
∞ Converts windows line ending to Unix/Linux line ending
perl -pi -e 's/\r\n/\n/g' <input-file> -
∞ Verify signatures
# Import a public key gpg --import <public_key>.asc # Verify a signature gpg --verify <file_name>.asc # To surpress the warning, trust public keys # shows the long identifier of the public key gpg --with-colons --list-keys "<public_key>" # trusts it gpg --trusted-key 0123456789ABCDEF --update-trustdb
-
∞ calculator
#!/bin/bash while [ $ != 'x' ] do echo "Welcome to calculator (x to quit)" echo "Enter the first operand: " read value1 echo "Enter an operator (+, -, *, /): " read operator echo "Enter the second operand: " read value2 if [ " $ operator " = " + " ] ; then answer=$(echo "scale=2;value1+value2" |bc); elif [ " $ operator " = " - " ] ; then answer=$(echo " scale=2;value1-value2" |bc); elif [ " $ operator " = " / " ] ; then answer=$(echo "scale=2;value1/value2" |bc); elif [ " $ operator " = " * " ] ; then answer=$(echo "scale=2;value1*value2" |bc); elif [ " $ operator " = " % " ] ; then answer=$(echo "scale=2;value1%value2" |bc); fi echo "Answer: $value1 $operator $value2 = $answer"; done
-
∞ Configurar interfaz virtual en Linux
; Importante si te quieres comunicar con otra red :D # ifconfig ethx:1 IP -
∞ Cambiar MAC Adress en Linux
; El cambio no es permanente # ifconfig ethx down # ifconfig ethx hw ether 00:AA:BB:CC:11:22 # ifconfig ethx up
-
∞ Disable ping reply
# Disable ping reply temporarily echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all # Enable the ping reply back echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
-
∞ Check if gzip/delfate is enabled (2 ways)
curl -I -H "Accept-Encoding: gzip,deflate" "$URL" --silent | grep -i "Content-Encoding:" # OR curl -H "Accept-Encoding: gzip,deflate" "$URL" --silent --write-out "%{size_download}" --output /dev/null curl "$URL" --silent --write-out "%{size_download}" --output /dev/null # 2nd must be greater the 1st
-
∞ Script that tests if a given value is a positive integer
#!/bin/bash VALUE=3 if [[ $VALUE =~ ^[0-9]+$ ]]; then echo "$VALUE is an integer" else echo "$VALUE is not an integer" fi
-
∞ Simple Python HTTP Server
python -m SimpleHTTPServer
-
∞ Script that determines the MIME type of a file
#!/bin/bash ########################################################################## # Title : getmimetype - determine MIME type of file # Author : Heiner Steven <heiner.steven@odn.de> # Date : 1999-08-24 # Requires : [ngm]awk # Category : Mail # SCCS-Id. : @(#) getmimetype 1.7 06/07/19 ########################################################################## PN=`basename "$0"` # Program name VER='1.7' : ${LOCALLIB:=/usr/local/lib} # We need a new version of AWK, i.e. "nawk" or "gawk" awk= for path in `echo "$PATH" | sed 's/^:/.:/;s/:$/:./;s/:/ /g'` do for p in mawk gawk nawk do [ -x "$path/$p" ] || continue awk=$path/$p; break 2 done done : ${NAWK:=${awk:=awk}} Usage () { echo >&2 "$PN - determine MIME type of file, $VER usage: $PN file [file ...] Reads the files $LOCALLIB/mimetypes $HOME/.mimetypes" exit 1 } Msg () { for MsgLine do echo "$PN: $MsgLine" >&2 done } Fatal () { Msg "$@"; exit 1; } set -- `getopt h "$@"` [ $# -lt 1 ] && Usage # "getopt" detected an error while [ $# -gt 0 ] do case "$1" in --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # First file name esac shift done MimeTypes=${TMPDIR:=/tmp}/pmt$$ trap 'rm -f "$MimeTypes" >/dev/null 2>&1' 0 trap "exit 2" 1 2 3 15 > "$MimeTypes" for file in $LOCALLIB/mimetypes $HOME/.mimetypes do if [ -r "$file" ] then cat "$file" >> "$MimeTypes" fi done # Define some MIME type defaults cat <<-EOT >> "$MimeTypes" # MIME type File Extensions text/plain .*\.[tT][eE][xX][tT]$ .*\.[tT][xX][tT]$ text/html .*\.[hH][tT][mM]$ .*\.[hH][tT][mM][lL]$ image/jpeg .*\.[jJ][pP][eE][gG]$ .*\.[jJ][pP][gG]$ image/gif .*\.[gG][iI][fF]$ image/png .*\.[pP][nN][gG]$ video/mpeg .*\.[mM][pP][eE][gG]$ .*\.[mM][pP][gG]$ video/avi .*\.[aA][vV][iI]$ application/postscript .*\.[pP][sS]$ application/pdf .*\.[pP][dD][fF]$ audio/basic .*\.[aA][uU]$ audio/wav .*\.[wW][aA][vV]$ audio/mp3 .*\.[mM][pP]3$ EOT for file do [ -f "$file" ] || continue if file "$file" | grep -i text >/dev/null then defaulttype=text/plain else defaulttype=application/octet-stream fi echo "$defaulttype $file" done | $NAWK ' BEGIN { # Read the MIME type table mimetypes = "'"$MimeTypes"'" while ( getline < mimetypes ) { if ( $1 ~ /^\#/ ) continue # ignore comments if ( $0 ~ /^[ ]*$/ ) continue # ignore empty lines if ( (k = split ($0, f)) > 1 ) { mimetype = f [1] for ( i=2; i<=k; i++ ) typeOf [$i] = mimetype } } close (mimetypes) #for ( i in typeOf ) print "<" i ">", typeOf [i] } { mimetype = "" for ( t in typeOf ) { if ( $0 ~ t ) { mimetype = typeOf [t] break } } if ( mimetype == "" ) mimetype = $1 print mimetype } '
-
∞ replace string in files
#!/bin/bash for file in *.groovy do echo "Traitement de $file ..." sed -e "s/string1/string2/g" "$file" > "$file".tmp && mv -f "$file".tmp "$file" done
-
∞ grep find
# grep find excluding .svn directories # from http://wordaligned.org/articles/ignoring-svn-directories alias gf='find . -path "*/.svn" -prune -o -type f -print0 | xargs -0 grep -I -n'


