A good[ish] website
Web development blog, loads of UI and JavaScript topics
This posts looks into using computer from the command line: how to navigate around your file system and how to do basic tasks such as; creating/deleting folders and files, installing packages with apt-get, general terminology, customizing the terminal, and some more.
It’s one of the things that seem hard, but actually are not (I mean the basics aren’t difficult), learn few commands and tricks and you’ll find it to be much better way of using a computer.
Command Line Interface CLI — an umbrella term:
... 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..
Terminal is in the Utilities folder inside Applications. Here's the default look.
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.
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 with 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.
This section is just a dump of commands basically.
With these you can get pretty far already. The commands work both in Mac and Linux (unless otherwise mentioned).
Nomenclatures:
$
character means that it’s a line to be written in command prompt (don’t actually type the dollar character).#
is a comment.Go to a folder:
$ cd foldername
List files:
$ ls
List also hidden files:
$ ls -a
List as a "list" and show the file sizes in human readable form:
$ ls -lah
Navigate to a different hard drive, usually only needed on a local computer:
$ cd /Volumes/"Some Volume Name Here"
The possible spaces in the directory names can also be escaped with a backslashes:
$ cd /Volumes/Some\ Volume\ Name\ Here
Where are you (pwd
: Print Working Directory):
$ pwd
/path/to/your/current/location
Make a folder (mkdir
: MaKe Directory):
$ mkdir foldername
Make a blank new file:
$ touch some-file.txt
Make a file with content:
$ "your content" > foo.txt
Or based onto another file:
$ cat other-file.txt > foo.txt
Download a file from internet (wget
: World Wide Web Get):
$ wget http://www.example.com/path/to/file.txt
Upload file to a server securely (scp
: Secure Copy):
$ scp ./my-file.zip bob@127.0.0.0:/var/www/my-site
Download file from a server securely (to your current working directory):
$ scp bob@127.0.0.0:/var/www/my-site/my-file.zip ./
Get headers of a website:
$ curl -I "https://clubmate.fi"
Quickly check if an URL redirects:
$ curl -I -s "http://clubmate.fi" | grep Location:
Location: https://clubmate.fi/
The -s
stands for "silent".
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
Move a file:
$ mv logo.png images/logo.png
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
Copy a file:
$ cp index.html index-copy.html
Move directory’s content one level up. Basically this:
$ mv myfolder/* .
Even better, you can chain the removal of the now empty directory with the double ampersand:
sudo mv myfolder/* . && rmdir myfolder/
$ rm my-file.txt
rm: remove write-protected regular empty file ‘my-file.txt’?
# Answer with y or n
Remove a directory:
$ rmdir my-directory/
Remove directory recursively:
$ rm -r my-directory/
Use the force, the "rimraf". Careful with this one, it’s easy to remove too much, it won’t prompt you and the files can’t be recovered:
$ rm -rf my-directory/
Print out the contents of a file (concatenate):
$ cat foo.txt
Edit a file in your chosen text editor:
$ vim foo.txt
$ nano foo.txt
$ vsc foo.txt
Every system comes with its own package manager.
Install packages:
$ sudo apt-get install htop
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.
Update the Homebrew package list:
$ brew update
Install a package:
$ brew install htop
Search for packages:
$ brew search htop
Update a packages:
$ brew upgrade htop
Update all packages:
$ brew upgrade
Show all outdated packages:
$ brew outdated
Kill a processe, (read more about killing). First note the PID, use htop
for that, then run:
$ kill -9 <PID-HERE>
Clear the terminal window:
$ clear
Or Ctrl+k will destroy the current buffer completely, meaning you can’t scroll up anymore.
Check folder permissions:
$ ls -l ./my-dir
Change owner of a file:
$ chown my-file.txt
Change owner of a directory (recursively):
$ chown -R newowner my-dir/
Change owner and group of a directory and all its files:
$ chown -R newowner:newgroup my-dir/
Change group only:
$ chown -R :newgroup my-dir/
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 permissions recursively, but to directories only. This change all directories in the current working directory to 755
:
$ sudo find . -type d -print0 | xargs -0 chmod 0755
This one changes all files to 644
recursively, but doesn’t touch directories:
$ sudo find . -type f -print0 | xargs -0 chmod 0644
Switch user, comes handy in server environment (su
: Switch User):
$ su username
Or switch to root with sudo:
$ sudo -i
Switch back to your original user:
$ exit
Make a new user:
$ sudo useradd bob
Change currently logged in users password:
$ sudo passwd
Change other user password:
$ sudo passwd bob
Reboot a machine, rarely needed but not never:
$ sudo reboot now
# Or only a reboot worked just as well
reboot
Turn the machine off:
$ shutdown
Or halt:
$ shutdown -H
Exit the machine or a user you’re in:
$ exit
Look from the current working directory .
a file named foo-bar.svg
:
$ find . -name foo-bar.svg
Locate your php.ini
file:
$ php --ini
Get the system time in Ubuntu (clock):
$ hwclock --show
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
Reload NGINX:
$ sudo nginx -s reload
Restart any service after changing setting, NGINX in this case:
$ sudo service nginx restart
Comments would go here, but the commenting system isn’t ready yet, sorry.