Categories
Command Line HowTo Linux Ubuntu

HowTo: Restore Your Default .bashrc File in Ubuntu

If you’ve managed to find yourself deleting, overwriting or generally messing up your profiles .bashrc file there’s a really easy way to fix it. A default version of the file can be found under “/etc/skel/.bashrc”, simply coping that over your profiles one (found in your home directory) will restore it to default. Make sure to also source the file if you want to continue using it under that terminal or close and re-open the terminal.

I managed to have a blank bashrc after upgrading Ubuntu with a separate /home partition, while deleting all the old config files I must have also removed this.

The following commands should do this for you, be sure to make a back up of your current .bashrc file too, just to be safe.

cp ~/.bashrc ~/.bashrc.bkup
cp /etc/skel/.bashrc ~/
source .bashrc

You should be now back to a default terminal experience and continue to customise it how you like!

Categories
Blog Command Line Featured Linux

10 Command Line Tricks I Wish I’d Always Known

One thing that Linux Guru’s and Terminal Wizards often argue is that it’s way quicker to use the command line to do anything in Linux than it is through menus and what not. While the jury is still out on this, there are a lot of things they know which do help put the ball in their court which can often take a lot of time exploring the terminal to discover. I’ve compiled a list of my favourite commands and tricks that I think that would have saved me a bunch of time if I’d been aware of them when I first ventured in to Linux.

1. Repeat Recent Commands:

Let’s say you’ve just ran a command but forgot you needed to run it as sudo, you can use !! to repeat it with the additional sudo at the beginning, as below:

alex:~$ apt-get install package
alex:~$ sudo !!
sudo apt-get install package

Additionally you’re able to use this to go back further than the last command by replacing the second ! for -n (where n is the number of commands ago you want to repeat).

alex:~$ ls
alex:~$ cd dir
alex:~$ cat file
alex:~$ !-3
ls

How about if you want to reuse the arguments from the last command but not the command itself, well it can do that too:

alex:~$ ls /home/alex/Desktop
alex:~$ cd !*
cd /home/alex/Desktop

2. Replace String In Last Command:

The use of the following command allows you to repeat the last command but replacing a string within it, useful if you made a mistake or typo. The follow example shows correcting a path for a change directory command.

alex:~$ cd /this/dir/is/wrong
alex:~$ ^wrong^right^
cd /this/dir/is/right

3. Reset:

Chances are, you’ll come across a situation where you’ve seemingly borked a terminal. Maybe you used cat on the wrong file or a program tried to export some strange characters which changed the settings. The following command should get you back to a fresh terminal quickly and you can often run it even when you don’t think you can. If the screen is really borked, try hitting enter before hand, typing reset and hitting enter again.

alex:~$ reset

4. Running A Command In The Background:

Adding an & after the command you run allows you to continue to use the terminal screen while keeping the process running. Below shows an example of opening gedit in the background but allowing me to continue using the terminal for other commands. Great for editing and compiling code with the minimal number of terminals open.

alex:~$ gedit &

Once ran it’ll give you the process ID which allows you to kill it easily once you’re done with it.

5. Exiting A Terminal & Keeping Background Processes Running:

If you’ve opened a bunch of processes in the background (as per previous command) but now you want to close the terminal screen but not these processes you can use the following to release the ownership of the processes and exit.

alex:~$ disown -a && exit

6. htop:

Similar to ‘top’ (which allows you to view tasks in real-time with other useful info such as memory and CPU usage) but it’s displayed in a much easier to understand and interactive. It allows you to kill tasks without entering its PID, search, filter, sort and a bunch of other features.

alex:~$ htop

7. Reverse History Search:

Ctrl + r

Using this will allow you to start typing and it’ll find the last command that contained the string you’re typing, hitting enter will execute that command. For example:

alex:~$ cd /home/alex/this/is/a/dir
ctrl+r & typing "a/dir"
(reverse-i-search)`': cd /home/alex/this/is/a/dir

8. Piping (Combining) Commands:

Piping commands allows you to pass the data that would usually be outputted to be directly passed as an input to another command, for example below is a list command being passed in to the less command to allow for scrolling of the data. This will work for most commands and experimentation is the best form of learning in this case.

alex:~$ ls | less

9. Auto Complete Command & File Names:

Half way through typing a command you can hit the Tab key and it’ll auto complete the word for you, if there are more than one other possible combinations a double tap of it will show you the possibilities. This also works for directory and files and can save some serious typing time.

10. Other useful Keyboard Shortcuts:

A few other keyboard shortcuts that are also useful to know (and not always as obvious as they should be):

Ctrl + c – kill current process
Ctrl + z – put current process in to the background
Ctrl + l – clear the terminal
Ctrl + a – set cursor to the start of the line
Ctrl + e – set cursor to the end of the line
Ctrl + shift + c – copy
Ctrl + shift + c – paste

Categories
Arch Linux Blog Command Line Fedora Linux Posts Ubuntu

Switching Between Two Resolutions in Linux

I have a TV and my monitor connected to my PC by a VGA switch box but what’s annoying is they aren’t the same resolution. This causes problems because I can’t see what I’m doing when I have switched to TV and usually have to leave the setting manager open so that when I change between them I just have to hit return and the resolution changes. That’s great and all but it’s far more effort than I want for something I do fairly often.

My plan was initially to create two xorg.conf files and switch between them using a script, I figured this would be the easiest way even though I haven’t messed around with xorg stuff for a few years ever since things just started working better in Linux. Turns out I couldn’t even find where they keep the xorg.conf file, it sure isn’t in /etc/X11 where it was the last time I looked..

On the search for this file though I came across another useful tool called xrandr, which essentially allows you to change the resolution from the command line.

Xrandr is used to set the size, orientation and/or reflection of the outputs for a
screen. It can also set the screen size.

If invoked without any option, it will dump the state of the outputs, showing the
existing modes for each of them, with a ‘+’ after the preferred mode and a ‘*’
after the current mode.

There are a few global options. Other options modify the last output that is spec‐
ified in earlier parameters in the command line. Multiple outputs may be modified
at the same time by passing multiple –output options followed immediately by
their corresponding modifying options.

For more information on xrandr check out the manual page (or type man xrandr in terminal).

It turns out it’s an extremely easy tool to use, with a command as simple as the following changing the resolution:

xrandr --output VGA1 --mode 1440x900

So the next thing I did was create the following script which allows the resolution to switch between 1440×900 and 1360×768 (my monitor and my TV native resolutions).

#!/bin/bash

TV="1360 x 768"
MONITOR="1440 x 900"

TEST="$(xrandr | grep current | sed -e 's/.*current //;s/, maximum.*//')"

#echo $TEST

if [ "$TEST" == "$MONITOR" ]
        then
                xrandr --output VGA1 --mode 1360x768

elif [ "$TEST" == "$TV" ]
        then
                xrandr --output VGA1 --mode 1440x900

else

        exit

fi

This script simply checks what the current resolution is being used and then toggles between the two set resolutions.

Categories
Arch Linux Blog Command Line HowTo Linux Posts Ubuntu

HowTo: Remove Every Other Line in Text Files – Linux

Let’s say you’ve got a text file, of any size, big or small, and you want to remove every other line of that file, well here are a few commands in Linux that allow you to do this.

Example, you want to get from this:

1
2
3
4
5
6
7
8
9
10

To this:

1
3
5
7
9

The sed way:

 sed -n "p;N;" file.txt > newfile.txt

The awk way:

 awk 'NR%2 != 0' file.txt > newfile.txt

Here you can actually specify N lines, replace 2 in the above command and you’ll be able to take out every N’th number. As an example, here’s the above replaced with a 3 on the file:

1
2
4
5
7
8
10

Easy as pie, right?

Categories
Kindle Linux Posts Ubuntu

Auto Sync Kindle in Ubuntu

For Christmas I got a Kindle 3 and it was instantly filled with Free eBooks (http://www.gutenberg.org/wiki/Main_Page) and research papers (PDFs). I found myself wanting to be able to automatically synchronize it with a folder on my desktop when it was plugged in and found no one else had posted online how to do this, so I began researching into udev and rsync to get something working.

Setting up udev rules to run a script when the Kindle is plugged in.

Firstly, we need to find some parameters of the Kindle which are unique to it so that udev can identify that it is the Kindle being plugged in and not another USB mass storage device. We’ll need to find out where the device is mounted in /dev/ which can be a little tricky because this folder is pretty full.

An easier way to do this is to first, without the Kindle plugged in do the following command (list the directory and pipe the output to a file called dev1 in the home dir):

ls /dev/ > ~/dev1

Now plug the Kindle in and redo the command, changing the destination output:

ls /dev/ > ~/dev2

The difference between the two files will show what has changed in the /dev/ directory:

 sdiff ~/dev1 ~/dev2

The output for mine (show in the image below) shows that 3 things changed when I plugged in the Kindle, sdb, sdb1 and sg2 directorys were added (shown by the > sign to the left). The folder I am looking for is the sdb1, this is where the mass storage device is found.

Write down where it’s found in /dev/ and then you can clean up those two files you previously made:

rm ~/dev1 | rm ~/dev2

The next thing we want to do is use udevadm to collect some useful information about the device which we can do by the following command (I have chosen to pipe the output to a file called kindle in the home dir, because the output was pretty big. If you don’t want to remove the > ~/kindle from the end of the command):

udevadm info -a -p $(udevadm info -q path -n /dev/sdb1) > ~/kindle

WARNING: This can look pretty daunting but, (in the words of Douglas Adams) Don’t Panic.

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0/block/sdb/sdb1':
    KERNEL=="sdb1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{partition}=="1"
    ATTR{start}=="16"
    ATTR{size}=="6410672"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="4294959104"
    ATTR{stat}=="     151     3389     4239     3776        0        0        0        0        0     2424     3776"
    ATTR{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0/block/sdb':
    KERNELS=="sdb"
    SUBSYSTEMS=="block"
    DRIVERS==""
    ATTRS{range}=="16"
    ATTRS{ext_range}=="256"
    ATTRS{removable}=="1"
    ATTRS{ro}=="0"
    ATTRS{size}=="6410688"
    ATTRS{alignment_offset}=="0"
    ATTRS{discard_alignment}=="0"
    ATTRS{capability}=="51"
    ATTRS{stat}=="     156     3389     4279     3808        0        0        0        0        0     2456     3808"
    ATTRS{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0':
    KERNELS=="21:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{device_blocked}=="0"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="3"
    ATTRS{vendor}=="Kindle  "
    ATTRS{model}=="Internal Storage"
    ATTRS{rev}=="0100"
    ATTRS{state}=="running"
    ATTRS{timeout}=="30"
    ATTRS{iocounterbits}=="32"
    ATTRS{iorequest_cnt}=="0xe1"
    ATTRS{iodone_cnt}=="0xe1"
    ATTRS{ioerr_cnt}=="0x1"
    ATTRS{modalias}=="scsi:t-0x00"
    ATTRS{evt_media_change}=="0"
    ATTRS{dh_state}=="detached"
    ATTRS{queue_depth}=="1"
    ATTRS{queue_type}=="none"
    ATTRS{max_sectors}=="240"

While this many look daunting, what you want to do if find some attributes from the output which mean only your Kindle will be found when looking for them all, the problem I have found is that, with udev rules you cannot go too far down in the parent tree which stops me being able to use the Kindles serial to identify it. This could mean that my computer will sync any Kindle that’s plugged into it but I don’t have 2 to check. The following are the attributes I chose:

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00"

Now you’ll want to write the udev rule in /etc/udev/rules.d/

cd /etc/udev/rules.d/

According to the readme the numbers represent the priority of the rule (higher overriding lower) followed by a descriptive name and it must end in .rules eg (xx-description.rules).

sudo gedit 81-kindle-sync.rules

The following is the whole line I used for the udev rule, the only addition to the above is the RUN+=”/home/alex/.scripts/kindlesync.sh” which will run the script in that directory, this script contains the rsync command.

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00", RUN+="/home/alex/.scripts/./kindlesync.sh"

Save and close the file and then restart the udev service:

sudo service udev restart

Writing a script to sync a folder on the Desktop with a folder on the Kindle using rsync:
Change the first directory from “/home/alex/ebooks/kindle_sync” to the directory on your computer where you want to keep the files that will be synced onto your Kindle and the second from “/media/Kindle/documents/sync/” to the location on your mounted Kindle where you want the files to be stored, save this file as kindlesync.sh. This is the script you call from the udev rules so for mine it’s saved as “/home/alex/.scripts/kindlesync.sh”.

#!/bin/bash

#Sync
rsync -av /home/alex/eBooks/kindle_sync/ /media/Kindle/documents/sync/

Go to the location of the script and make it executable with the following command:

chmod +x kindlesync.sh

Now try plugging in your Kindle and make sure it syncs up!

Problems

In the udev rule, ACTION==”add” can be used to specify when the Kindle is first plugged in, however when trying to use this Ubuntu’s auto-mounting service mounts the drive after the script is ran so the sync becomes useless. Without this it runs the script 3 times, twice before it has mounted and then once it has mounted – this is currently the best I have found but it’s extremely messy.

Categories
Command Line Linux Posts Ubuntu

How To: Install Pyrobot in Ubuntu

A quick guide for getting pyrobot running in Ubuntu, I’ve managed to get it running on 32bit and 64bit Ubuntu (10.10) so follow the guide accordingly to whichever architecture you’re using (64bit is about half way down the post).

32-bit

1. Download this file, or copy the below into a file and save it as pyroinstall.sh to your home dir.

#!/bin/sh

currentdir=$(pwd)

sudo apt-get install build-essential python2.6 python2.6-dev python-tk python-numeric libjpeg62-dev libncurses5-dev swig

wget http://pyrorobotics.org/download/pyrobot-latest.tgz

tar -zxvf pyrobot-latest.tgz

cd pyrobot

python configure.py

make

sed -ie 's/-e #!/#!/g' $currentdir/pyrobot/bin/pyrobot
sed -ie 's/-e # /# /g' $currentdir/pyrobot/system/version.py

echo 'export PATH=${PATH}:'$currentdir'/pyrobot/bin' >> $currentdir/.bashrc

Note: The ‘ in the code highlighter I have above won’t work in terminal, there isn’t much I can do about it so you’ll have to re-write it. Also if you’re doing this manually change the “$currentdir” to “~” if you are doing it in your home directory.

2. Make sure this file is in your home directory (e.g /home/alex/pyroinstall.sh) and make it executable:

 chmod +x pyroinstall.sh

3. Run the usual way (as super user because it has to install stuff etc):

 sudo ./pyroinstall.sh

4. It’ll ask you some questions, below are the answers I used – you can use other configurations if you know what you need. I also put the full output in pastebin ’cause it was bunging up the guide – http://pastebin.com/Kc5jyhja

1. 	2.6
2. 	/usr/include/python2.6
3. 	/usr/bin/python2.6
4. 	/etc/X11
5. 	none
6.01	n
6.02	n
6.03	n
6.04	n
6.05	n
6.06	n
6.07	n
6.08	n
6.09	n
6.10	n
6.11	y
6.12	y

5. Once it’s done reboot or run:

 source ~/.bashrc

6. Allow read/write/execute permissions for the pyrobot folder:

 sudo chmod -R 777 ~/pyrobot

You should all be done, try typing the following to get it running!

 pyrobot

64-bit

In order to get pyrobot running on 64bit, you basically need to add -fPIC to CFLAGS in all the relative Makefiles as explained in this mailing list post – http://www.mail-archive.com/pyro-users@pyrorobotics.org/msg00344.html – Lucky for you I’ve already gone through the effort of doing it and compressed it (download here you won’t need to if you are going to run the script though, it’ll do it for you..).

This script should download the file, configure and make and output the path to the .bashrc file for you (similar to the 32bit one does) so download this file, or copy and paste the script below into a file in your home directory:

#!/bin/sh

currentdir=$(pwd)

sudo apt-get install build-essential python2.6 python2.6-dev python-tk python-numeric libjpeg62-dev libncurses5-dev swig

wget http://dl.dropbox.com/u/307455/pyrobot-5.0.0_64bit.tar

tar -zxvf pyrobot-5.0.0_64bit.tar

cd pyrobot

python configure.py

make

echo 'export PATH=${PATH}:'$currentdir'/pyrobot/bin' >> $currentdir/.bashrc

2. Make sure this file is in your home directory (e.g /home/alex/pyroinstall64.sh) and make it executable:

 chmod +x pyroinstall64.sh

3. Run the usual way (as super user because it has to install stuff etc):

 sudo ./pyroinstall64.sh

Steps 4. 5. and 6. are the same as above..

Change Default Editor

If you want to change the default editor from emacs to anything else (this will change the default editor used in other terminal applications too..) use the following commands, just change gedit to whatever you’d like (vi, vim, kedit etc):

echo "export EDITOR=/usr/bin/gedit" >> ~/.bashrc
source ~/.bashrc

Any problems leave a comment and I’ll try and help as best as I can..

Categories
Linux Posts

Ubuntu Disconnecting from Wi-Fi and Failing to Reconnect.

I have an updated fix for this issue using a startup script to connect and a time scheduled script for checking the connection, rather than having one constantly running in the background, avalible here – http://alexsleat.co.uk/2011/01/09/a-more-elegant-solution-to-ubuntu-wi-fi-reconnecting-issue/

I’ve been having some trouble with my wifi on Ubuntu recently but I think it’s because I’m almost out of range of the access point. The problem is that once it disconnects it never seems to be able to reconnect unless wireless is disabled and re-enabled. No doubt there is another way of fixing this issue but it gives me a chance to write my first shell script.

In short the script disables network-manager (it was trying to do things auto which was screwing with things), sets up the connection and connects then checks for a string in the wireless card parameters which will only occur if it’s disconnected, if the string appears it’ll disable and re-enable the wireless device which seems to allow it to connect again. If it’s connected it’ll wait 3 minutes (180 seconds) before checking again where as if it does disconnect it’ll check again 30 seconds after it’s tired rebooting the device.

Here’s the script, be sure to try it if you’re having the same problem (you might need to change the wireless device ‘wlan1’ to yours) and comment if I’m doing something wrong or there’s another way around this, cheers.

#!/bin/bash

service network-manager stop

iwconfig wlan1 essid NETWORKNAME
iwconfig wlan1 key NETWORKKEY
ifconfig wlan1 up
dhclient3 wlan1

while true;
do

if (iwconfig wlan1) | grep -o "Access Point: Not-Associated"
then
	ifconfig wlan1 down
	echo "not connected, rebooting Wifi"
	ifconfig wlan1 up
	sleep 30
else
	echo "connected"
	sleep 180
fi

done