Paul’s Linux Blog

Just Another Uber-Geek

Browsing Posts tagged Bash

Thanks to this great little script you no longer need to work out which program to use to uncompress multiple file archive formats.
Add this script to your .bashrc file and use as follows:
extract file.zip

 
extract () {
if [ -f $1 ] ; then
case $1 in
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.bz2)   <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvjf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.gz)    <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvzf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>.xz)    <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvJf $1    ;;
*.bz2)       bunzip2 $1     ;;
*.rar)       unrar x $1     ;;
*.gz)        gunzip $1      ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span>)       <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvf $1     ;;
*.tbz2)      <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvjf $1    ;;
*.tgz)       <span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">tar</span> xvzf $1    ;;
*.<span class="highlight" style="border: 1px solid gray; background: yellow none repeat scroll 0% 0%;">zip</span>)       unzip $1       ;;
*.Z)         uncompress $1  ;;
*.7z)        7z x $1        ;;
*.xz)        unxz $1        ;;
*.exe)       cabextract $1  ;;
*)           echo "\`$1': unrecognized file compression" ;;
esac
else
echo "\`$1' is not a valid file"
fi

You can use your Gmail account as a smart host to send all messages from your Linux computer.

You need to use a simple program called ssmtp. It accepts a mail stream on standard input with recipients specified on the command line and synchronously forwards the message to gmail.

This is great as all your messages look like they come from gmail and are saved in your sent items for reviewing later on.

Lets do the install.

Installing ssmtp

Type the following command under Debian / Ubuntu Linux: you could use yum under fedora.

 # apt-get update && apt-get install ssmtp

Configure gmail as a smarthost

Open /etc/ssmtp/ssmtp.conf, enter:

 # vi /etc/ssmtp/ssmtp.conf

Update file with the following to the end:

AuthUser=yourname@gmail.com
AuthPass=Your-Gmail-Password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

Removing any old mail agents

Disable any other mail agents you have installed sendmail or postfix are most common, if you are using them:

To find out if they are installed type:

 # dpkg -l |grep mail
 # dpkg -l |grep post

If none are found skip this section and test your send mail.

Now stop the mail deamon if one was found:

 # service sendmail stop
 # service postfix stop

I like to use chkconfig or sysvconfig under debian so you may need to use apt-get install for these packages:

chkconfig is from my fedora and redhat days it is a cmd line program

sysvconfig is similar to ntsysv under redhat fedora again but it is in a tui or text user interface.

NOTE: If you must use a gui then install BootUpManager aka "apt -get install bum"

 # apt-get update && apt-get install checkconfig
 # chkconfig sendmail off

This removes the link to sendmail (note you won't need to do this if it was not installed previously)

 # rm /usr/sbin/sendmail

In debian type

 # ln -s /usr/sbin/ssmtp /usr/sbin/sendmail

For other linux you may need to use:

 # ln -s /usr/local/ssmtp/sbin/ssmtp /usr/sbin/sendmail

Send a TEST Mail

Now, you can use mail / mailx command to send email messages. You can also write a shell script to backup your files and email to somewhere else (see below). You can test settings using following syntax:

 # echo "This is a test" | mail -s "Test" me@gmail.com

NOTE: If the mail command is not found then install mailx as follows, and substitute mail for mailx in the test:

 # apt-get install mailx

Told you it was quick and easy :)

 # cp /boot/grub/menu.lst /boot/grub/menu/lst.20090729

There are times when you want to be able to start, stop, restart, reload and even permantly stop  services from running.

I always run through my system services and ensure I have turned off all the services I do not wish to be running, this serves several good reasons:

1) Reduce's the amount of memory used.
2 Can make an increase in system performance.
3) Why run a service you don't need, that could be a security risk.
4) PC will boot faster.

How do I manage my services?

There are many ways to manage services on linux.

The services that start reside in the init.d and rc?.d folders in /etc.

To start and stop a service for example networking you would use:

 # sudo apt-get install pptp-linux
 # sudo /etc/init.d/networking stop
 # sudo /etc/init.d/networking start

Redhat uses these locations to start and stop services but also offers a shell command that can mange these services so they stay on or off permanently.

chkconfig - This command alows you to see all the services in a list and which of them are on or off in any runlevel.

NOTE:  A run level is the level the system is operating at this list is a guide to the levels not necessarily the setup that your linux uses.

1 - Single user mode
2 - Not used
3 - Full system no GUI
4 - Not used
5 - Full system with GUI

To find out what your default runlevel is you can look at the /etc/inittab file.

Using chkconfig

To see which services are configured as system services use:

 # sudo chkconfig --list

To turn off a service use:

 # sudo chkconfig --levels 345 sshd off

NTSYSV

Redhat also provides a command called ntsyv.

ntsysv - This is a text user interface that function as the above command.

It can be used to configure the running runlevel as follows:

 # sudo ntsysv

or for other runlevels use:

 # sudo ntsysv --level 345

What if I'm running Crunchbang.

In crunchbang these utilities are not installed, you will need to install them yourself if you wish to configure you services manually.

Use the following commands to install the above commands.

 # sudo apt-get install chkconfig

crunchbang does not have ntsysv but it does have a similar command called sysvconf.

 # sudo apt-get install sysvconf

I need a GUI not command line

If you really need that gui tool then use the humorously named BootUpManager aka BUM

 # apt-get install bum

This tool allows you to manage all your services as above but in a graphical way.

Remember when playing with services you need to be root, use sudo in front of all the commands before running them.

I have been using crunchbang for some time now and it seems sensible to perform an update.
After looking though several articles I notice there is no official way to perform the upgrade so I followed the Ubuntu upgrade path instead.

To upgrade crunchbang you will need an internet connection and some time., they recommend not doing the upgrade over an ssh connection but I am fearless so I did it against the warnings.

The first step is to make sure you have update-manager-core installed.

 sudo apt-get install update-manager-core

Then to perform the upgrade type:

 sudo do-release-upgrade

This start's the upgrade process, you will be asked a few questions before it starts to download the many packages and begin the install procedure this can take some time.

I left mine overnight, it did not help much as in the morning I noticed the upgrade was waiting for some manual input into what to do with some of the package configurations, before installing the next package in the list.

If you say yes you would like to keep the current configuration for example on grub, then when you reboot you will not see the new kernel as it has not been appended to the config, if you say use package mainainers config then you will lose any custom settings in your own one, most people would be ok with this but I had made a few changes such as adding vga=791 to the kernel line as I like my text on boot to be smaller and I like to remove the quiet boot option too. (Note this does affect the boot graphics, until the Login box appears)

The best thing to do would be open another terminator / console and make a backup of the file then accept the new config. It is a bit slower but safer.

 # cp /boot/grub/menu.lst /boot/grub/menu/lst.20090729

Let me know if it works for you.

I found myself today needing to move some files around but unfortunately they had spaces in them and would not move as expected.

A quick solution soon found using the infamous 'FIND' command and some examples.

the most basic kind of search is as follows:

 find . -name "*.avi" -print

It quite simply finds all files the have .avi on the end.

However sometimes you need a bit more, say you want to see the file sizes for these files:

 find . -name "*.avi" -exec ls -l {} ;

Now we should get the file attributes for each file found, note the {} expand during the execution of the script and are replaced by the filenames found.

Lets say we want to move these files into the same directory, but some files we don't want to move, we could use the following command for this:

 find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec cp -v {} /usb01/ ;

The command in this example says find in directory "/" files with .avi AND (-a) NOT (!) Clip or clock, then execute the copy command (cp) filename into /usb01/

The next part is to remove original files, this could all be done on the same command line but I like to check things have worked before committing to a delete.

 find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec rm -v {} ;

496 find / -name "*.avi" -exec file {} ;

I also cam across the following command that search the path ie the directory name as well, I have not used it yet but thought I would add it.

You can match the entire filename with -path instead of -name.

 
find . -path '*/139.P/*smooth' -exec mv "{}" destination ;

Hope this explains some of the mysteries of the find utility.

Powered by WordPress Web Design by SRS Solutions © 2010 Paul’s Linux Blog Design by SRS Solutions