Tuesday, October 20, 2009

Linux power shell commands and tricks: Part I

A lot comes bundled with Linux OS these days (thanks to the FOSS revolution) and we've zillions of utils and UI's to choose from to adapt to the Linux way of life in software development!

Still, Linux encourages the UNIX thought process and the philosophy of DIY (do-it-yourself)...
A power user (developer, build engineer, admin) would programmatically configure (read: automate) his environment and do more with the console using the plain old Linux "shell"

This series (to continue in parts) is on those power shell utilities (command combinations, scripts, performance tuning tweaks) that would help a user do more with less!!!

To start with, here I post a collection of such things that I've found useful:

1) A script (collection of commands taken from Scott Croft's Oracle Tuning on RHEL tweaks) to make your own network-configuration file (using static IPv4 address).

Note:
a) Need root level or sudo access to be able to run this script
b) Tested on RHEL and Fedora.

Takeaway: you'll have a Linux machine configured to communicate over an IPv4 network, with a static IP configuration.

You can copy-paste the below commands in an ASCII text file (with a .sh extension) and create a re-usable script or you can run the commands step by step:

HOSTNAME=$(grep HOSTNAME /etc/sysconfig/network|awk -F= '{print $2}')
hostname $HOSTNAME
GATEWAY=$(ip route list |grep default |awk '{print $3}')
echo "GATEWAY=$GATEWAY" >> /etc/sysconfig/network
DEFDEV=$(ip route list|grep default|awk '{FS=" "; print $5}')
IPADDR=$(ip addr show $DEFDEV |grep inet |grep -v inet6|awk '{print $2}'|awk -F/ '{print $1}')
echo "IPADDR=$IPADDR" >> /etc/sysconfig/network-scripts/ifcfg-$DEFDEV
sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-$DEFDEV
BCAST=$(ip addr show eth0 |grep inet |grep -v inet6|awk '{print $4}')
echo "BROADCAST=$BCAST" >> /etc/sysconfig/network-scripts/ifcfg-$DEFDEV
echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-$DEFDEV

2) Usage of "xargs", "exec" and pipe to do-more-with-less:

a) To add a new extension to all files in a dir:
ls | xargs -t -i mv {} {}.old

xargs reads each item from the ls ouput and executes the mv command.
The -i option tells xargs to replace {} with the name of each item. The -t option instructs xargs to print the command before executing it.

b) To rename a subset of files, specify the file names with the ls command:
ls *.log | xargs -t -i mv {} {}.old

c) To add a current timestamp extension:
ls *.log | xargs -t -i mv {} {}.`date +%F-%H:%M:%S`

The extension will look like ".2009-10-20-19:37:16"

d) To rename just the extension of files:
rename .log .log_archive.`date +%F-%H:%M:%S` *

This command replaces the first occurrence of .log in the name
by .log_archive.`date +%F-%H:%M:%S`

e) Find out exactly which files are eating up a big chunk of your disk space:
To find all files over 10,000KB (10MB+) in size and display their names, along with size,

find / -size +10000k | xargs -i ls -lh {}
or
find / -type f -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
or, in some other Linux variants:
find / -type f -size +10000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

e.g.
# find /var/ -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
/var/lib/rpm/Packages: 69M
/var/lib/rpm/Basenames: 11M
/var/lib/rpm/Filedigests: 11M
/var/cache/yum/updates/filelists.xml.gz.sqlite: 57M
/var/cache/yum/updates/primary.xml.gz.sqlite: 21M
/var/cache/yum/fedora/filelists.xml.gz.sqlite: 14M
/var/cache/yum/everything/filelists.xml.gz.sqlite: 59M
/var/cache/yum/everything/primary.xml.gz.sqlite: 29M

...topics coming up next in Part II: memory, process, performance tuning and more!

2 comments:

Anonymous said...

waiting for next part...

Anonymous said...

The mentioned commands are very useful in realtime.Expecting the same for "memory, process, performance" sa most of the devices are running in UNIX based.

Thanks in advance.