Posts Tagged ‘command line’

Move multiple files from command line

15, March 2011

Using find to copy or move multiple files

All the .txt files are located in directories and subdirectories of the ~/tmp folder and I want to copy them to ~/tmp2

To copy them, open a Terminal and use;

$ find ~/tmp -name ‘*.txt’ -exec cp ‘{}’ ~/tmp2 \;

This copies all the files in the original folders and sub folders and saves them to the ~/tmp2 folder.

or to move them,

$ find ~/tmp -name ‘*.txt’ -exec mv ‘{}’ ~/tmp2 \;

this deletes them from the original folders and sub folders and saves them to the ~/tmp2 folder.

Start up Sound

15, March 2011

Start up Sound:

This works on my Xubuntu Natty install. I found a .ogg sound file that I wanted to use as a start up sound so needed an command line ogg player to play it.

$ sudo apt-get install ogg123

Now create a script in /usr/bin as follows [replace mousepad with the text editor of your choice];

$ sudo mousepad /usr/bin/startup

Add the following to the file

#!/bin/sh
#### play sound at startup ####
## reduce volume ##
sleep 5
StartupNotify=false
/usr/bin/amixer -c 0 sset Master,0 60%
## play sound ##
/usr/bin/ogg123 /path_to_my/sartupsound.ogg
## increase volume again ##
/usr/bin/amixer -c 0 sset Master,0 85%

save the script and make it executable with.

$ sudo chmod +x /usr/bin/startup

Now add the script to the system start up, in Xubuntu open [Settings], [Settings Manager], [Session and Startup], [Application Autostart], and then the [Add] button. Add a name of your choice in the Name field, and the same in the Description field, then the path to your script in the Command field i.e /usr/bin/startup in this case the [ok] to finish. You will then get a startup sound next time you log on.

Shutdown Sound

15, March 2011

Shutdown Sound:

This works on my Xubuntu Natty install. I found a .ogg sound file that I wanted to use as a shutdown sound so needed an command line ogg player to play it.

$ sudo apt-get install ogg123

now add a script as follow script to /etc/init.d as follows

In a Terminal, create the file with, [change mousepad to your favourite text editor]

$ sudo mousepad /etc/init.d/K99shutdownsound.sh

copy the following into the script

#!/bin/sh
#### play sound at startup ####
## reduce volume ##
amixer -c 0 sset Master,0 50%
## play sound ##
/usr/bin/ogg123 /path_to my/shutdown.ogg

save the script and make it executable.

$ sudo chmod +x /etc/init.d/K99shutdownsound.sh

now add a shortcut to /etc/rc0.d where shutdown scripts go and to /etc/rc6.d where reboot scripts go

$ sudo ln -s /etc/init.d/K99shutdownsound.sh /etc/rc0.d/K99shutdownsound.sh

$ sudo ln -s /etc/init.d/K99shutdownsound.sh /etc/rc6.d/K99shutdownsound.sh

Now I get a reboot and shutdown sound.

gconftool-2

1, December 2010

Using the gconf tool to make changes to your desktop from the command line. These allow you to script changes you make every time to upgrade.

These have been tested on the Gnome Desktop on Lynx & Maverick.

Copy and paste these commands into a Terminal [Applications], [Accessories], [Terminal]

## Add icons to the [System] menu. ##
gconftool-2 --type Boolean --set /desktop/gnome/interface/menus_have_icons True

## Remove shutdown “Do you really want to do this 60 second count down” question ##
gconftool-2 --type Boolean --set /apps/indicator-session/suppress_logout_restart_shutdown True

## Return to old Update notification icon in top menu ##
gconftool-2 --type Boolean --set /apps/update-notifier/auto_launch False

gconftool-2 --type int --set /apps/update-notifier/regular_auto_launch_interval 0

## Add Banner message to GDM ##
sudo -u gdm gconftool-2 --set --type string /apps/gdm/simple-greeter/banner_message_text “Ubuntu 10.10 Maverick Meerkat 64 Bit”

sudo -u gdm gconftool-2 --set --type string /apps/gdm/simple-greeter/banner_message_text_nochooser “Ubuntu 10.10 Maverick Meerkat 64 Bit”

sudo -u gdm gconftool-2 --set --type boolean /apps/gdm/simple-greeter/banner_message_enable true

## Set wallpaper ##
gconftool-2 --type string --set /desktop/gnome/background/picture_filename /home/Pictures/My_Wallpaper.jpg

APT Locked

20, October 2010

apt-get update error

Trying to update apt gives error message;

$ sudo apt-get update

error message;

E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the list directory

To fix it remove the lock with;

$ sudo rm /var/lib/apt/lists/lock

10 Things to Do After installing Maverick

20, October 2010

Before you mess, backup the sources.list with;
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

1] Select a new wallpaper as the wallpaper has not improved since the purple tie dyed T Shirt look of Lucid. Right Click the desktop and select [Change Desktop Background] and select any other wallpaper from the defaults or one of your own.

2] remove the revolting purple background to the graphical login window, install GDM2 Setup

GDM2 Setup, A login interface management utility for the new GDM. Allows for wallpaper setting, autologin option, prompted or userlist login, etc.

The setup utility that comes in Ubuntu Karmic (and many other Gnome based distros) is missing most of the older GDM setup utility’s functionality. This application puts the functionality back in the hands of the users through a familiar and simple GUI. https://launchpad.net/gdm2setup
Add this empty directory first [the program halts if it can’t find it, a bug https://bugs.launchpad.net/gdm2setup/+bug/560130 that should have been sorted by now guys];

$ sudo mkdir /usr/share/images/xsplash

Add source with;

$ sudo add-apt-repository ppa:gdm2setup/gdm2setup

there is no Maverick repo yet, so you then need to open [System], [Administration], [Synaptic Package manager], {settings], [Repositories] and click on the “other Software” Tab. Now highlight the gdm2setup line and select [Edit] and change Maverick back to Lucid and close.

then update

$ sudo apt-get update

now add the program

$ sudo apt-get install python-gdm2setup

Then find the program called Login Screen (GDM2Setup) in [System], [Administration]

3] Swap the Close/Min & Max buttons back to the Right on the Windows, try if for a while first as it hear to stay! “windicators” are on their way for right hand side.

Install Ubuntu Tweak http://ubuntu-tweak.com/

Add Ubuntu Tweak source
$ sudo add-apt-repository ppa:tualatrix/ppa

$ sudo apt-get update

$ sudo apt-get install ubuntu-tweak

Then find the program called Ubuntu Tweak in [Applications], [System Tools] and in the program select [Windows Manager Settings], [Windows Titlebar Button Layout]

4] Remove the Social networking/message notification indicators i.e. the new “me” menu and the “envelope” from the top Right Hand notification area, I’m not Social and proud of it :-).

remove me-menu with;

$ sudo apt-get remove -y indicator-me

remove the envelope with;

$ sudo apt-get remove -y indicator-messages

both will disapear on rebooting the desktop.

5] Add the Gnome Configuration Editor to your menus. Right Click on the [Application] menu and select [Edit Menus], then select [System Tools] and place a tick in the box next to [Configuration Editor] and then select [Close]. Now select [Applications], [System Tools] then [Configuration Editor]

6] Remove the 60 second to shutdown confirmation message. Open the Configuration Editor [see 4 above] in Applications, [System Tools] now using the left-hand menu select [apps], then [indicator-sessions] and tick the box next to [ suppress_logout_restart_shutdown ] then [File], [Quit]

Or in a Terminal;
$ gconftool-2 --type Boolean --set /apps/indicator-session/suppress_logout_restart_shutdown True

7] Updates: revert back to the update icon in the top toolbar, open Gnome Configuration Editor [see 4 above], then navigate to [apps], [update-notifier] and untick the [auto-launch] option, then change the [regular_auto_launch_interval] to zero, then [File], [Quit] and reboot your PC.

Or in a Terminal;
$ gconftool-2 --type Boolean --set /apps/update-notifier/auto_launch False
and
$ gconftool-2 --type int --set /apps/update-notifier/regular_auto_launch_interval 0

8] Hate the Nautilus bread-crumb file path?, switch back to the text file path with Ctrl+l or / and back again with Esc. You can also switch permanently by opening the Gnome Configuration Editor [see 4 above], then navigate to [apps], [nautilus], [preferences] and putting a tick in the always_use_location_entry box.

9] Get Flash working in Firefox: Open Firefox and access [Tools], [Add-ons], select the [Get Add-ons] Tab and then [Browse All add-ons] then search for FLASH-AID and install it and follow the on screen instructions.

10] Enjoy…..

Adding A Repository

22, February 2010

To add the Medibuntu repository, see here here for details or copy and paste this command into a [Terminal];

sudo wget –output-document=/etc/apt/sources.list.d/medibuntu.list http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list && sudo apt-get –quiet update && sudo apt-get –yes –quiet –allow-unauthenticated install medibuntu-keyring && sudo apt-get –quiet update

all on one line.

Sheevaplug – automount USB drive at boot

17, February 2010

The problem with the Sheevaplug is that it boots too damn fast and the USB drive is too slow to be available for the fstab mount.

The solution, see here for my post on setting up the fstab file to mount your USB drive. When you edit the fstab on the Sheeva you will find it empty but you should end up with an entry similar to this.

# UNCONFIGURED FSTAB FOR BASE SYSTEM
/dev/sda1 /mnt/usbdrive ext3 auto,user,rw,exec 0 0

My usb drive is mounted at /dev/sda1 and I created a mount point called /mnt/usbdrive, the drive is formatted as ext3 but you can replace this with the format of your drive or just replace with the word auto for it to check.

Once you have your fstab in place you need to create a script which will slow down the boot for the USB drive to start. Thanks to restamp on the plugcomputer.org forum see here for the solution.
First navigate to the init.d folder, in a [Terminal] issue the command;

cd /etc/init.d

then using whatever text editor you have installed [I added nano], open a file called wait4usbdrive with;

nano wait4usbdrive

then copy into it restarts script;

#!/bin/sh
#
# If /etc/fstab has been configured to mount a USB drive, pause to give
# the USB drive devices time to show up in /dev. If this is not done,
# checkfs.sh will fail, requiring manual intervention…
#
case "$1" in
start)
grep -q ^/dev/sda /etc/fstab &&
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
[ -b /dev/sda ] && exit 0
sleep 1
done
exit 1
;;
stop)
;;
esac

Then make it executable with;

chmod +x wait4usbdrive

Then link it to the rcS.d folder with this command;

ln -s ../init.d/wait4usbdrive /etc/rcS.d/S25wait4usbdrive

Now you can shutdown and restart the plug and the USB drive should now auto-mount. My USB drive is a 1.5 TB iomega with it’s own power supply.

Backup using rsync, SSH & cron

31, August 2009

Backup on Ubuntu.

Updated 09/11/09 to correct some errors!

This “How To” was written with the help of this article by Troy Johnson, here and amended were necessary to fit my needs.

I have 2 PC’s on a network and want to backup some files from one PC the [remote_client] to the other PC the [server] to do this I want to run a script from the server. The process can be achieved using rsync & SSH and automated with a cron job on the server.

Step:1

We install on the server

open-ssh-server and rsync

and on the client openssh-client and rsync

This can be done via the Synaptic Package Manager or using the command line i.e.

~$ sudo apt-get install openssh-client

~$ sudo apt-get install rsync

you also may want to check that cron is installed and if you want a GTK version of rsync you could install gadmin-rsync.

So the files I want to backup are on the remote_client in the following folder; /home/martin/my_files

and I want to back them up on the server in the following folder; /home/martin/backups

Note: I have an account on both PC’s with the same name and password, and read access to both the source & destination folders.

On the server we test out a basic script called backup.sh and saved with the Permissions set to “allow executing as a program”, check out my post here for help with that, the content of the script is;

#!/bin/sh

## first script to copy files from remote_client to server ##

rsync -avz -e ssh martin@remote_client:/home/martin/my_files/ /home/martin/backups

save the script and then run it by typing;

~$ ./backup.sh

in a Terminal assuming you saved the script in your home directory /home/martin and called it backup.sh. You should be prompted for your user password on the remote_client then the process should start.

Step:2

Ok so that does the trick but if I’m to automate this script it needs to work without SSH asking me for my password each time. To do this I need to generate a private/public key pair, this is much more secure than adding the actual password in the text of the script or elsewhere linked to it.

We now need to be logged onto the server;

To generate the key pair I open a [Terminal] and issue the command;

~$ mkdir /home/martin/.key

to create a hidden folder in my home folder called .key [the dot makes it hidden], then;

~$ ssh-keygen -t dsa -b 1024 -f /home/martin/.key/server-rsync-key

Generating public/private dsa key pair.

Enter passphrase (empty for no passphrase): [press Enter]

Enter same passphrase again: [press Enter]

Your identification has been saved in /home/martin/.key/server-rsync-key.

Your public key has been saved in /home/martin/.key/server-rsync-key.pub.

The key fingerprint is:

etc. etc.

I now have 2 files in the .key folder in my home folder called;

server-rsync-key

and

server-rsync-key.pub

As you will later set-up a cron job which will run a script that needs access to the junk-rsync-key file we need to change the default ubuntu file permissions to owner, read/write group & other to none

$ chmod 600 server-rsync-key

Now copy the other key, server-rsync-key.pub over to the remote_client into my home folder /home/martin and log onto the remote_client.

Now on the remote_client, I issue the following command in a [Terminal]

~$ if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi

[this checks to see if the folder .ssh exists and if not creates it]

~$ mv server-rsync-key.pub .ssh/

[this moves the key from my home folder to the .ssh folder, hidden in my home folder]

~$ cd .ssh/

[we now move to the .ssh folder]

~$ if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi

[this checks to see if there is an authorized_keys file and if not creates it with the correct permissions]

~$ cat server-rsync-key.pub >> authorized_keys

[this copies the contents of server-rsync-key.pub key into the file authorized_keys file.

If we now run our original script with the amendment below on the server it no longer asks for a password.

rsync -avz -e “ssh -i /home/martin/.key/server-rsync-key” martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:3

Now the key can be used to make connections to the remote_client, but these connections can be from anywhere (that the ssh daemon on remote_client allows connections from) and they can do anything (that remote_user can do), which could be dangerous.  To make this more secure edit the ‘authorized_keys’ file in remote_client /home/martin/.ssh folder with a text editor and modify the line ending user_name@server_name from this:

ssh-dss
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

to this:

from="10.1.1.1", command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

where “10.1.1.1” is the IP address of server, and “/home/martin/.key/validate-rsync” point to a script called validate-rsync as follows:

#!/bin/sh
case “$SSH_ORIGINAL_COMMAND” in
*\&*)
echo “Rejected”
;;
*\(*)
echo “Rejected”
;;
*\{*)
echo “Rejected”
;;
*\;*)
echo “Rejected”
;;
*\<*)
echo “Rejected”
;;
*\`*)
echo “Rejected”
;;
*\|*)
echo “Rejected”
;;
rsync\ –server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo “Rejected”
;;
esac

If server has a variable address, or shares its address (via NAT etc.) with hosts you do not trust, omit the ‘from=”10.1.1.1″,’ part of the line (including the comma), but leave the ‘command’ portion. This way, only the ‘rsync’ will be possible from connections using this key. Make certain that the ‘validate-rsync’ script is executable by remote_user on remote_client and test it.

So in my case the result looks like this:

command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg9YS0NKxugT+o4UuIj
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

Our backup.sh script now looks like this:

#!/bin/sh

## second script to copy files from remote_client to server ##

rsync -avz -e “ssh -i /home/martin/.key/server-rsync-key” martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:4

I now need to run the script from a Cron job, first move the backup.sh script into your $PATH, for help with this check out my post here.

Now on the server open a [Terminal] and issue the command:

~$ crontab -e

This should open a text editor, normally vi, normally paste into it the following text as a reminder

# leave a space between each option #
# the * is a wildcard option #
# dayofmonth is 1 to 31 #
# dayofweek can be 0 – 6, 0 is Sunday #
# or use mon tue wed etc #
#
# mins hours dayofmonth month dayofweek command
# * * * * * * command

Then use Crontrol+o to save and press [Enter] to save, then Control+x to [Exit].

and test…

Cron jobs

31, August 2009

Cron tabs can be used to automate jobs on your system by running programs or scripts at prdefined times, dates or days, each user can have their own cron tabs or they can be run as root. To open a crontab, type the following in a [Terminal];

crontab -e

this will open in the vi text editor, but if you prefere the gedit text editor, run the following command;

export VISUAL=gedit

If you stick with vi then use Control+o followed by [Enter] to save your tab, then Control+x to [Exit].

When you run crontab -e, you will get an empty crontab with just the following [or similar] dispalyed;

# m h dom mon dow command

the headings are described below and I usually add these to the top of the file to make things easier to edit. So my crontab looks like this:

# m h dom mon dow command

# mins -> minutes (0-59)

# hours -> hour (0-24)

# DayOfMonth -> day of month (1-31)

# month -> month (1-12)

# DayOfWeek -> day of week (0-6) [Sunday is 0], or

# DayOfWeek -> day of week (mon – sun)

# You can use ‘,’ ‘-’ to give you 1,2,3,5 or 1-5 etc

# The * is used as a wildcard i.e every month

# m h dom mon dow command

# * * * * * /command

Examples;

each of the 6 command requires a space between them, here are a few examples;

### runs cron 2 mins past every hour ###

2 * * * * /home/martin/bin/play_sound.sh

### runs cron every 2 minutes ###

# m h dom mon dow command

*/2 * * * * /home/martin/bin/play_sound.sh

#01 * * * * root run-parts /etc/cron.hourly

#The run-parts script is simple enough: it just runs all the executables in the specified directory

### runs cron every fri at 15:30 ###

# m h dom mon dow command

30 15 * * * fri /home/martin/bin/backup.sh