Basic UNIX/Terminal Commands

Basic UNIX/Terminal Commands

Basic guide on terminal commands

I remember when I first started using the command line, it felt like smoke and mirrors as I didn’t know where I was or what to do. It all seemed very confusing at the beginning but once I got the grip of it I couldn’t go back.

Here is a quick guide on simple terminal commands that can be handy when using a unix server or the terminal on your local machine.

Please note that this is just a quick guide!

Basic file handling on the terminal

ls
Returns a list of the files and folders on the current folder.

mv
Moves a file or renames it if the same folder is specified.
If you want to rename a file from the terminal called foo.php you can rename it by moving it to the same folder using a different name

Example:

 mv foo.php bar.php 

This will rename the file foo.php to bar.php
If you want to move a file to a different folder using the terminal you can just specify the folder on the second argument.

Example:

mv foo.php lib/foo.php

This will move the file foo.php to the lib folder.


cp
Copies a file from the terminal.

Example:

cp foo.php bar.php

If you would list (ls) your files on the current folder you will find both foo.php and bar.php and bar.php will be an exact copy of foo.php


rm
Deletes a file.

Example:

rm foo.php

This will permanently delete a file. You can add -rf if you don’t want to get asked if you are sure.

IMPORTANT: Be careful when using this as any changes cannot be undone.


chmod
Changes the read, write and execute permissions of a file or folder. You can find these on your FTP client under the file permissions information and it might look something like this:

File Permissions on FTP client

Example:

chmod 664 foo.php

This will make the file writable only by it’s owner and read only for everyone else.

I could extend a lot more on the chmod command but I will leave it for another time.

Basic Folder and Directory Terminal Commands

mkdir
Makes a new directory (folder).

Example:

mkdir foo

This will create a new folder called foo on the current directory.


pwd
pwd command on the terminalTells you the where you are at this specific point.

cd
On the previous example you can see the cd command. This command means change directory, it is the way to navigate folders.

Example:

If you want to go to the home folder from the terminal you can just type:

cd ~/

You can also specify the full path of the folder where you want to go.

cd /foo/bar/app/views

This will take you to the folder views inside the folder app and so on and so forth.

If you want to Go up a folder you can type:

cd ..

This means change directory to the parent folder.


open
This command opens the file specified with the default application in charge of opening the current file type.

Example:

At this point Sublime Text is my default text editor for .php files.

open foo.php

Typing this into the terminal will fire up Sublime Text and open foo.php.

This also works with folders.

open ~/

This will open the home directory.
NOTE: This only works on a local machine. There is not a current way of opening remote files this way.


This was a very short guide on some of the most basic unix commands that can be used on the terminal on a local machine or via ssh on a remote one.

Now days I cannot think of working without the terminal. It made my life so much easier as my workflow streamlined.

It really is easier that it looks. Why don’t you give it a go?




Subscribe to our Newsletter