//Browsing archives (Page 7 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

$HOST=;for((port=1;port<=65535;++port));do echo -en "$port ";if echo -en "open $HOST $port\nlogout\quit" | telnet 2>/dev/null | grep 'Connected to' > /dev/null;then echo -en "\n\nport $port/tcp is open\n\n";fi;done

View Comments »

Selective content replace on files. For example to replace '<?' with '<?php' in all PHP files:
find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

View Comments »

# by dj.r4iden
echo "Your ip Address is" `lynx --source http://www.formyip.com/ |grep The | awk {'print $5'}`

View Comments »

Use find by time to delete files more than x days old. For example the command below will delete files more than one day old:

find . -mtime +1 -exec rm {} \;

View Comments »

Seen on http://www.splitbrain.org/blog/2008-02/27-keeping_your_home_directory_organized

Having one temporary directory for downloads etc can quickly become a mess, so try this tip to organise by date. Put this into your .bashrc

export TD="$HOME/temp/`date +'%Y-%m-%d'`"
if [ ! -z "$1" ]; then
td="$HOME/temp/`date -d "$1 days" +'%Y-%m-%d'`";
mkdir -p $td; cd $td
unset td

You'll receive a new function 'td' for making temp-dirs under $HOME/temp/ named by date. It is also possible to switch days back with 'td -[n]', i. e. 'td -3' which means '3 days back from now'.

View Comments »

Tired to get your mailbox full of cron information messages?
Eg.: some programs (pg_dump, /etc/init.d/xxx, ...) output informational messages on stderr, but you can't close stderr because it may also contain failure informations.


LOG="/tmp/myscript-`date +%s`.tmp" IGNORE='^(Username: Password:|)[[:space:]]*$' # message to ignore, for demonstration only exec 3<&2; exec 2>$LOG # duplicate stderr for later use; redirect stderr to $LOG file
# insert your code (eg.: echo -e "login\npassword"|pg_dump -u user > file.dump)
exec 2<&3 # restore stderr # if $LOG file is not empty and contain anything else than what should be ignored, output it to stderr (for demonstration purpose) [ -s $LOG ] && egrep -vq $IGNORE $LOG && egrep -v $IGNORE $LOG >&2 rm -f $LOG

View Comments »

SSH_COMPLETE=( $(cat ~/.ssh/known_hosts | \
                 cut -f 1 -d ' ' | \
                 sed -e s/,.*//g | \
                 uniq | \
                 egrep -v [0123456789]) )
complete -o default -W "${SSH_COMPLETE[*]}" ssh

View Comments »

A simple shell script to get the latest stable version of the linux kernel:


kernelV=`finger finger@kernel.org | grep 'stable version' | awk '{print $NF}'`
wget -c http://www.kernel.org/pub/linux/kernel/v2.6/linux-$kernelV.tar.bz2

View Comments »

It's for removing those orphaned config files! :D

aptitude search ~c | awk '{ print $2 }' | xargs aptitude -y purge

View Comments »

for file in `ls *.tar.gz`;do tar -xvf $file;done

for file in `ls *.tar.bz2`;do tar -xvjf $file;done

View Comments »

Assign random names to all files in a folder (including subfolders!):

find . -type f -exec bash -c 'mv "$1" "./$RANDOM"' - {} \;

Note that this is a somewhat expensive operation, so it might take a few seconds for large numbers of files.

View Comments »

Often it can be handy to have a few files to test another script on. A quick way to do this is to create some empty files with random names:

for ((  i = 0 ;  i < 12;  i++  )); do touch $RANDOM; done

Just change the number (12) to give the required number of files.

View Comments »

If you work on remote boxes a lot, you ll probably need a to backup a directory from there onto your machine sooner or later.. This can easilly be done using ssh & tar; all in 1 line of (script friendly) code:

ssh user@server "cd /foo && tar cvz * --exclude=bar*" > backup.$( date +%y%m%d ).tgz

Substitute user & server by the relevant info; /foo by the map you need and bar with the stuff you dont need; execute the command; login to the server when asked & Blamo! You ll have a nice backup.date.tgz file on your machine :)

View Comments »

This is a script that I recently wrote to find the logs of my IDP that were over 90 days old and delete them.

The script is pretty much self explanatory.

## Lets find the log directories that are over 90 Days old.
find /var/netscreen/DevSvr/logs -mtime +90 -type d -prune -print > /root/scripts/files-deleted

## Now let's log the directories we are going to delete.
touch /root/scripts/deletelog

## We want to know when the files were deleted so we will append that to the log file.
date >> /root/scripts/deletelog

## Now we go through and delete the directories that are over 90 days old
## and log the directories that we delete to the log file.
for FILE in `cat /root/scripts/files-deleted`
echo Deleting $FILE ... >> /root/scripts/deletelog
rm -Rf $FILE

## Finally we will email a copy of the log file to the admin for his/(or her) review.
cat /root/scripts/deletelog | mail -s "90 Day IDP log cleanup" youremailaddress@domian.com

View Comments »

This a dialog script that asks the user a couple of questions and then does something with the answers.

It was something quick and dirty to accomplish a task I needed.

# This piece asks a question and stores the answer in a buffer file.
dialog --inputbox "Enter the hostname or IP of the SCS device you want to connect to..." 10 60 2>/home/SCS/out/server

# This was another piece of information that I needed to accomplish the task I needed.
dialog --inputbox "Enter the port on the SCS device that you want to connect to" 10 60 2>/home/SCS/out/port

# Here is where i set my variables with the buffer files from above.
SERVER=`cat /home/SCS/out/server`
PORT=`cat /home/SCS/out/port`

# This was just a piece to help me make sure my variable were set right.
# I often write in escape route in my scripts so that i can test them without blowing stuff up.
echo "You will now be transported to $SERVER on port $PORT."

# Here I give myself 5 seconds to ctrl +c the script.
sleep 5

#and finally the action to take with the answers from above.
ssh $SERVER -t -t connect $PORT

View Comments »

If you need to change your wireless MAC address (I had to do this when I used another computer to try to fix mine on a network with MAC filtering) this command comes in handy.

Pre 10.5(I know this works in Tiger but it should work 10.3 up):
sudo ifconfig en0 ether 00:00:00:00:00:0

sudo ifconfig en0 lladdr 00:00:00:00:00:00

just switch 00:00:00:00:00 with whatever mac address you want to pretend to be.

View Comments »

From dotfiles.org; original author unknown:

###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1        ;;
            *.tar.gz)    tar xvzf $1     ;;
            *.bz2)       bunzip2 $1       ;;
            *.rar)       unrar x $1     ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xvf $1        ;;
            *.tbz2)      tar xvjf $1      ;;
            *.tgz)       tar xvzf $1       ;;
            *.zip)       unzip $1     ;;
            *.Z)         uncompress $1  ;;
            *.7z)        7z x $1    ;;
            *)           echo "'$1' cannot be extracted via >extract<" ;;
        echo "'$1' is not a valid file"

View Comments »

I'm frequently logged into multiple different boxes concurrently. To help me keep track of which box I'm working on at the moment, I have this in my .bashrc file, which I keep rsynced across the various boxes.

# defines the color variables used below, including ${Normal} # google "tput setaf" and "tput setab" for more information . ~/.colors.sh if [ `id -u` -eq 0 ]; then Color=${RedBG}${White} # red background for root else case `hostname` in machine1 ) Color=${Blue} ;; machine2 ) Color=${Green} ;; machine3 ) Color=${Cyan} ;; machine4 ) Color=${Purple} ;; * ) Color=${Yellow} ;; esac fi PS1="${Color}$PS1${Normal}\n> "


Black="$(tput setaf 0)"
BlackBG="$(tput setab 0)"
DarkGrey="$(tput bold ; tput setaf 0)"
LightGrey="$(tput setaf 7)"
LightGreyBG="$(tput setab 7)"
White="$(tput bold ; tput setaf 7)"
Red="$(tput setaf 1)"
RedBG="$(tput setab 1)"
LightRed="$(tput bold ; tput setaf 1)"
Green="$(tput setaf 2)"
GreenBG="$(tput setab 2)"
LightGreen="$(tput bold ; tput setaf 2)"
Brown="$(tput setaf 3)"
BrownBG="$(tput setab 3)"
Yellow="$(tput bold ; tput setaf 3)"
Blue="$(tput setaf 4)"
BlueBG="$(tput setab 4)"
LightBlue="$(tput bold ; tput setaf 4)"
Purple="$(tput setaf 5)"
PurpleBG="$(tput setab 5)"
Pink="$(tput bold ; tput setaf 5)"
Cyan="$(tput setaf 6)"
CyanBG="$(tput setab 6)"
LightCyan="$(tput bold ; tput setaf 6)"
Normal="$(tput sgr0)" # No Color

View Comments »

Try changing the following in your ~/.inputrc to alter the behaviour of auto completion on application/file names:

set completion-ignore-case on
- performs filename matching and completion in a case-insensitive fashion. The default value is 'off'.

set completion-query-items 200
- number of possible completions that determines when the user is asked if they want to see the list of possibilities. The default limit is 100.

- if set to 'on', words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell. The default value is 'off'.

- if set to 'on', a character denoting a file's type is appended to the filename when listing possible completions. The default is 'off'.

View Comments »

tab completion is something we use all day long. put this into your .bash_profile. You can use tabs to auto complete the hostname for ssh calls.

complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh

View Comments »

Go to [http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl] and search for your location. Copy the link to the 3 day forecast feed, for example [http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml]. Then just put that link into the following command to get a quick command line weather forecast.

wget -q -O - http://feeds.bbc.co.uk/weather/.../XXXXX.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"

$ wget -q -O - http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"
BBC - Weather Centre - Forecast for San Francisco, United States of America Tuesday: sunny, Max Temp: 22C (72F), Min Temp: 13C (55F) Wednesday: sunny, Max Temp: 22C (72F), Min Temp: 15C (59F) Thursday: sunny, Max Temp: 25C (77F), Min Temp: 14C (57F)

View Comments »

echo Bef = $((`cat /home/$USER/.bash_history | sort | uniq | wc -l`*100/`cat /home/$USER/.bash_history | wc -l`))%

View Comments »

To unpack an Debian packages without installing or configuring it:

ar -x package.deb

Gives "control.tar.gz" and "data.tar.gz". The data which would be installed is in data.tar.gz.

View Comments »

Simply convert .nrg (Nero format) images to .iso format images with dd:
Nero writes a 300kb header onto the iso, so dropping the first 300kb results in a .iso format image.

dd bs=1k if=NRG_FILE of=ISO_FILE skip=300

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit