Tag Archives: Unix

Ubuntu server upgrade

# Get list of available upgrades
sudo apt-get update

# Upgrade packages
sudo apt-get upgrade

# Remove no longer needed packages
sudo apt-get autoremove

# Upgrade distro
sudo apt-get dist-upgrade

Remove old kernels

# Reboot into newest first, of course
sudo shutdown -r now

# Remove all old kernels
sudo apt-get remove --purge $(dpkg -l 'linux-image-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')

Unix: Remove lines from a file based on regular expression

Just a note to self. Useful for trimming away useless information from a log file for example.

# Output result
$ sed "/pattern/d" file.log

# Overwrite file
$ sed "/pattern/d" file.log > file.log

# Inplace deletion (requires GNU sed)
$ sed -i "/pattern/d" file.log

In the pattern, things like capturing groups and alternations needs to be escaped with a slash, \. If you have RegexBuddy you can use the GNU BRE flavor to help you construct the pattern.

Source: StackOverflow

Tail less

I keep seeing people typing these lines in the console:

$ tail /path/to/some.log
$ tail -f /path/to/some.log

This is often a dumb thing to do. Why? Because you can’t really do anything with tail. What if you discovered you needed to look at something right above the lines you got printed out? Or what if you were -f’ing and something flew past you that you needed to investigate further? You’d have to leave tail and run it again with more lines or use a different tool instead. Not very practical.

What more people should do is to use less tail and more less 🙂

$ less /path/to/some.log

Things you can do with less



Up one line

Down one line

Up one page

Down one page

Beginning of file

End of file

ctrl + c

Stop follow


Search forward

Search backwards

Next search result

Previous search result

Much more flexible and handy than tail! Know your tools 😉 Now back to work…

Unix: Managing users and groups

Just a note to self on how to administer users and groups on Unix systems, and some related tasks.

# Add group
groupadd -g <n> foobar

# Add user
useradd -G foobar -d /path/to/home -m -s /usr/bin/bash alice

# Add user to group
user -G foobar -a alice

# Change user home
usermod -d /path/to/new/home -m alice

# Change default group of user
usermod -g foobar alice

# Change password of user
passwd alice

# Change ownership
chown -R alice:foobar /path/to/target

Unix: Command history search

In a Unix Bash shell we can scroll through previous commands by using the up and down arrow keys, but we can also search for previous commands. I keep forgetting how, so time to write it down 😛


Start a command search

Run the command directly
Ctrl+e or Ctrl+a

Exit search and jump to the end or beginning of command.
← or →

Exit search and move through command as usual

Remove everything after the current cursor position and to the end of the command

Unix: Useful and pretty prompt

Some servers at work has varying degrees of useful prompts when I connect to them through SSH. Usually they are quite annoying and for example showing the shell type and version, which I frankly don’t care much about. Here’s how to make it show the current user, hostname and working directory. In addition the hostname is made more visible, which is a nice effect.

$ bold=$(tput bold)
$ reset=$(tput sgr0)
$ export PS1="\u@\[$bold\]\h\[$reset\]:\w\$ "

Stick it in your .profile, .bash_profile or whatever else you’ve got going on to make it more permanent.