This is a oneliner to copy files from current directory to another target directory using tar, preserving file attributes, dates etc.
#!/bin/sh fromdir=/path/from todir=/path/to/target echo "cd $fromdir; tar cf - . | (cd $todir; tar xpf -)" cd $fromdir; tar cf - . | (cd $todir; tar xpf -)
Posted by Hans-Henry Jakobsen
This is a simple bash script that is run by crontab every 5 minutes on a linux box.
It e-mails me the new address when a change of IP address is detected.
The script (ipchangemail.sh)
#!/bin/bash
# Check if IP-address has changed. If a change has occured, mail me the new address
# Add the following line to crontab if you would like it to be run every 5 minutes:
# */5 * * * * ./ipchangemail.sh
# The network interface I want to monitor
NET_INTERFACE=eth0
# File to keep the latest IP address
IP_FILE=myip.txt
# Mail to this address when a change occur
MAILTO=mail@example.com
# Read the previous IP address from file
source $IP_FILE
CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`
if [ "$CURRENT_IP" != "$OLD_IP" ]
then
# Send email about address change
`echo "New IP address detected: $CURRENT_IP" | mail -s "New IP address" $MAILTO`
# Write new address to file
`echo "OLD_IP=$CURRENT_IP" > $IP_FILE`
fi
The script can be downloaded here.
Tags: bash, ifconfig, mail, sed
Posted by Hans-Henry Jakobsen
This is a little oneliner to rename a files suffix from/to uppercase/lowercase.
Rename a jpg suffix to JPG in the current folder
# find -name "*.jpg" | while read a; do mv "$a" "${a%%.jpg}.JPG" ; done
The work JPG can be replaced by any other word :)
Tags: bash, lowercase, rename, uppercase
Posted by Hans-Henry Jakobsen
This is a short post on how you can “share” a bash session/prompt with other users.
# screen
:multiuser on
# screen -x
Serveral users can connect and share the same session at once.
To close the screen session just use the key combination <Ctrl + a>
Posted by Hans-Henry Jakobsen
This is a simple oneliner to determine which user has used most diskspace in their /home directory
du -sm $(find /home -type d -maxdepth 1 -xdev) | sort -g
The result could look something like this
... 215 /home/userT 1367 /home/userB 10865 /home/userL 25326 /home/userY 116328 /home/userH 154426 /home/
The numbers to the left is size i MB.
Tags: bash
Posted by Hans-Henry Jakobsen