Tag Archives: linux

Ubuntu Server: How to Move or Copy Files Starting with a Dash (-) Tips, Tricks and Tutorials 16 SEP 2013

ubuntu-torso-in-a-white-topIt’s not often that you do run into this particular problem, but when you do it is a bit of an annoyance – most Ubuntu Server terminal commands (in other words linux commands), in our examples the cp (copy) and mv (move) instructions, can handle files beginning with a dash (-).


Well obviously because all commands accept parameter switches that are indicated with a dash (-) or double dash (–), meaning that when the command hits a file name staring with a dash, it basically assumes it to be a switch declaration which is obviously very wrong.

Luckily for us, even though it isn’t immediately all that apparent, there is a simple way to get around this and allow for the moving or copying of files starting with a dash – basically we delimit the file options by essentially instructing the application that we’ve finished including our switches.

We do this by placing a double dash (–) after the last lot of switches which tells the mv or cp command that what follows are filenames, thus allowing them to happily work on everything coming in next.

Useful to know.

Ubuntu Terminal: How to set a User’s Shell to Bash Tips, Tricks and Tutorials 19 AUG 2013

ubuntu-10-logoGiven an installation of Ubuntu Server (or pretty much any other Linux variant that requires you to use a terminal), changing a user’s shell is thankfully pretty easy.

I generally like to use bash as my shell of choice, as it gives me the most user friendly and verbose interface to work in, meaning that I often find myself having to switch a newly installed system’s default out for bash for my freshly added user account.

And given that this post is more a reference for me than anything else, these are a useful set of steps to follow in order to change to Bash:

1. Check the user’s current shell by reading the entry in the /etc/passwd user information file.

grep craig /etc/passwd

Grep will return something that looks like craig:x:1006:1006::/home/craig:/bin/sh. That particular line tells us that the system is currently using sh as my shell.

2. Grab a list of available shells by reading the common /etc/shells file.

cat /etc/shells

3. Change the shell using the chsh command. If you are logged into your account, then chsh will first prompt you for your password, followed by a prompt for the new shell. Note, you have to enter the full path, as listed in /etc/shells.

Oh, you can also quicken the process by calling chsh with parameters, as indicated by the man page:

chsh -s /bin/bash craig


Ubuntu Server: How to Free up Disk Space Software & Sites 03 APR 2013

ubuntu-10-logoEvery now and then your server will throw up its hands and declare that it has run out of space, which means that you need to go in and figure out what is taking up all the space and then a) increase the amount of available disk space or b) delete or archive things in order to free up some disk space.

As it turns out, this is mostly a manual process of locating things and deleting or clearing out if possible, and more often than not, you’ll find that overzealous logging, which even logrotate couldn’t effectively deal with, sitting at the root of your problem.

Now we’re interested in b) for this particular post, so we start by doing a Disk Free check to get a better idea of our current situation:

sudo df -h

With that information in hand we then systematically look at the problem areas with targeted calls to Disk Usage:

sudo du -h –-max-depth=1 /
sudo du -h –-max-depth=1 /var/
sudo du -h –-max-depth=1 /var/log/

Once you’ve identified a particularly large folder, check it’s contents with the usual human readable listing:

sudo ls -lha

Spot the overly large offenders and happily delete with the usual rm command. Note that for log files it might be necessary to first identify the controlling service and stop that, in order to allow you to remove the file. Also, a nifty trick if you don’t want to delete what could be important program files is to simply overwrite them with blank, thus leaving their group and file permissions perfectly intact.

Solved: Print Error – PPD version is not Compatible with Gutenprint CodeUnit 12 JAN 2013

20081112_Canon_IP4500_PrinterFor some or other reason my Canon IP4500 printer simply stopped printing on my Ubuntu 12.10 box in the office. Accessing the Printers under the System Settings menu, I noticed quite a few broken print jobs in the print queue, and on closer inspection they all read ‘failed with error: PPD version is not Compatible with Gutenprint (xyz)’. It is possible that a recent update to the CUPS printing system must have broken something, but thankfully the Internet once again proved itself to be a hell of a helpful troubleshooting resource.

To fix, open a terminal (Ctrl+t) and run:

sudo /usr/sbin/cups-genppdupdate

This should generate a new PPD for your installed printer and if successful, you simply need to restart the CUPS (Common Unix Printing System) service, which you can do by running:

sudo service cups restart

And as luck would have it, problem solved!

Ubuntu: How to Setup a SSH Tunnel via a Terminal CodeUnit 26 OCT 2012

SSH tunnels are useful beasts in that they allow you to communicate with machines and ports on a private network which are not directly accessible to the external world, by building a bridge between your local machine and a machine in the walled off network to which you happen to have SSH access to.

The diagram below shows you an example whereby to gain access to a Oracle server on Port 1521, you would first SSH into a linux box on the inside network and then create a SSH tunnel which would transport traffic between your machine and the Oracle server.

Another example could be if you have MySQL installed on your server but have cut off external access to the database server, leaving only SSH open. In this case, you would connect into the box via SSH and create a tunnel to the MySQL database server on localhost port 3306.

To create a SSH tunnel is fairly simple and can be created with this command:

ssh -f remote-server.net -p 22 -l myusername -L 3307:localhost:3306 -N

The line above first connects to remote-server.net via SSH on port 22 using the user name ‘myusername’, and then sets up a SSH tunnel connecting port 3307 on your local PC (i.e. and hooking it up to port 3306 on the remote-server.net box itself. Of course you could have created a connection to any other box that remote-server.net has direct access to.

You should be prompted for a password when running this command. Note that the -f switch means that this process will be started in the background and the trailing -N instructs OpenSSH not to execute the command on the remote server.


Ubuntu: How to add a Suffix (or Prefix) to a Line of Text in a Text File using AWK CodeUnit 18 JUN 2012

If you are looking for a top notch tool to carry out quick text manipulations on text files under Ubuntu terminal, you could probably do no better than by looking up AWK (or I suppose SED if you are so inclined).

Anyway, back to AWK. The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data (either in files or data streams) for the purpose of producing formatted reports. The language used by awk extensively uses the string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions.

AWK was created at Bell Labs in the 1970s, and its name is derived from the family names of its authors – Alfred Aho, Peter Weinberger, and Brian Kernighan. ‘awk’, when written in all lowercase letters, refers to the Unix or Plan 9 program that runs other programs written in the AWK programming language.

Okay, now that we have some history, lets see just how easy it is to add either a prefix or suffix to each line contained in our text file using awk:

#add a prefix to each line in the text file
awk '{ printf("myprefix %sn", $l);}' sample-text-file.txt

#add a suffix to each line in the text file
awk '{ printf("%s mysuffixn", $l);}' sample-text-file.txt

#add both a prefix and a suffix to each line in the text file
awk '{ printf("myprefix %s mysuffixn", $l);}' sample-text-file.txt

The first example will result in each line in the sample text file being prepended with the word ‘myprefix’. The second example will result in each line in the sample text file being appended with the work ‘mysuffix’. I doubt that at this stage I still need to spell out what the third example does!

If you wish to save these changes to a text file, using the standard IO redirect functionality, i.e. the > sign:

#save output to a file
awk '{ printf("myprefix %s mysuffixn", $l);}' sample-text-file.txt > altered-text-file.txt

(Note that you shouldn’t direct the output at the input file as you’ll seriously screw things up. Rather save to a new file instead).


Related Link: http://en.wikipedia.org/wiki/AWK

How to test if a Crontab is working in Ubuntu CodeUnit 15 JUN 2012

Editing your cron jobs with the crontab -e command will ensure that your crontab is at least syntactically correct, by not installing it unless it is a valid cron file.

Outside of that though, it falls to you to test that your jobs are in fact running correctly. This can be achieved by piping the cron command output to a file that you control for each one of your jobs.

In practice:

00 01 * * * bash mycommand1.sh > /tmp/mycronjob.log 
00 02 * * * bash mycommand2.sh > /tmp/mycronjob.log

And in the same vein, you can check that the crontab as a whole is being executed by creating a job right at the end of the cron list that runs every minute and pipes out its output to a file that you control:

*/1 * * * * echo "Success! $(date)" >> /tmp/cronwatch.log

Note that > means rewrite the target file every time, whilst >> means append to the end of the file (and create if not exist).


Related Link: https://help.ubuntu.com/community/CronHowto

How to add a Comment or Remark to a Crontab in Ubuntu CodeUnit 08 JUN 2012

To add a comment or remark in your Ubuntu crontab is trivially easy, following the standard for adding comments already found in bash scripting.

In other words, all you have to do in order to add a line which you don’t want the cron system to process, is start it off with the hash (#) character!

In practice:

# This is a comment and will not be processed by the cron engine
00 01 * * * ls -lh /home/craig