OS X and Ubuntu command prompt demystified

Here we'll look deep into a bowel of computing: how to navigate around your file system and how to do basic tasks such as, creating and deleting folders and files, installing packages with apt-get, general terminology, customising the terminal, and lot more.

2014.04.11 Edit: Added some MySQL commands
2014.04.16 Edit: how to change password

Take off the condom

Heck, take of your underpants too! This is how I felt after learning to interact with my computer via command line:

It’s one of these things that seem hard but actually are not (I mean the basics are not hard), learn few commands and tricks and you’ll find it to be much better way of using a computer. I just recently got into command prompt, so I’m no means an expert on it, but here’s some of my notes from the learning process.


Command Line Interface CLI – more or less an umbrella term for all this

[…] is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines).

Shell – the program you’re using

The most generic sense of the term shell means any program that users employ to type commands.

Bash – a type of shell

Bash is a Unix shell […] a free software replacement for the Bourne shell […] it has been distributed widely as the shell for the GNU operating system and as a default shell on Linux and Mac OS X.

Terminal – OS X shell

Terminal (Terminal.app) is the terminal emulator included in the OS X operating system by Apple Inc..

Why would I want to learn that?

  1. There is a whole cosmos of GUI-less handy applications, you have more stuff at your disposal. For me one of the triggers was to be able to use Grunt
  2. Linux and OS X use a lot of the same commands, learn one and you know the other
  3. Servers are mostly Linux, learn to configure it from command line and you can run your own server. Configuring a server is the most ideal way to get comfortable with shell *

OS X Terminal

Terminal is in the Utilities folder inside Applications. Here’s the default look.

Very basic terminal as it comes out the box
Very basic terminal as it comes out the box

Make it prettier and better

I recommend iTerm, it has tabs and bunch of other cool stuff. Some people also prefer TotalTerminal.

The default look is a bit morose, it can be made more interesting easily. Grab the .bash_prompt file from here and just drop it into your home directory, boot iTerm. You can customise the iTerm colours in it’s Setting → Profiles → Colors (⌘ + ,), change the background to your preference. I like to have my text editor white and terminal black, so I can distinguish them easily.

iTerm with a custom `.bash_profile`
iTerm with a custom .bash_profile


You go to a directory with the cd command.

$ cd foldername

Really important thing is to use tab completion.


The above gif explained:

$ cd w<tab>

# Complets into
$ cd web/

# Next dir
$ cd web/be<tab>

# Completes into
$ cd web/beveled-corners

Start by typing few letters of the folder name and then press tab, and it’ll fill the whole name in for you. This is priceless!

If there is more folders wit almost the same name (i.e. config.php and config-sample.php), it wont work of course, then you can double tab to see all the files with the same beginning.

Set of commonly used command

With these you can get pretty far already. The commands work both in OS X and Linux, if not mentioned otherwise.

  • $ character means that it’s a line to be written in command prompt (don’t type the dollar character)
  • # is a comment
  • If there’s nothing, it means it’s an output of a command

Go to a folder

$ cd foldername

List files

$ ls

List also hidden files

$ ls -a

Navigate to a different harddrive, usually only needed on a local computer

$ cd /Volumes/"Some Volume Name Here"

Where are you

$ pwd

Make a folder

$ mkdir foldername

Download a file from internet, super handy!!!!!!11111

$ wget http://www.example.com/path/to/file.fileextension

Open a file in OS X

$ open filename.psd # Opens to Photoshop

Open into a certain app in OS X

$ open -a "Adobe Photoshop 7.0" foo.jpg

Rename a file, mv is actually a move command, we’re kinda moving the file to the same directory

$ mv index.html index-old.html

Move a file

$ mv logo.png image/logo.png

Create a file

$ cat > foo.txt

Print out files contents

$ cat foo.txt

Using apt-get Ubuntu package manager, install packages

$ sudo apt-get install htop # htop lets you list proceses in Linux

Search for packages

$ apt-get search htop

Reinstall package

$ sudo apt-get install --reinstall packagename

Update packages, first update the package list, then the actual packages

$ apt-get update && apt-get upgrade

See this great apt-get cheat sheet.

Now that we have htop intalled, heres how to run it

$ htop

Kill a processe, (read more about killing). First note the PID, use htop for that, then

$ kill -9 PID-HERE

Clear the terminal window Ctrl + l or


Check folder permissions

ls -l directoryname

Change owner

$ chown -R newowner myfolder/

Change owner and group

$ chown -R newowner:newgroup myfolder/

Cahnge group only

$ chown -R :newgroup myfolder/

Switch user, comes handy in server environment

$ su username

Upload a file from your local computer. Here I’m uploading an irssi script from my local Downloads folder into remote autorun direcotory

scp -P 6000 ~/Downloads/UNIBG-autoident.pl joe@example.com:~/.irssi/scripts/autorun

Reboot Ubuntu, rarely needed but not never

sudo reboot now
# Or only a reboot worked just as well

Find your php.ini file

$ php --ini

Move directory content one level up, e.g. remove all folder contents to the directory you currently are in. Gif for clarity:


$ mv myfolder/* .

Even better, you can chain the removal of the now empty directory

sudo mv myfolder/* . && rmdir myfolder/

Login to MySQL shell:

$ mysql -u root -p

Create MySQL DB:

CREATE DATABASE databasename;

Create user:

CREATE USER username@localhost;

MySQL commands are really self explanatory.

Set password for the MySQL user:

SET PASSWORD FOR username@localhost= PASSWORD("passwordhere");

Grant privileges for the user to the newly created DB:

GRANT ALL PRIVILEGES ON databasename.* TO username@localhost IDENTIFIED BY 'password

Refresh MySQL:


Import MySQL DB:

mysql -uroot -p --default-character-set=utf8 dbname < ~/path/dbfilename.sql

Or if you do it from the MySQL shell get rid of the login thing the beginning:

--default-character-set=utf8 dbname < ~/path/dbfilename.sql

List MySQL databases:


List MySQL users:

SELECT User FROM mysql.user;

Get the system time in Ubuntu (clock)

$ sudo hwclock --show

Get the owner and group of a file or folder

$ ls -l /path/to/file
# Or
$ stat /path/to/file

Get status of a service in Ubuntu, mysql in this case

$ service mysql status

Get all installed packages in Ubuntu

dpkg --get-selections | grep -v deinstall

Add user to a group

sudo usermod -a -G www-data username

Add your user to a group, to www-data in this case

sudo usermod -a -G www-data username

Change permission to directory recursively, meaning: this'll change all dirs and sub dirs to 755

$ sudo find . -type d -print0 | xargs -0 chmod 0755

Change permission to file recursively, meaning: this'll change all files in that dir and and sub dirs to 644

$ sudo find . -type f -print0 | xargs -0 chmod 0644

Reload NGingx

$ sudo nginx -s reload

Change currently logged in users password:

$ passwd

Change other user password:

$ passwd username

Restart any service after changing setting, NGinx in this case

$ sudo service nginx restart

nixCraft is great resource on these matters! Askubuntu.com is also priceless.

* There's so much more to it than few commands, though. Hat tip to all sysadmins. Especially people dealing with email servers.

Club-Mate, the beverage → club-mate.fi