Showing posts with label commandline. Show all posts
Showing posts with label commandline. Show all posts

Sunday, 6 September 2009

Howto: Reclaim hard disk space from the system.


Before I went on holiday last week I had an internal drive start to fail, so a quick look at Ebuyer and a new Samsung Ecodrive F2 1.5TB monster was delivered. I swapped out the drives and went on holiday. While I was away I realised that I had left the default setting on the drive for system reserved space. Almost 80GB was reserved by the system for logs and such which I feel is a tad excessive myself ;)

This works on ext2/3 partitions and from what I can tell and been advised this is fairly safe to do, I'm using "fairly safe" deliberately here, as I best understand it the reserved space is mainly for system logs, helps to prevent fragmentation and helps when a drive becomes 100% full.

Note: This is safe to do on drives containing data, no need to backup everything to perform it, no data should be lost, based on advice I received and my own results of course, I will not be held liable for your data or lack of backups in general.

I have set my storage drives to 0% reserved space, left my root drive at 5% space and my /home drive at 2% space (which should be around 3GB which is excessive I think but better safe than sorry). Change /dev/sda1 to match the drive you want to change!

sudo tune2fs -l /dev/sda1

Lists the drives settings (note that "-l" is a lower case L, in case of font issues), you can then see how many blocks are being reserved by the system, you can then adjust the default 5% that the system uses to something nicer), If you have a Nautilus open before issuing the command, notice how much free space is available (lower left corner of the window) then issue the command and refresh the Nautilus window, the result is instant extra space available to see and use.

sudo tune2fs -m 0 /dev/sda1

This will set it to 0%, adjust that "0" to a more suitable number if needed, I prefered to leave some space reserved on root and /home, drive space is cheap but I don't like the though of wasting too much, making adjustments on various drives and partitions recovers me a good chunk over 100GB!



Monday, 24 August 2009

A script to copy clipboard to a file.

While mucking about with some scripts I got a little bored of copying them into files and chmod +x and copying them into the path, so I just knocked up this little scriptlet. It will optionally set the execute flag on the file if it is a script you are pasting, or leave it as a normal txt file for just saving some text.


First install the package xclip with either Synaptic or:
sudo apt-get install xclip

This isn't a great script, there is very little error checking and it won't even go out of its way to tell you if the syntax is wrong, luckily its easy enough and does what I wanted simply.

Syntax is :
clip2file -x filename
Copies the clipboard into a file and sets it to execute with chmod +x then movies it into ~/bin
clip2file -a filename
Copies the clipboard into a file and nothing more.

You should have already made a bin/ folder in your home (~/bin/) this I believe is already defined as in the $PATH on Ubuntu install.

make a new file in that bin folder called "clip2file" and open it in your favourite text editor such as nano, vim or gedit and past the following script into it.

#!/bin/bash
#
# Copy the contents of the X clipboard into
# the specified file.
#
# This makes it crap loads easier to make new commands from scripts.
#
# Perhaps make it automagically +x if the first script line is
# #/bin/*
#
#

hflag=
aflag=
xflag=

while getopts 'ha:x:' OPTION
do
case $OPTION in
h) hflag=1 ;;

a) aflag=1
aval="$OPTARG" ;;

x) xflag=1
xval="$OPTARG" ;;

?) printf "Usage: %s: [-x filename] [-a filename] or use -h to recieve help\n"
exit 1 ;;
esac
done

shift $(($OPTIND - 1))

if [ "$hflag" ]
then
printf "Clip2file provides an easy method to create a file from the X clipboard\n"
printf "as either a text file, or make the file executable as a bash script.\n\n"
printf "-x filename ......... Create the file, then set the execute bit.\n"
printf "-a filename ......... Create the file but do not set execute.\n\n"
exit 1
fi

if [ "$xflag" ]
then
xclip -selection clipboard -o > "$xval"
chmod +x "$xval"
mv "$xval" $HOME/bin/
fi

if [ "$aflag" ]
then
xclip -selection clipboard -o > "$aval"
fi

exit 0

Set the file to allow executing, with with chmod +x clipt2file or by right clicking it in nautilus and choosing Proerties then going to the Permissions tab and setting it to allow executing.

Now if you want to add a new script off a website, simply copy the text into the clipboard and in a terminal or run dialog (alt+F2) enter:

clip2file -x filename

Filename of course being the name to save the file too, you can then run your new script right away.

Please remeber this script isn't perfect but its good enough for what it does, if someone would like to expand on it then I would happily post the improved version with credit.

Sunday, 23 August 2009

Howto: SSH Public Key Authorisation Login (AKA Passwordless)

To be clear, this is how to login using SSH to another machine without having to enter your password. It is not an all encompassing solution but a "good enough" set up. I have to administer a couple of machines and a shell which I have use of, my own machine is kept up to date and I feel is secure enough with only myself as sole user.

Firstly we should make a key pair if you haven't already done so, these are deposited into into ~/.ssh

ssh-keygen -t rsa

Next we need to get the contents of ~/.ssh/id_rsa.pub onto our remote machine and into ~/.ssh/authorized_keys this can be done a couple ways, I found it as easy to just ssh into the remote machine and paste the contents directly into the file just note that if you paste it in you may have to correct the formatting so it is all on one line, I noticed a couple of line breaks sneak in. You could use something like the following however:

scp -P 20060 ~/.ssh/id_rsa.pub remoteaccount@remotesserver.com

Then you can just append it automatically with the following (we will touch the file in case it doesn't already exist on your machine:

touch ~/.ssh/authorized_keys
cat id_rsa.pub >> ~/.ssh/authorized_keys


We should at this point make the .ssh folder only readable by the owner:

chmod 700 ~/.ssh

You can optionally delete the id_rsa.pub key file if it was in your home folder, its the public key part so its not really necessary in my opinion but it is nice to tidy up after yourself ;)

rm id_rsa.pub

If you try and login now to the remote machine with SSH you should with any luck find you do not have to enter your password at all and can just get right on with your work.

Friday, 7 August 2009

How to use OTF fonts in Ubuntu

It isn't possible to use OTF fonts directly in Ubuntu without converting them into TTF's.

If we spend just a few minutes and install FontForge and make a very small script it becomes almost trivial to do.

sudo apt-get install fontforge

Now we should make the scripts, well it is two actually but one is a small config script for FontForge and the other is just a script to convert many OTF files in one go, rather than individually. First the FontForge settings file.

cd ~/bin
touch otf2ttf.conf

Then open otf2ttf.conf in your favourite text editor and paste the following into it.

#!/usr/local/bin/fontforge
# Quick and dirty hack: converts a font to truetype (.ttf)
Print("Opening "+$1);
Open($1);
Print("Saving "+$1:r+".ttf");
Generate($1:r+".ttf");
Quit(0);

Save the file, and now lets make the script to convert many fonts in one swoop.

cd ~/bin
touch otf2ttf
chmod +x otf2ttf

Again, open this file with your prefered text editor and paste the following in.

#!/bin/bash
#
# had to enable extglob
shopt -s extglob

# this uses globbing to match fiels ending in otf/OTF
for i in +(*.otf|*.OTF)

do fontforge -script /home/subbass/bin/otf2ttf.conf $i

done

To use this now (provided that your ~/bin folder is in your path) just open a terminal where your OTF files are, and issue the command otf2ttf. It will convert 20 files in just a couple of seconds to give you an idea of speed, you can then move all the resulting TTF files into ~/.fonts


Credit to http://www.stuermer.ch/blog/convert-otf-to-ttf-font-on-ubuntu.html where I found the script before re-typing the instructions. I put it here so I don't lose it next time I need it.

Wednesday, 5 August 2009

Howto Create Split RAR Files

Lets learn how to create split Rar files

If you have a large amount of data to backup or especially to transfer it can be very prudent to use an archive that can be split into smaller parts, if one section becomes corrupted during transfer, it is far better to transfer a single 15mb file again, than an entire 10GB file, right?

Lets grab the rar program:

sudo apt-get install rar

Ok lets compress our directory of files:

To compress file(s) to split rar archive know which directory you want to compress, I'll use a fictional DVD Image folder in the home folder.

rar a -m5 -v5M -R myarchive /home/yourname/dvdimage

Let me break the above command down

rar – starts the program
a – tells program to add files to the archive
-m5 – determine the compression level (0-store (fast)…3-default…5-maximum(slow))
-v15M – determine the size of each file in split archive, in this example you get files with size 15MB (if you wanted files of 512kB size you would write -v512k)
myarchive – name of the archive you are creating
/home/yourname/dvdimage – is folder of the files you wish to add to the archive

You can also add -p to the command after a and it will prompt you for a password.

You can read the manual for more options with man rar (Press q to exit and arrows to scroll up/down)

To uncompress the archive type:

rar x myarchive.part01.rar

Or right click on file myarchive.part01.rar in Nautilus and choose Extract Here.




Friday, 24 July 2009

Bulk rotate images using EXIF data

Using a simple command line command it is very simple to rotate any number of images using the exif data (hopefully) contained in the image. Most cameras will save the exif data and include the camera rotation when the photograph was taken.

sudo apt-get install jhead

then when we run the following command in the folder it will losslessly rotate the photographs that need it.

jhead -autorot *

Job done :]

Tuesday, 14 July 2009

Set up rxvt terminal with unicode and clickable URL's

Rxvt is a super fast unicode terminal which I have recently switched to away from Eterm for my transparent desktop terminal.

sudo apt-get install rxvt-unicode-ml

Next is to create a configuration file for it in your home folder.

touch .Xdefaults

Now paste this lot in...

URxvt*termName: rxvt

## borderless and no scrollbar
URxvt*scrollBar_right: false
URxvt*scrollBar: false
URxvt*borderLess: false

## teh transparency stuff
URxvt*inheritPixmap: true
URxvt*tintColor: white
URxvt*shading: 100

##
## These 3 lines make links clickable
##
URxvt.urlLauncher: firefox
URxvt.matcher.button: 1
URxvt.perl-ext-common: matcher,readline

## geometry and font
URxvt*geometry: 80×15
URxvt*font: xft:Terminus:pixelsize=12

## change default colors
URxvt*background: #000000
URxvt*foreground: #A8A8A8
URxvt*color0: #000000
URxvt*color1: #A80000

## URxvt*color2: #00A800
URxvt*color2: #ED254F

URxvt*color3: #A85400
## main bars in irssi
URxvt*color4: #020202
URxvt*color5: #A800A8
URxvt*color6: #00A8A8
URxvt*color7: #A8A8A8
URxvt*color8: #545054
URxvt*color9: #F85450

## Time in Irssi
URxvt*color10: #ED254F
## URxvt*color10: #50FC50

## URxvt*color11: #F2FC50
URxvt*color11: #ED254F

URxvt*color12: #5054F8

URxvt*color13: #ED254F
## URxvt*color13: #F854F8

URxvt*color14: #50FCF8
URxvt*color15: #F8FCF8

Note that some of the colours have been changed to coordinate with my current desktop theme, the original colours are commented out so you can easily revert. Also the line defining the window geometry seems to have no effect, I shall look into that and correct it when I get some time and inclination. At the moment I don't mind because the default window size is fine, and the launcher I use to start the transparent terminal I use on the desktop mainly for my screen with irssi, hellanzb and mediatomb in has the geometry set on it. the launcher command is:


rxvt -g 80x15

Last job is to tell compiz to not decorate this window, this will get rid of the title bar, borders and shadow etc.

Hopefully you have installed the advanced compiz settings (ccsm), so just open that up and go to the section for window decorations
in Effects, Window Decorations.

Add the rxvt window to be excluded from shadows and decorations, and that should see you done.



Wednesday, 8 July 2009

Control+Alt+Backspace shortcut does not restart the X server

Control+Alt+Backspace shortcut does not restart the X server

Edit: I believe this was removed because the kernal already contained another combo that does the same job, Alt+SysReq+k will terminate and restart X if it should become unresponsive.

If you would prefer the old key combination then issue the following two commands in a terminal. I would advise against this though, but as always, the choice is entirely yours.

sudo apt-get install dontzap
then do:
sudo dontzap –disable



Monday, 22 June 2009

Install Samba Server on Ubuntu

Install Samba Server on Ubuntu


If you want to share files between your Ubuntu and Windows computers, your best option is to use Samba file sharing.


To install, first open a terminal window and enter the following command:


sudo apt-get install samba smbfs


We’ve got samba installed, but now we’ll need to configure it to make it accessible. Run the following command to open the configuration file, substituting your editor of choice:


sudo gedit /etc/samba/smb.conf


Find this section in the file:


####### Authentication #######

# “security = user” is always a good idea. This will require a Unix account
# in this server for every user accessing the server. See
# /usr/share/doc/samba-doc/htmldocs/Samba-HOWTO-Collection/ServerType.html
# in the samba-doc package for details.
; security = user


Uncomment the security line, and add another line to make it look like this:


security = user
username map = /etc/samba/smbusers


This will set Samba to use the smbusers file for looking up the user list.

Create a Samba User

There are two steps to creating a user. First we’ll add that username to the smbusers file.

sudo gedit /etc/samba/smbusers


Add in the following line, substituting the username with the one you want to give access to. The format is ubuntu-usuername = “username”. You can use a different samba user name to map to an ubuntu account, but that’s not really necessary right now.

joe = “joe”


Next, we’ll run the smbpasswd utility to create a samba password for the user.


sudo smbpasswd -a


Now you can create samba shares by right clicking in Nautilus and defining the share in properties/sharing.


Monday, 15 June 2009


How to read 20 magazines for free.


You will need Firefox and an addon called User agent Switcher and configure it with an iPhone setting :

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419 (United States)

You can then visit http://zinio.com/iphone/ and read 20 magazines each month for free.

It gets better though, it is very simple to download these pages onto your computer using Linux, aren't you glad you dumped windows ;)

We now need the URL to the image so "copy image location" from the right click menu then using this command:

curl -O -A "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3" INSERTURL

Replace INSERTURL with the copied link you got to page 1, then delete back to the underscore and append [1-300].jpg this is a range option to grab pages 1 to 300 and can be adjusted, so the final command will look like this:

curl -O -A "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3" http://imgs.zinio.com/iphone/issues/416078746/[1-300].jpg

The files will need to be renamed, as sadly they won't sort correctly if you are going to do the next step of combining them into a single pdf. We need the jpgs numbering from 001.jpg so they order correctly. this can be achieved in a variety of ways, using Thunar rename to insert the necessary 0's in two stages is one simple way, insert 00 at position 10 for number 1-9, and insert 0 into postion 10 for pages 10-99. it can also be scripted in a couple of steps with :

for i in `seq 1 9`; do mv *_$i.jpg 00$i.jpg ;done

for i in `seq 10 99`; do mv *_$i.jpg 0$i.jpg ;done

This could of course be scripted into one single command using a bash script.

Then we can simply issue the command:

convert *.jpg magazine.pdf

Thats it, you now have a local copy of the magazine in a handy PDF format relatively simply, it lokos more complex than it really is once you get your head around it.

Friday, 22 August 2008

Recurse to find files and move them to a location

I just had to move a bunch of files out of individual sub-folders into the parent folder, time consuming to do that 50 or so times so a quick check of the "find" command and the solution is here:

find . -iname '*.avi' -exec mv {} /home/subbass \;

Command breakdown looks like this:

find .
find "here"
-iname '*.avi'
case insensitive name match on *.avi
-exec mv
execute the move command on the matches
{}
the match result from find
/home/subbass/
path to move the files to
\;
end of the -exec, each match runs as a new command

A quick command line, fairly simple syntax and it should prove a great time saver. You could easier of course cp or rm files instead of mv or many other possibilities.