Even a master at Linux desktop-fu can find using the command line difficult. But beyond the basic commands to navigate and manage your system there are like arcane spells special incantations that provide new functionality save you time or which are just plain interesting. Here’s a small selection of command-line gems with a focus on the desktop and Ubuntu.
Sudo and packages
You’ve probably read commands to run as root many a time from magazines and this requires using sudo in front of every command. Alternatively if you’re expecting to run many commands run sudo -s and you’ll be logged in as root.
And for those times you’ve run a command and then been told you need root privileges there’s no need to type it out again with sudo in front just run sudo !! and the last command will be repeated with root privileges.
An easy way to see past commands is just to run history. You can then select with the mouse and middle-click to paste. Alternatively combine with grep to search for a command for example: history |grep make.
Adding new repositories to Ubuntu can be done through the Software Sources dialog but then you also need to add the appropriate key file to prevent Ubuntu from bugging you about untrusted sources and you can’t copy and paste it. An easier way to add repositories is with the apt-add-repository command and it’ll do both for you for example: sudo add-apt-repository ppa:rye/ubuntuone-extras. You need to know the PPA first of course which you can browse at https://launchpad.net/ubuntu/+ppas.
Sometimes you’ll find .deb packages built for Ubuntu that aren’t in the Ubuntu repositories. Double-clicking on them will install them from the GUI for the command line use dpkg -i <package>.
See the running processes on your system. To look for a particular program use ps ax |grep program.
Alternatively use pidof to find the process ID (pid) of a program.
kill -9 pid
The kill command ends a single process but for stubborn ones kill -9 should terminate it. To kill multiple programs at once or kill one based on name instead of process ID use killall for example killall firefox.
Got a stubborn GUI program that won’t close? Run xkill and when your cursor changes to a target click on the offending window to zap it and its parent process.
Run a command in the background. Alternatively you can pause an already running program with Ctrl-Z returning you to the command prompt. You can then type ‘bg’ to background it or ‘fg’ to bring it back to the foreground.
View system resources and processes in real time. Press ‘d’ to set a quicker delay and ‘1’ to show load on multiple CPUs. Alternatively install htop (apt-get install htop) for a prettier coloured display.
Disk and filesystem
Quickest way to change to your home directory.
An alternative to ls -la for browsing files and directories.
See all the files opened by a process. Use with grep to search by name or use the -p and -u flags to search by PID and user respectively. You can even show net connections by protocol with -i.
du -h -x –maxdepth=1
Display disk usage for the current directly and below only in human-readable format. Add | sort -nr to then sort the result by size.
Display an overview of free space across all disks in human-readable format.
hdparm -Tt /dev/sda
A basic but quick gauge of hard drive performance.
mount | column -t
Get a clean overview of all mounted filesystems (local network USB connected and so on).
See the kernel log from bootup and any subsequent kernel messages. Use with | less to browse or |grep to search for particular keywords (for example to see driver messages for hardware).
The default text editor for the command line in Ubuntu. This is very handy for editing config files.
Control the starting stopping and management of services. For example service samba restart. Run on its own with a service to see available options.
See the services listening on your system. Use netstat -tupn to see current connections to your machine.
Get your running kernel version. Helpful when checking which kernel is booted.
Get a visual overview of shares on a Windows network.
shutdown -h now
Shut down your machine. Alternatively use reboot to reboot. Sudo or root access assumed.
The power of Linux really shines when you combine commands. This is just a sample of some of the more interesting tricks you can do at the command line.
Make a PDF out of a command’s manual page (which can be much easier to read): man -t command | ps2pdf – filename.pdf
Make it easier to read PDFs out of manual pages.
Force a disk check at the next boot rather than waiting for every 30 reboots (sudo -s first): >/forcefsck
The presence of ‘forcefsck’ in the root of a filesystem will tell Ext2/3/4 to check the use of ‘>’ creates an empty file. The file will be removed automatically.
Back up your hard disk to a remote machine over the network securely and compress it on the fly: dd bs=1M if=/dev/sda | gzip | ssh user@remote.PC.IP.Address ‘dd of=drive_backup.gz‘
Substituting /dev/sda for another disk if necessary.
Check your battery life on the laptop: grep -F capacity: /proc/acpi/battery/BAT0/info
Watch the interrupts on your system in realtime: watch -n.1 ‘cat /proc/interrupts’
Mount a CD-ROM .ISO image file onto a local directory (be sure to mkdir the directory first): mount -o loop CDROM.ISO /mnt/directory
Shutdown a Windows machine over the network (assumes you have an admin account): net rpc shutdown -I Windows.PC.IP.Address -U username%password
You can substitute ‘shutdown’ with ‘shutdown -r’ to reboot it instead.
Download an entire website including internal links images and all: wget -r -p -l0 -e robots=off -U mozilla http://www.website.com
Find out your external IP address: curl icanhazip.com
The Matrix in your console: tr -c “[:digit:]” ” ” < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR=”1;32″ grep –color “[^ ]“
Matrix in your terminal. Because you can.
And for the finale just because you can: watch an ASCII version of Star Wars (yes the movie!) via telnet: telnet towel.blinkenlights.nl