//Browsing archives (Page 8 of 13)
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow


If you just want to find out what's new in a directory:

lsnew() { ls -lt ${1+"$@"} | head -20; }



View Comments »



I've long been frustrated by the lack of an interactive perl shell, where I can enter commands and see the results immediately. Yes, I have tried the perl debugger.

#!perl

use strict;
no strict 'vars';
no strict 'refs';
use warnings;

use Data::Dumper;
$Data::Dumper::Indent--;

$| = 1;

# an alias for exit()
sub quit { exit; }

my ($ver,$maj,$min) = ($] =~ /(\d+)\.(\d{3})(\d{3})/);
$maj += 0;
$min += 0;
print +(split '/', $^X)[-1], " $ver.$maj.$min\n";

$, = ',';
$THE_PROMPT = '% ';
print $THE_PROMPT;
while (<>) { print eval; print +($@ || "\n") . $THE_PROMPT };

Caveats:
- you have to enter complete commands on a single line
- because each 'eval'ed line is printed, if the result is undefined you'll get a "Use of uninitialized value" warning

Usage:
- naturally, I create a bash alias for the script
- this is cygwin on winxp, hence the "perl.exe" in the output below

$ alias perlsh='perl ~/bin/perlsh.pl'
$ perlsh
perl.exe 5.10.0
% @l=qw(the quick brown fox jumps over the lazy dog)
the,quick,brown,fox,jumps,over,the,lazy,dog
% @sorted_by_length = map {$_->[0]} sort {$a->[1] <=> $b->[1]} map {[$_, length]} @l
the,fox,the,dog,over,lazy,quick,brown,jumps


View Comments »

If you want to find all SSH enabled hosts in all subnets to which your computer is connected, you can use this oneliner:

nmap -p22 -oG - $(ip addr | awk '/inet .*global/ {print $2}' | tr '\n' ' ') | awk '/22\/open/ {print $2}' | uniq


View Comments »

Add "set -o vi" at the bottom of ~/.bashrc or /etc/bashrc, then press ESC in prompt to enter vi mode and search your issued command in history by giving a "/" at front.

For example, in shell command line, press ESC, type /ls, enter.


View Comments »

Serve files on port 8080 for anybody from the directory from where you start this command:

:;while [ $? -eq 0 ];do nc -vlp 8080 -c'(r=read;e=echo;$r a b c;z=$r;while [ ${#z} -gt 2 ];do $r z;done;f=`$e $b|sed 's/[^a-z0-9_.-]//gi'`;h="HTTP/1.0";o="$h 200 OK\r\n";c="Content";if [ -z $f ];then($e $o;ls|(while $r n;do if [ -f "$n" ]; then $e "<a href=\"/$n\">`ls -gh $n`
";fi;done););elif [ -f $f ];then $e "$o$c-Type: `file -ib $f`\n$c-Length: `stat -c%s $f`";$e;cat $f;else $e -e "$h 404 Not Found\n\n404\n";fi)';done


View Comments »

find . -name "*windows.template" | while read TEMPLATE ; do echo -e "\nText to Append=" >> $TEMPLATE ; done


View Comments »

[ $[ $RANDOM % 6 ] == 0 ] && echo Die || echo Live

Of course you can always replace "echo Die" by something a bit more interesting, kill a process perhaps or 'rm -rf' :)


View Comments »

wget -qO- http://cdimage.debian.org/debian-cd/4.0_r5/i386/iso-cd/debian-40r5-i386-netinst.iso | cdrecord

Straight from the net to my cd-burner!


View Comments »

I wanted a simple (accurate to the second) stop watch when I was at the command line. So I did a quick one-liner.

START=$( date +%s ); while true; do CURRENT=$( date +%s ) ; echo $(( CURRENT-START )) ; sleep 1 ; echo -n ^[[A ; done

Note that the strange symbol after the 'echo -n' is the result of pressing 'CTRL-v' then the up arrow. If you want it to give you a 'split' just hit enter and it'll move onto the next line. Remember it's only second accurate so only one split per second will work. Hit 'CTRL-c' to stop it.


View Comments »

How to generate list of numbers with equal width:
# seq -w 0 20

This command produces the following output:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20


View Comments »

CASE: You've relocated Subversion and Trac repositories to another machine/directories. You don't want to edit n+1 trac.ini files.

You have for example:
/repos/svn-myprojects/my-first-project
/repos/trac-myprojects/my-first-project

/repos/svn-myprojects/my-second-project
/repos/trac-myprojects/my-second-project

...etc...

Change ALL trac.ini repository_dir settings:

trac-mass-repodir-edit.sh:

#!/bin/bash TRACSPATH=$1 REPOPATH=$2 for i in $( find $TRACSPATH -maxdepth 1 -type d | grep -v "^$TRACSPATH\$" ); do BN=`basename $i` INIPATH=$i/conf/trac.ini TMP=$i/conf/trac.ini-temp # Replace repository_dir cat $INIPATH | perl -pe "s@repository_dir = .*@repository_dir = $REPOPATH/$BN@i" > $TMP mv $INIPATH $INIPATH-old && mv $TMP $INIPATH && rm $INIPATH-old done


Usage:
./trac-mass-repodir-edit.sh /repos/trac-myprojects /repos/svn-myprojects


Then you want to resync and upgrade all existing Tracs:

trac-mass-upgrade.sh:
#!/bin/bash

TRACSPATH=$1
REPOPATH=$2

for i in $( find $TRACSPATH -maxdepth 1 -type d | grep -v "^$TRACSPATH\$" ); do
  BN=`basename $i`
  # SVN directory exists
  if [ -d $REPOPATH/$BN ]; then
    echo "Processinc Trac: $BN.."
    trac-admin $i resync
    trac-admin $i upgrade
  fi
done


Usage:
./trac-mass-upgrade.sh /repos/trac-myprojects /repos/svn-myprojects


Now you have everything in order.


View Comments »

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.


View Comments »

Like <TAB> expands a filename given a prefix you can also expand a file name given the middle part of a file name using escape and then 'g'.

$ ls
abc1def  abc2def  abc3def  abc4def  abc5def
$ ls *2*<ESC>g


Which will expand to:

$ ls abc2def


View Comments »

If you want to repeat a command at set intervals, for example you are monitoring something, then you can use about the watch command. It will print the results of the command to screen every 2 seconds (you can change the interval with -n).

For example watching the disk space:

watch -n 10 df -h


View Comments »

Have you ever wanted to check the content of the first virtual console without switching to it with "Ctrl+Alt+F1"? Or the second console of a remote server? Or would you like to send something to the user working at the third virtual console (not via wall command)?

The GNU/Linux kernel provides two character devices for such tasks:

* /dev/ttyX - represents X. virtual console
* /dev/vcsX - represents X. virtual console text contents

So, to answer the questions use these commands:

1. cat /dev/vcs1
2. ssh root@server 'cat /dev/vcs2'
3. echo "something" > /dev/tty3


View Comments »

Works on OS X, Linux, anywhere with OpenSSL installed:

To encrypt a file:

openssl des3 -salt -in infile.txt -out encryptedfile.txt


To decrypt the file:

openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt




View Comments »

Check correct syntax and file format of '/etc/passwd' and users existence

pwck


Check correct syntax and file format of '/etc/group' and groups existence

grpck


View Comments »

Print the contents of a file from a given regular expression to another

sed -n '/start/,/end/ p' file

This will print the contents of the file from the line that matches /start/ until the line that matches /end/


View Comments »

You can create a wav file from a written text using one of these command

echo "It's such a beautiful day! Why are you in front of the computer?" | festival --tts

echo "It's such a beautiful day! Why are you in front of the computer?" | text2wave -scale 50 -o beautiful_day.wav

To use with a text file, just replace 'echo "..."' with 'cat filename'.


View Comments »

cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force


View Comments »

The m4 command is designed specifically for macro processing. m4 manipulates files, performs arithmetic, has functions for handling strings, and can do much more.

Here's a quick example of using m4 with templates:
$ cat temp
The COLOR fox jumped over the TYPE dog.


Were you to substitute the strings 'COLOR' by 'brown' and 'TYPE' by 'lazy', you could use:
$ m4 -DCOLOR=brown -DTYPE=lazy temp
The brown fox jumped over the lazy dog.


Else, if you want to substitute 'white' and 'big' for the same:
$ m4 -DCOLOR=white -DTYPE=big temp
The white fox jumped over the big dog.


View Comments »

To setup a simple reminder to appear on your screen at a set time you can use xmessage with the 'at' command:
echo 'xmessage "Hello"'|at 19:00

Alternatively to be alerted a set time later, use sleep:
sleep 8h; cat /dev/urandom > /dev/dsp


View Comments »

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.


View Comments »






Home Latest Browse Top 25 Random Hall Of Fame Contact Submit