A Survival Guide to Unix (Part 1)

Xìng Voong
5 min readJan 28, 2021

Table of Contents:
I. Introduction
II. So Why Unix Command Line
III. Common Commands

  1. mkdir
  2. cd
  3. vm_stat
  4. echo $PATH
  5. cal -y
  6. man
  7. touch
  8. ls
  9. cat
  10. who
  11. rm
  12. cp

I. Introduction

I have been using Unix Command Line for a while now and I can not stop talking about how useful they are. It saves me so much time not to have to touch my mouse and get lost in my Finder.

Let’s not waste time.

This tutorial is a collection of what I think is useful Linux commands that no matter how comfortable you are with your Mac, you still can use them.

This tutorial assumes you run the command lines in a Mac terminal.

II. So Why Unix Command Line

It is so much faster. It is quicker to write a couple letters or commands and click enter then just look through all menu options.

That single reason is good enough for us to learn them.

III. Common Commands

to start:

  1. Launch your terminal in Mac.
  2. Create a working directory for this tutorial:
    please follow the steps, for now, I will explain later.
    in your terminal, type:
  3. $mkdir unix_tutorial
    $cd unix_tutorial

mkdir

stand for make directory

mkdir [name_directory] 

create a directory named name_directory if it does not exist

cd

stand for change directory


cd [name_directory]

changes the current working directory to name_directory

$mkdir unix_tutorial   
$cd unix_tutorial

what we did above were:
first: we create a directory name unix_tutorial
second: we change the working directory to unix_tutorial.
Some extra tips on cd command
to go back to the previous directory, do:

$ cd ..

to go back to the home directory, do:

$ cd ~

Yay! We have set up our workspace.
Up next, in your terminal, type: vm_stat

unix_tutorial xingvoong$ vm_stat

explanation:
unix_tutorial is our working directory
xing_voong is my user name on my personal laptop
$ the dollar sign is the start of a Unix command
vm_stat is a Unix command

vm_stat

Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 41457.
Pages active: 1746982.
Pages inactive: 1736050.
Pages speculative: 9597.
Pages throttled: 0.
Pages wired down: 509005.
Pages purgeable: 102050.
"Translation faults": 85023185.
Pages copy-on-write: 3372555.
Pages zero filled: 47523657.
Pages reactivated: 1451823.
Pages purged: 822338.
File-backed pages: 623825.
Anonymous pages: 2868804.
Pages stored in compressor: 379743.
Pages occupied by compressor: 151040.
Decompressions: 189312.
Compressions: 969158.
Pageins: 2166764.
Pageouts: 25776.
Swapins: 2560.
Swapouts: 2560.

vm_stat stands for virtual memory statistics.
vm_stat display information about CPU memory and lock I/O

echo $PATH

unix_tutorial xingvoong$ echo $PATH
~/mongodb-macos-x86_64-enterprise-4.4.0/bin:/Users/xingvoong/.gem/ruby/2.6.0/bin:/usr/local/opt/ruby/bin:/usr/local/opt/ruby/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/anaconda/bin:/bin:/sbin:Users/bin:/Users/local/sbin:/Users/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/opt/X11/bin

$PATH is an environment variable.
echo $PATH displays the environment variable that you are currently on.

cal -y

unix_tutorial xingvoong$ cal -y
2021
...
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 1 2 3 4 5
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
30 31
...

cal -y shows the current year calendar
For each command in Unix, there are many options. Options are represented by a dash “-”.
In the above command, -y mean to choose the year option.

man

To get more information and options for a command, use man.
Exp:

$ man vm_stat

to exit out terminal after using man command: control + Z

touch

create a file by touching it

$ touch a_file.txt

or we can create a file using:

> a_file.txt

explanation: the command means that an arrow points to a_file.txt. If a_file.txt already exists, > a_file.txt will clear all the content in it.

in your favorite editor, open a_file.txt, type:
“hello, this is a file I use to learn unix”

ls

stands for list. It lists all the files and directories in the current environment variable.

$ ls
a_file.txt

there is only one file in our current working directory

cat

stands for concatenate. It allows us to:
1: view content of a file

$ cat a_file.txt
hello, this is a file I use to learn unix.

the downside of this cat command is when a file contains so much content, there would be too much to display.
To overcome that, we could use

$ more a_file.txt

2: concatenate two files:

$ touch file2.txt

create a second file for concatenating
in your editor, open file2.txt, type “this is a second file”
to concatenate a_file and file2, do:

$ cat a_file.txt file2.txt
hello, this is a file I use to learn unix.
this is a second fileXings-MacBook-Pro:unix_tutorial xingvoong$

Since I did not have a new line at the end of file2.txt
The outputs are not that pretty.
Go back to file2.txt to add a new line.

To write the output to a new file, simply do:

$ cat a_file.txt file2.txt > file3.txt

the above command writes the content of a_file.txt and file2.text into file3.text

$ ls
a_file.txt file2.txt file3.txt

Now, there are three files in this directory.
to view the content of file3.txt

$ cat file3.txt
hello, this is a file I use to learn unix.
this is a second file

who

displays users who recently logged into the computer

unix_tutorial xingvoong$ who
xingvoong console Jan 9 13:28
xingvoong ttys000 Jan 9 13:51
xingvoong ttys001 Jan 9 21:48

yes, it was me the entire time. Noone hacks my computer. I hope no one hacks yours either

rm

to remove a file
create some files to remove

$ touch a
$ touch b
$ touch c

Now, we will remove them:

$ rm a

remove file a from the current directory

$ rm b c

remove file a and file b from the current directory

create a directory to remove

$ mkdir to_remove
$ rm to_remove
rm: to_remove: is a directory

since to_remove is a directory, we can not remove it using to rm.
To remove a directory and files in that directory, we do:

$ rm -r to_remove

-r means recursive

Now, our working directory look likes this:

unix_tutorial xingvoong$ ls
a_file.txt file2.txt file3.txt

cp

copy <file_a> to <file_b>
create file_a.txt with content: “this is file a”
create file_b.txt with content” “this is file b”

$ cp file_a.txt file_b.txt

to be continue

--

--

Xìng Voong

I’m a writer and an engineer based in San Francisco. I write about education, technology and culture. Checkout my official blog at https://xingvoong.com