Archive for the ‘Linux stuff’ Category


Manage multiple Linux users editing the same projects

March 1, 2012

I work on projects on remote servers where a number of developers might login and edit a file. This usually results in the file permissions changing so that they own it which can cause problems. A simple solution is for everyone to be a member of the same group, then make it so that by default it is that group that is set on a file when someone creates it. We also then want to set the group write flag by default as well so that anyone can edit that file.

The steps involved in setting this up on a server are as follows.

— create a new group everyone will share (mygroup)
groupadd mygroup

— create any new users (if required, this can be skipped if you have existing users)
useradd -G mygroup -m myusername
passwd myusername

— Make the default group for a user to be mygroup so everyone in that group can access their files
usermod -g mygroup myusername

— Set the global umask in the global bashrc file so that all users set group write on files (needs everyone to logout and in again)
vim /etc/bashrc
umask 002

Note for the bashrc thing to work it has to be called from the user’s local .bashrc file intheir home directory. Most linux distros do this by default but it is possible for individuals to remove the include for the global bashrc which will then mean that group write is not set on files they create. The bit in the users .bashrc file that includes the global bashrc looks like this…

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc

Now if your shared work is in /var/www/html for example, you’ll need to change the group owner on all files in that directory to the new mygroup

chgrp -R mygroup /var/www/html

And you will also want to make sure every file is writable by the group

chmod -R g+w /var/www/html


SVN add all waiting files

September 7, 2011

If you would like to add all pending files to svn via an svn add command, the following works quite nicely:

svn status | grep ? | sed 's/^? \(.*\)/\1/g' | xargs svn add

And the same for removing missing files:

svn status | grep ! | sed 's/^! \(.*\)/\1/g' | xargs svn rm


Changing permissions of just files or just directories recursively

June 22, 2011

Sometimes you just want to change the permissions of all the fiels or all the directories under a certain folder. For example, when wanting to set the sticky bit on just directories on all directories under a folder.

Here we combine the find command with chmod …

find /path/to/someDirectory -type d -print0 | xargs -0 chmod g+s

And for just files (for when you forget that chmod -R g+s is applied to everything)…

find /path/to/someDirectory -type f -print0 | xargs -0 chmod g-s


How to configure apache with ssl ubuntu

June 20, 2011

Guide for making an https site on ubuntu here:


Splitting files by line count on the command line

March 30, 2011

Two new command line tools I learnt of today. To count the number of words or lines in a text file there is the wc utility.

wc -l myfile.csv

Returns the number of lines in the specified text file. If you want to then split that into several smaller files you can use the split utility. By default this splits files by number of bytes, but with the -l option you can split by number of lines

split -l 100 myfile.csv

The new files appear in the current working directory named aa, ab, ac, ed etc


Find and replace with sed and find

March 7, 2011

To find all instances of a string in a file you can use grep like so

grep -n findstring somefile.txt

To replace all instances of a string with another string in a file you can use sed like so

sed -i 's/find/replace/g' somefile.txt

To find all instances of a string in all files in a directory you can use grep like so

grep -n findstring /some/dir/*

To replace all instances of a string in all files in a directory you can combine sed and find like so

find /some/dir -type f -exec sed -i 's/find/replace/g' {} \;

-n on grep makes it print off the line numbers of where it found the search term

-i on sed means inplace and means it will do the replacement in that file and save it. You can do -iold if you like and every altered file will then be backed up to .old first

-type on find means only regular files are returned.

-exec on find means that the command following will be executed for each found file. The {} items puts the filename into the command. All args after -exec are assumed to be arguments of the command until a semi-colon is found. The semi colon has to be escaped otherwise it will be seen as a command line end.



All you need to remember about tar

February 10, 2011

The first thing to remember about tar is that tar is primarily for turning a directory into a file so you only have one thing to send rather than a direcotry with lots of things. Its second use is to then compress that file. So if you want to compress a single file, you first need to put it into a directory before using tar.

Most useful options:
1. Decide if you are creating or extracting to or from a tar and use one of the following…
c – create a tar from a dir
x – extract contents from a tar

2. Decide on the compression standard
z – gzip, the usual one
j – bz, makes smaller files but takes longer to process

3. Do you want lots of messgaes? Use v for verbose, usful to see that something is actually happening

4. Your options will always finish with f and after you specify the name of the tar file you are either creating or extracting from. File name convension is filename.tar.gz if using gzip or finish with bz if using the other compression option.

5. If you are creating a tar, finish with the name of the directory you want to tar.

Turning directory mydir into a tar and compressing with gzip:
tar czvf mydir.tar.gz mydir

Extracting directory mydir from a tar which is compressed with gzip:
tar xzvf mydir.tar.gz