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.
Take off the condom
A command line lets you get intimate with your computer, data and protocols. GUIs are like wearing full body condoms.
— Command Line Magic (@climagic) December 19, 2013
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?
- 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
- Linux and OS X use a lot of the same commands, learn one and you know the other
- 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.
Make it prettier and better
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.
You go to a directory with the
$ 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-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 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 /path/to/your/current/location
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
$ 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
Kill a processe, (read more about killing). First note the PID, use htop for that, then
$ kill -9 PID-HERE
Clear the terminal window
Check folder permissions
ls -l directoryname
$ 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 email@example.com:~/.irssi/scripts/autorun
Reboot Ubuntu, rarely needed but not never
sudo reboot now # Or only a reboot worked just as well reboot
$ 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 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
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
$ sudo nginx -s reload
Change currently logged in users password:
Change other user password:
$ passwd username
Restart any service after changing setting, NGinx in this case
$ sudo service nginx restart
* There's so much more to it than few commands, though. Hat tip to all sysadmins. Especially people dealing with email servers.