Linux/Unix

Posts about using Linux and Unix

SSH Bad Owner Or Permissions Error

After a recent update on Ubuntu I found that I was unable to use ssh due to a strange permissions error to do with the ssh config file. This was quite a problem as I wasn't able to push changes to my git server. The error was as follows:

$ ssh hostname
Bad owner or permissions on ~/.ssh/config

All that was needed was for my user only to have read and write permissions to the ssh config file. The following command fixed things for me.

chmod 600 ~/.ssh/config

If that doesn't solve things for you then you might need to also make sure that the correct user is associated with the config file.

chown user:user ~/.ssh/config

Grep For Text In All Files In A Directory

Searching all files in a directory and sub-directories for a particular term is really useful and comes in handy in all sorts of situations. It is available on all Linux systems and the basic syntax is as follows.

grep -r -i pattern directory

The -r flag is used to recursively search underneath the given directory and the -i flag is used to ignore case. The pattern is a normal regular expression, which can be changed to an extended set by using the -E flag.

An example of finding a search term in everything under the current directory would be like this.

grep -r -i searchterm ./

Remember that if you want to use regular expression characters then you'll need to escape them. For example, to find all PHP opening tags you will need to escape the < and ? characters.

grep -r -i \<\?php ./

Happy Birthday Bash Script

Following on from the PHP script to print happy birthday I wanted do the same in a bash script. I don't really use bash for much more than stringing together commands so I had to figure out how to do loops and if statements using the simple bash syntax. I also wanted to pass the name of the person as an argument, rather than hard code it into the script. This is what I came up with.

#!/bin/bash

output=''
for i in {1..4}
do
  output=$output"Happy birthday "

  if [ $i -eq 3 ]
  then
    output=$output"dear $1\n"
  else
    output=$output"to you\n"
  fi
done
 
echo -e $output

Save the file as happy.sh or similar and run it in the following way (Name is the argument that we pass to the script).

$ ./happy.sh Name

Find The Size Of Files And Directories In Linux With The du Command

The du (or 'disk usage') command is a Linux command that can print a list of the files within a directory including their sizes and even summarize this information. It is useful if you want to see how large a group of files is and provides more information about directories than the ls command does.

Using du within a directory will show you the size (in bytes) of all files and directories under that directory, including the size of the current directory. To make du produce more readable results just use the -h flag to make the file sizes into a human readable format. You can also use the -c flag to produce a grand total of all of the sizes found, and the -s flag to display only a total. The -a flag can be used to display all files as well as directories, leave this out to just display directories.

Remove SVN Files From Source In Linux

There might be a couple of reasons why you would want to do this. Perhaps the repository has been checked out instead of exported, or maybe the repository doesn't exist any more. A couple of strategies exist remove all SVN files from a set of directories in Linux. You can either use the rm command directly and pass in a find command using grave accent quotes (key to left of '1').

rm -rf `find . -type d -name .svn`

Or you can pass the output of the find command to the xargs command, which calls the rm command.

find . -type d -iname ".svn" -print0 | xargs -0 rm -rf

You can even use the -exec flag of the find command to run the rm command.

find . -type d -iname ".svn" -exec rm -rf {} \;

Using xclip To Copy Output From The Command Line In Linux

When copying the standard output from within a terminal I often push it to a file using the "command > file" syntax. The trouble is that I then end up with a file that I have to open in order to get the output, and I often forget to delete the file once finished. This is especially annoying when I just want to paste some debug output into a help topic or similar. The solution to this is to use xclip to store the output in the xclipboard instead of a file. This is essentially the function of xclip, it allows access to a clipboard that you can store anything in.

A good example of this in use is when pasting the output of php -i into the amazing xdebug tailored instillation instructions. When you print this to the terminal you get pages of output. To push this into xclip instead just pipe it like this.

Installing A Realtek Wireless Card On OpenSuSE Linux 11

I've been using my Medion Akoya E1222 netbook for about a year now and I still think it's an awesome little machine. Recently, I decided to swap from Ubuntu to another Linux distro and have been experimenting with Fedora, JoliOS, PuppyLinux before finally going for OpenSuSE. The only problem I had was figuring out how to get the wireless card working.

I had a little bit of trouble tracking down this information, so I thought I would put it here both for future reference and for anyone else looking for the same information. This is mainly specific for my netbook and particular distribution, but it might help others with the same sort of problems.

The first thing you need to do is find out if your system can actually see the wireless card. This can be done using the lspci command, which in my version of SuSE needed to be called directly from its location in the sbin directory.

Running Commands In The Background In Linux

A useful technique to know about when using Linux is to run commands in the background. Sometimes certain commands can either take a while (like copying a large file) or will simply take over the terminal window when run. For example, lets say that you open a file in gedit, you might use the following command.

gedit file.txt

Doing this will open the file in gedit but will not allow you to do anything in the terminal window until you close gedit. To open the file but still keep the terminal window active use the & symbol at the end of the command. This will run the proceeding command in the background.

gedit file.txt&

To run a file copy in the background do the following.

cp file.txt file.txt.bak&

Note that this command shouldn't be used for all commands, especially those that produce some form of output. For example, running the following command

DVD Backup With Linux

If you are part of a company the chances are that you have either set up or are aware of a backup policy for your data. However, even if you are not then you might have the need to backup certain items to a DVD drive.

DVD burning is accomplished in Linux by using the mkisofs and growisofs programs. This can be run in either a two stage or a single stage process. The mkisofs program is used to create an iso file that is then written to the DVD using the growisofs program. The following will take three files and create an iso called toburn.iso, this will then be used to write the data to the DVD.

Find And Replace On All Files In And Below A Directory

The following shell command uses the find function to find all files in or below the current directory that have the extension php. It then passes each file found onto a sed command which then replaces all <? with the longer <?php version.

find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

The -name argument in find will look at the base of the file name, that is, the file without any directory path. The -exec command is used to pass each file found onto another command, in this case sed is used.