Reading Email with PHP and IMAP/POP3
Garmin Edge 205 GPS Cycle Training Tool
Nasa's Space Shuttle Atlantis Transiting the Sun
Installing Proprietary nVidia Drivers on Ubuntu Linux
Javascript Snippets
A Simple Terrain Model
 
 

Useful Ubuntu Linux Commands

Linux commands can be far from intuitive, with files related to a single application seemingly scattered throughout various locations, such as usr, bin, etc, var. This can make it difficult to recall how to invoke certain essential but not every-day commands. This page lists these commands in a single place.
compton, 6 September 07
Updated 12 February 10

Read a file and perform a command for each line

The following trivial example simply copies a file from inputfile to outputfile, but naturally you can replace the echo command with any other of your choosing.

while read line; do echo ${line} >> outputfile; done << (cat inputfile)

Useful Additions to .bash_profile

# set xterm title and shell prompt
export PS1="\[\e]2;\u@\H \w\a\e[31;1m\]\u@\H \w\[\e[0m\] "

# ignore duplicate
HISTCONTROL=erasedups:ignorespace

# ignore these commands in history file
HISTIGNORE=l[sla]:cd:pwd:exit:clear


Set Disk Checking to Once per Month

Ubuntu defaults to running a disk check every 30 boots, or once a month (I think). The following command will set it to just check once per month, regardless of how many times you reboot:

sudo tune2fs -c 0 -i 1m /dev/sda1

Search within Selected Files Recursing Through Folders

grep has the -r option to do a recursive search through folders under the specified path. However it won't work in conjunction with wildcards such as *.java. To do searches such as this, you need to use it in combination with find and xargs like so:

find ~/startfolder -type f -name '*.java' -print0 | xargs -0 grep 'searchpattern'

Create a tar.gz Compressed Archive

tar -cvzpf archive.tar.gz sourcedir1 sourcedir2
The -c option means create a new archive, -v is for verbose, -z indicates it should be a gzipped archive, -p means preserve permissions on files, and -f means use the specified archive file.

List contents of a tar.gz Compressed Archive

tar -tzf archive.tar.gz

List All directories containing certain files

find . -type f -name '*.py' |sed 's#\(.*\)/.*#\1#' |sort -u
This will list all directories under the current directory which contain python files.

Flush bash history

The bash shell keeps a record of commands that have been used in the file ~/.bash_history. However the commands used in the current session are not written to this file until you log out of the current session. If you wish to flush the history to this file before logging out, use the following:
history -a
'a' here stands for append - so this is the regular behaviour. If you want, you can use 'w' instead, which will replace the current bash history file with the session history.

List the most recently modified file

ls -lt | grep ^[^dt] | head -1

Switch to a Virtual Terminal from the command line

sudo chvt 1

Display a Unix timestamp in a readable form

date -d @1193144433

Reset WINE (equivalent to a reboot in Windows)

wineboot

Restart the pulseaudio deamon

pulseaudio -k; pulseaudio -D

Restart Apache2 on Ubuntu

sudo /etc/init.d/apache2 restart

View Apache2 error log

gedit /var/log/apache2/error.log

This location is also where apache keeps its access.log file.

Force Disk Check on Next Reboot

sudo touch /forcefsck

Reload Apache2 Config Settings

sudo /etc/init.d/apache2 force-reload

Install Apache2 Module (eg ModRewrite)

sudo a2enmod module_name

eg sudo a2enmod rewrite

Then force Apache to reload config settings as above.

Regenerate the fonts cache:

sudo fc-cache -fv

The default location of system fonts is /usr/share/fonts/. Different font types are grouped into subfolders off here.

Create a symbolic link

ln -s /path/to/target /path/to/linkname

Change the target of an existing symbolic link

ln -s /path/to/target /path/to/linkname --force

If the link already exists, this command will change it so it links to the specified target location. If it doesn't already exist, it will be created as normal.

Copy a file over a network via SSH

ssh -l {login_name} 192.168.0.XXX cat /path/to/target > /path/to/outputfile

Shortcuts for Eclipse IDE

Lower Case: CTRL+SHIFT+Y
Upper Case: CTRL+SHIFT+X

Jump to Matching Bracket: CTRL+SHIFT+P

Copy current line(s) and place them above/below: CTRL+ALT+UP/DOWN
Move current line(s): ALT+UP/DOWN

Comment/uncomment current line(s): CTRL+/

Switch to next/prev tab: CTRL+PgUp/PgDn

Close current file: CTRL+W

Find open file: CTRL+E

Import Gzipped MySQL Database on the command line

gunzip < /path/to/database.sql.gz | mysql -u root -D databasename

The final -D databasename option for the mysql client selects the named database, and is only necessary when the gzipped SQL file doesn't begin with a USE databasename command.

Unix for loop syntax

The following returns every line in all files called build.mk in all subdirectories of the current working directory which contain the text 'bird', along with the name of each file:

for filename in `find .
1 2