The Terminal – Code quicker with Command lines

Whether you’re working in a OS X, Unix, Linux or Windows (from version 10 upwards) environment, terminal is the main way programmers interact with the operating system

Options are used to modify the behaviour of a command. They start with a –   >> -l

ls -l /home/ryan

lS is the command

-l is the option that will modify the behaviour of the command

/home/ryan is an argument

The Shell

Within a terminal, you have a shell. It’s part of the OS and defines how the terminal will behave after running a command. BASH is the most common shell. Even though the new default shell on Mac is zsh. 

Arrows: your first shortcut

When you’ve typed a few commands in the terminal, you can use arrow up/down to find them again. There is no need to re-write them all the time.

Basic Navigation

pwd: print working directory

ls: list directories in your working directory

ls – l: detailed list

ls directoryName : list the content of that directory

ls -l directoryName: show detailed list of the content of that directory

Paths: absolute and relative paths

Absolute paths specify a location (file or directory) in relation to the root directory. 

Always start with a /

Relative paths specify a location (file or directory) in relation to where we currently are in the system. 

ls Documents vs ls / home/john/Documents

(Relative vs Absolute)

Paths shortcuts

~ : home directory

~/Documents is the absolute path for Documents

. : current directory

./Documents is the same as Documents

.. : Parent directory

cd.. : Go up 1 directory

cd : go to home directory

Tab : start typing a path and use tab to prefill the rest. If nothing happens it means there are several possibilities. 


Everything is a file

Linux does not look at extensions. It looks inside a file and then determines what kind of file it is.

file [path] : will tell you type of file it is

Linux is case sensitive

Spaces in names

Spaces are okay in directory names, but because the space usually a sign between 2 line arguments we need to tell the terminal that this “spaced” directory is a directory.

Option 1 quotes: ‘directory name’

Option 2 escape characters: directory\ name

Hidden files and directories

If a file or directory starts with a .  (full stop)  it is hidden. If you rename a file with a full stop in front it will simply make the file hidden, no need for an extra command.

ls -a : To see hidden files ls -a  (add the -a)

ls -la: to see all files including hidden files in a detailed format

Manual Pages

Man + command : will show you an explanation of what a command does

q: to escape the explanation

/term  : search a term

n: next term

File Manipulation

Making directories

mkdir : make a directory

mkdir directoryName: will create directory where you are

mkdir ../directoryName: will create a directory in the parent directory

mkdir ~/directoryName: will create a directory at the root level

mkdir -p Documents/middleName/smallName

This will create the missing parent directories to make sure smallName is in the right place

mkdir -pv : will explain all the different steps

Removing directories

rmdir directoryName: remove the directory if it is empty

rm – r directoryName: remove the directory and its files

rm -ri directoryName: it will ask you to verify directory contents before removing. i means interactive.

Creating and deleting files

touch file : create a file

rm file : removes a file

Copying files

cp source destination: make a duplicate of source and call it destination

Moving files

mv source destination : mv the file source to a new place. You can add a change of name at the same time.

mv source destination/newSourceName

Renaming files and directories

mv name newName: you are basically moving a file to the same place but with a different name.

Vi Text Editor

Vi is a text editor in the terminal. There are 2 modes:

  • Insert (or Input) mode
  • Edit mode

Input mode you will add content to a file

Edit mode you can perform actions like deleting, copying, search and replace etc.

vi file : open the text editor for that file

If the file does not exist, it will create it first.

You always start off in edit mode.

Basic commands

i : To go to input mode 

Esc : go back to edit mode

ZZ : Save and exit

:q! : discard all changes

:w : save file but don’t exit

:wq : save and exit like ZZ

In Vi, if a command starts with :, you need to press on enter for it to execute.

Viewing files. From the terminal

cat nameOfFile: see the content of the file. 

less nameOfFile : like cat but easier to read for large files

Vi Edit mode: basic commands

Arrow keys – move the cursor around

^ (caret) – move cursor to beginning of current line

$ – move cursor to end of the current line

nG – move to the nth line (eg 5G moves to 5th line)

G – move to the last line

w – move to the beginning of the next word

nw – move forward n word (eg 2w moves two words forwards)

b – move to the beginning of the previous word

nb – move back n word

{ – move backward one paragraph

} – move forward one paragraph

:set nu : enable line numbers

Deleting content

x – delete a single character

nx – delete n characters (eg 5x deletes five characters)

dd – delete the current line

dn – d followed by a movement command. Delete to where the movement command would have taken you. (eg d5w means delete 5 words)


u : undo the last action

U : undo all changes to current line


Wildcards allow you to manipulate several files at the same time. 

* : zero or more characters (think regex)

? : single character

[] : range of characters

ls b*  : list all files beginning with the letter b

ls filename/*.txt : every file that has a .txt extension

ls ?i*   : every file that has i as a second letter

ls *.??? : every file with a 3 letter extension

ls [sv]* : every file that begins with s or v

ls *[0-9]* : every file that includes a number digit (think regex)

ls [^a-k]* : every file that does NOT include a letter between a and k (think regex)

ls  *.rc : list all files with the extension .rc

ls *.??? : list all files with 3 letter extensions

ls *[[:upper:]]* : list all files that container one uppercase character

ls ???? : list all files that are 4 characters long

file /path/* : find the file type of all files in /path/

mv path/*.??g path/images/ : move all files with extension ending with g (png, jpg) to the folder /images/


Permissions keep files safe. You can apply rules to make sure they are readable AND/OR writable AND/OR executable.

The following letters will define what can happen to the file. 

r : read

w : write

x : execute

Then you can apply these rules to certain people: owner, group or others.

To view permissions you use the -l after ls. To see the detailed view.

ls -l : see detailed view with permissions

The results will start with 10 characters looking like this :



1st character:

d : directory

– : normal file

character 2 – 4 : 3 characters are permissions for the owner

character 5 – 7 : 3 characters are permissions for the group

character 8-10 : 3 characters are permissions for the others

Change permissions

chmod + permissions + path

Permissions have 3 parts here : who (ugoa) + adding/removing (+ / -) + what (rwx)

chmod g-r file.txt   // remove the write to read the file.txt for the group

Example file:

-rwxr—– file.txt

chmod o+rwx file.txt // will convert to -rwxr–rwx

Set permissions quicker with numbers

Decimal system: base 10 number system 0 – 9

Octal system: base 8 system 0 – 7

Chmod 751 : 

Chmod 240

Chmod 750

1st number is for the owner

2nd number is for the group

3rd number is for others

Read has a value of 4

Write a value of 2

Execute a value of 1

4 + 2 + 1 = 7

The max you can have is 7 ie rwx

So : 

chmod 777 : everybody can do everything

chmod 700 : owner can do everything but thats it

Generally you will want groups and others to at least execute and/or read (711, 751, 750 are common)

751 : owner everything, group read and execute, other can just execute

Permissions for directories

Same letters but applied to directory idea :

r (4): you can do an ls and see all the files of the directory

w (2) : you can create files and directories in this directory

x (1): you can go to the directory 

chmod 400 means the owner can do an ls to see the files but you can’t go there

Ls -ld nameOfDirectory  : show the permission of that directory and NOT the list of files in that directory. 

Directory permissions are NOT also applied to the files inside the directory. 

There is a root use that has more rights than any body. You can run as this root user on Mac by starting your command with sudo. 


Reminder >> cat filename.txt will show the content of the tile


head -6 file.txt  : print the 1st 6 lines

tail -4 file.txt : print the last 4 lines

sort filename : sort the line of the file in alphabetical order

nl filename : show line numbers


wc filename : fill show number of lines, words, characters

wc -l filename : only show lines

wc -lw filename : show lines and words

Cut : reduce data

cut -f 1 -d ‘ ‘ : only show 1 column from our file

-f 1 : which column

-d ‘ ‘ : how do we separate the information, here it’s a space

Sed : steam editor

sed ‘s/searchThis/replacewithThis/g’ filename  >> don’t forget the quotes

// means search and replace. The g means for every instance like in regex. If no g, it just replaces once.


uniq filename.txt // Remove duplicate lines of data

But the duplicate lines need to be one under the other.

tac filename   > the opposite of cat. It will show the content of the file but the last line at the top.

Grep and Regular Expressions


eGrep is a program which will search a given set of data a print every line with that pattern.

egrep ‘house’ filename.txt // find every line with the string ‘house’

egrep -n ‘house’ filename.txt // will add the number of the found line

egrep -c ‘house’ filename.txt // how many lines matched

Regex & Grep

egrep ‘[aeiou]’{2,}’ filename.txt // fine any line with 2 or more vowels

egrep ‘2.+’ filename.txt // any line when there is a 2 or more but not at the end

egrep ‘2$’ filename.txt // the number 2 is the last character on the line

egrep ‘is|go|or’ filename.txt // any line that has either is or go or or

egrep ‘^[A-K]’ filename.txt // any line that begins with A-K

Piping and Redirection

Every command that we run has 3 data streams connected to it.

STDIN : Standard input (data going in)
STDOUT : Standard output (data printed out)
STDERR : Standard error (error messages)

Piping and redirection plays around with all these streams.

: means don’t print on screen , send it to a file

ls > filename // put the list of files & directories in filename. If it doesn’t exist, create it.

You can see the content of this filename by cat filename. It will show ls, but in a normal file list format.

Saving to an existing file with > (delete first)

If you save to an existing file it will delete its content before adding the new content

wc -l filename > outputfile // remove everything in outputfile, add word count + filename as text.

Saving to an existing file with >> (append)

ls >> outputfile // at list of directories to whatever was in outputfile

Redirection from a file with <
Print out information without whats after the <

wc -l < outputfile // print number of lines of outputfile but thats it wc -l < hello.txt > outputfile // put in outputfile, the number of lines of hello.txt (just the number)

Redirecting STDERR

You can output something into an error stream by putting the number before the >

ls -l file1.txt file2.txt 2> errors.txt // whatever error message comes out of this command will be put in errors.txt

ls -l file1.txt file2.txt > filename 2>&1 // this will add error message to file name

Piping with ‘|’
Piping is sending one program to another.

ls | head -3 // show the 1st 3 lines of the output of ls

Left of | : output
Right of | : do something with that output

Adding pipes together

ls | head -3 | tail -1 // its chained, to show top 3 lines and then the last line of the 3 lines

ls | head -3 | tail -1 > myfile // same idea chained and then output the final result in myfile

Ls -l ~ | grep ‘^…..w’ // find all files where groups have writable permission

ls -l /projects/ghosttrail | tail -n +2 | sed ‘s/\s\s*/ /g’ | cut -d ‘ ‘ -f 3 | sort | uniq -c : NO IDEA

Process Management

A program is a series of instructions. When we run a program, the instructions are copied, memory is allocated for everything to run. The running of the program is a process.

top / see the processes currently running

Killing a Crashed process

ps aux | grep ‘firefox’ // Identify the Id of the process

The first number is the id

kill 437 // kill the process with id 437 (sometimes it doesnt work, not strong enough)

Bash Scripting

Anything you can run on the command line you may place into a script.

  1. Create a file with 1st line: #! (the shebang)
  2. Add the interpreter to the 1st line /bin/bash
  3. Add what you want the script to do:
  4. echo This is a message that will present what ls will do:
  5. ls
  6. Make sure you have the right permissions
  7. ls -l nameOfFile
  8. Change permissions
  9. chmod 755 nameOfFile
  10. Run the file, simply by writing it in terminal
  11. ./nameofFile

nameOfVariable = ‘john’
echo Hello $name

Automatic variables run in a script
$0 : The name of the script
$1 – $9 : Any command line given to the script. $1 is the first argument, $2 is the seond
$# : Number of line arguments given to the script
$* : all of the command line arguments

Back ticks : save output to variable
You can save the output of commands to a variable with `

variableName = ls ~/Documents

$variableName will list all files/directories from /Documents

What do you think?

Written by John

Leave a Reply

Your email address will not be published. Required fields are marked *

Manipulating the DOM with Javascript

Learn Git WITH GIt-IT