Ubuntu Reference Guide

part of the WillPowered Inc. network
© 2011 William Hall <5/> - This site uses HTML5

Introduction\Useful Commands

This part covers basic commands and useful bits.

When typing into the Bash shell, you can use CTRL + U to clear the whole line or CTRL + W to delete the last word. CTRL + C aborts whatever you're doing.

If you use an asterisk to be an alias for all files and you have files starting with a dash, this can be problematic. The solution is to type ./* to essentially escape the dashes so they don't get counted as parameters.

sudo aptitude
A terminal-based package manager. Useful in combination with putty and SSH. See here for more details and special keyboard commands. Your mouse also works for this program, even in a terminal.
sudo aptitude hold package1 package2
Tell apt to stop updating these packages and keep them at their current version.
sudo aptitude search ~ahold
Use aptitudes search to show you a list of held packages.
sudo aptitude changelog package1
Show the changelogs for package1, i.e. see what the update for a package contains before you update it.
sudo blkid
Displays the UUID for all partitions for use in /etc/fstab.
cat /path/to/a/file
Displays the contents of a file.
cat /etc/group
Lists all the groups and their members.
cat /proc/cpuinfo
Lists the vital statistics on your computers processor. Useful for when you do not have physical knowledge of the machine.
cat /var/log/auth.log | grep "something" > /path/to/a/file.txt
Saves all entries from your SSH log containing the word something to a file called file.txt.
cat /var/log/messages | grep "something" > /path/to/a/file.txt
Saves all entries from your kernel messages containing the word something to a file called file.txt. If you tell Iptables to log then it will log to the system messages which is where this comes in handy.
cd /directory
Change the current directory. Two dots like this .. are used for the directory above the current directory.
sudo chkconfig
sudo chkconfig servicename on
See what services are automatically starting with your system and make one start if it isn't already.
sudo systemctl list-unit-files
sudo systemctl enable servicename
The equivalent of the chkconfig commands above in systemd.
complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh
Add this to your .bash_profile or .bashrc file to have an autocompleting ssh prompt. Not the best but it should save you some time. Source.
cp file /directory
Copy file to directory. Use * to copy all files in current directory. Add -r to copy a directory.
crontab -e
env EDITOR=nano crontab -e
env EDITOR=nano crontab -e -u fred
Manage cron jobs. The second line forces the editor to be nano in case it is not the default and you are not inclined to change it. The third line lets you edit another users crontab using the editor of your choice.
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
List cronjobs for all users. See here for more details.
date ; sudo service ntpd stop ; sudo ntpdate -s time.nist.gov ; sudo service ntpd start ; date
This is a series of commands which forcibly synchronise your computer clock to get it back in line. The bonus is it shows you what your computer thinks is the time before and after.
df -h /mounted/partition
Displays free space on a mounted partition or the entire computer if the mounted partition is omitted.
df -h | grep /mounted/partition | cut -c 41-43
Display the free space on a mounted partition as a percentage by cutting out the bumpf.
diff -rqw /path/to/directory/1 /path/to/directory/2 --exclude=stats
Compares the files in both directories and reports those that are different or unique. The q option hides the details. The w option ignores white space and line endings.
dig any domain.com
dig @dns.server.com any domain.com
Get all DNS records for domain.com from your DNS server. Replace any with a type of domain record if you want a more specific search (mx, a, cname, txt, etc, etc). Install dnsutils to use it. The second option queries a specific DNS server.
for i in *.JPG; do mv "$i" "${i/.JPG}".jpg; done
Rename all your .JPG files to .jpg.
dos2unix (also unix2dos)
find . -type f -exec dos2unix {} \;
Don't forget to install it from apt first though. Convert text files with Windows line endings to Unix line endings. Automatically skips binary files such as images. Unix2dos does the opposite.
sudo dpkg -i file.deb
Install a deb file you have downloaded because it isn't in the repo's. Make sure you have installed any dependencies first though (but DPKG will tell you if you have missed a dependency).
du -ch /path/to/directory | grep "total"
Displays how large the directory is in and easily understandable format. Uses grep to cut out the bumpf. For more du and df tricks, check this out.
find options
See here for all sorts of finding options.
find /path/to/directory/ -type f -print0 | xargs -0 grep -i "search phrase"
Search inside the files in the directory for the search phrase
function findinfiles() { find ./ -type f -print0 | xargs -0 grep -i "$1"; }
function findinphpfiles() { find ./ -type f -name "*.php" -print0 | xargs -0 grep -i "$1"; }
Neat bashrc functions
find ~/.thumbnails -type f -atime +7 -exec rm {} \;
Delete nautilus thumbnail cache for the last 7 days. Handy for pictures and videos if you need to refresh their thumbnail.
find . -type d -name ".svn" -exec rm -rf {} \;
Find and remove the .svn directories in the current directory. Useful for those accidental rsyncs...
find directory1 -type f -printf "%P\n" | sort > comparison.txt
find directory2 -type f -printf "%P\n" | sort | diff - comparison.txt
Compares two directories by filenames so you can see what files are in one but not the other. (source)
find ./ -printf '%h\n' | sort | uniq -c | sort -k 1 -n
Counts files in folders, handy for seeing where your inodes went. (source)
find . -type d -empty -delete
Finds and deletes empty directories. Handy tidying up bad merges.
sudo badblocks /dev/hda1 > bad-blocks-hda1
sudo fsck -t ext4 -l bad-blocks-hda1 /dev/hda1
Saves a list of bad blocks on a partition and then feeds that to the file system check to fix the partition. I think.
git rev-list --count master
git rev-list master | wc -l
git log master --pretty=oneline | wc -l
Count commits in a git branch. Replace master with master.. for the difference to HEAD or master..yourbranch to count the commits between branches.
git show --format="%aN <%aE>" a1b2c3
Show the git commit details for the specified commit (or the previous commit if you omit the commit ID). Useful when you are looking up what email and name you are using in git
grep '\.rar$'
Find lines ending in .rar
grep -lr "string" directory/
Search recursively from the root directory specified for files containing the word string and display the names of the files.
grep -v "string"
Show results that don't include string, i.e. a negative or inverted grep.
groups username
Display all the groups that username is a member of.
history | grep 'command'
Pass the terminal history to grep and search it for command. Better than pressing up a million times.
ln -sf /target
Make a symbolic link in your current directory to another directory.
ln -sf /target newname
Makes newname the symbolic link instead of using the same directory name.
chown -h user:group link
Change the ownership of a symbolic link.
ls /directory
Displays the contents of the specified directory. If there is no directory, the contents of the current directory are displayed. Add -l before the directory to display extra information.
lsof -i :22
netstat -antp | egrep ':22|:ssh'
See if your server is listening on port 22. Sometimes you'll have netstat and not lsof...
mysql [database name] [-u] [-p] < database.sql
Insert the contents of an SQL file into a local database. Database name, username and password are optional.
> lserver
> set q=mx
Query a specific DNS server for MX records. By default, nslookup uses your computers DNS server and lists A records.
openssl pkcs12 -export -out cert.pfx -in cert.pem
Include the certificate and key in a pem file and then you can make a pfx for all your favourite microsoft products.
sudo passwd username
Change users password.
sudo passwd -l username
Disable a users login, i.e. if they have left or are a system user, i.e. for apache users, especially when using MPM-ITK.
rsync -e 'ssh' -avl --delete --stats --progress --exclude 'boring' user@computer:/home/user /home/user/backup
rsync -rvt /home/user user@computer:/home/user/backup
Use rsync to backup a directory over ssh, deleting files that have been deleted from the source and excluding the boring directory. Note that rsync must be installed on both destination and source computers.
rsync -v --stats --progress /path/to/source /path/to/dest
Simple rsync within a computer...
rsync -rvt --include="*/" --include="*.php" --exclude="*" --prune-empty-dirs public_html/ phponly/
Use rsync to get all PHP files from a directory, ignoring directories that don't contain PHP files.
Add or include -l to include symbolic links
/usr/sbin/sshd -d -p 23
Runs the SSH daemon in a debug mode so you can capture any error messages, particularly whilst setting up SFTP. You can omit the -p to run it on the main port but only if you stop the original service first.
sudo lshw > specs.txt
Displays entire computer specification, down to the fine detail about everything attached to the motherboard. Saves the content in a text document because the output will be longer than your screen.
Opens any mail you have received, especially from the system if you use apticron.
sudo mount -o force /dev/sda1 /path/to/mount/point
Forcibly mount something as read write I.E. hfs+ file systems from mac computers.
mount -t iso9660 -o loop disc.iso /media/iso
Mounts an iso file into the file system.
mv file1 file2
Move or rename file1 to file2.
openssl sha1 public_html/system/core/CodeIgniter.php
Displays a SHA1 hash of the file in question.
split -l 50000 filename.txt
Breaks down a long file into many smaller files, each with up to 50,000 lines.
su user
Change user in a terminal.
sudo command
Runs command as root. Required to access commands and files above the usual user permissions.
sudo -i
Switch to the root user temporarily using sudo powers.
sudo !!
Sudo Bang Bang. Repeat the last command with sudo powers.
tar cvzf archive.tar.gz directory/
Compress a directory into a .tar.gz file. Check out the ultimate tar command tutorial for more examples and options.
top [then shift+o and p]
Orders processes by swap usage.
tune2fs -l /dev/sda1 | grep created
Tells you when the filesystem was created, useful for judging the age of a setup.
sudo visudo
Edits the sudoers file to change sudo permissions. Use username ALL=(ALL) ALL for passworded access. Use username ALL=NOPASSWD: ALL for unpassworded access
zip -r backup.zip /path/to/folder
zip -r backup.zip /path/to/folder -x /path/to/folder/excludedfolder\* /path/to/folder/excludedfile.ext
Compresses files into a zip file. Needs the zip packages. Note the \* in the exclude path. Add -y to not follow symlinks.
zip -r photos.zip photos/* -x "*x*-*"
Compressing very specific files, the preceeding and concluding asterisks as very necessary.
unzip -l backup.zip
Lists the files in a zip file.
unzip -p backup.zip path/to/file/in/zip/file > /path/to/new/output/file/on/your/computer.txt
Extract a single file from a zip file. The extraction is dumped to STDOUT so you have to manually transfer that output into a new file.
wget remoteurl.com
wget -d remoteurl.com
Simply download a remote url
who -b
Tells you the last time the system was booted.
ls | grep -P --regexp='\.[2-9][0-9].gz$' | xargs sudo rm
Files all filenames ending in greater than .20.gz and pipes to rm. Xargs pipes the input into the command as rm can't handle that itself