Inspiration & Genius – One and the Same

Some more command line shell fu for ya!

Remove Empty Directories:

Remove all empty directories within the current directory

find . -type d -empty -exec rmdir {} ;

Or another way to do it:

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"

Create File Dumps

This script displays the contents of files (or stdin) in ascii, hexadecimal, decimal, octal, and binary formats.

#!/usr/bin/perl

undef $/; # slurp files
while( $content = <> ) {
$offset = 0;
print "OFFSET ASC HEX DEC OCT BINn";
while( length $content ) {
$n = ord( substr( $content, 0, 1, '' ) );
printf "%08x %c %2x %3u %3o %sn"
, $offset,
, ( $n > 0x1f && $n < 0x7f ) ? $n : ord '.',
, $n,
, $n,
, $n,
, substr( unpack( "B*", pack( "n", $n ) ), -8 )
;
$offset++;
}
}

Moving Large Directories

Why not use cp or mv to move /usr/home to /storage/export/home? Weird things happen to hard and softlinks when you mv or cp. Try it and remember that a mv between different filesystems is actually a copy and delete.

Try this instead:

# tar -C /usr -cf - home | tar -C /storage/export -xvf -

Or, to copy to a remote machine:

# tar -C /usr -cf - home | ssh user@somemachine tar -C /storage/export -xvf -

You may want to add the -z switch to the tar commands. It will add compression but it depends on the type of data and your connection speed if it really improves transfer speeds.

Transfer Files With Netcat

On client machine

nc -lpvv port > file

On server machine

nc -vv clientip port < file

Example :

Client: nc -l -p 6868 > file.txt
Server: nc 31.41.59.26 6868 < file.txt

Diff Two Directories

A quick script to compare files from two directories (for example a backup and working directory).

#!/bin/bash

cr='*'
if [ -z $3 ]; then cr=$3; fi

for f in `find $1/$3 -type f | sed "s|$1/||"`
do
printf "===!%-76s" "$f!" | tr ' !' '= '; echo
diff $1/$f $2/$f | sed -e "s/^</$1: /" -e "s/^>/$2: /"
done

Usage: [script name] directory1 directory2 to check all files
[script name] directory1 directory2 *html to check files of type html.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Genesis Framework

Genesis Framework for WordPress

Studiopress Themes

Scribble Theme - A Beautiful Frame For Your WordPress Website

Advertisements