Basics of Z Shell for Newbie Developers
Tags: beginners, codenewbie, wecoded, womenintech
Published: 2022-03-16 20:43:38

I’ve always been a bit scared of the terminal. It brings up images of a black screen with green text, with a hacker furiously typing away at the keyboard.

The terminal is a powerful tool, and pretty unforgiving of mistakes. If you delete something by accident or mess something up, there’s usually no going back. 

While setting up my laptop for my new job, I was instructed to enter and execute various commands (that looked like jargon to me!) into the terminal. I had very little idea of what I was doing, but thankfully managed to bumble my way through without destroying anything too badly. But this prompted me to learn a bit more about the terminal and Z Shell, the shell used by most MacBooks.

## What is the difference between the terminal and the shell?
The shell is the program that processes the commands we input and outputs the results.

The terminal is like a wrapper that goes around the shell. It allows us to enter commands and it translates the keystrokes for the shell. The terminal supports fonts, colours, etc.

## Basics of using Z Shell
Below are the basic commands used to navigate the file system using Z Shell.

- `cd` moves to your home directory
- `cd Documents` moves to the Documents sub-folder
- If you want to move to a completely different folder (that is, not a sub-folder within your current directory), use the full pathname. For example: `cd /Applications/Rectangle.app`
- Your home page is usually represented by the symbol ~ So if you want to access a folder that is inside your home directory, you can begin with ~ For example: `cd ~/Desktop`
- `cd ..` takes you up one level (to the parent directory)
pwd prints the current working directory (the directory you’re currently in). This is very helpful if you don’t know where exactly you are.
- `ls` lists the contents of the current directory. Note, you can also use this to find out the contents of a directory without actually going into that directory:
`ls ~/Desktop` will show the contents of the Desktop folder.
- You can also add options to the ls command:
`// list all files (including hidden files)
ls -a 
// list files in long format
ls -l
// Note: you can also combine them. For example:
ls -la`

## Viewing and editing files

### Viewing files

`cat Documents/hello` will display the text from the file named hello.

If you try to use the cat command on a document that is not a text file, it may stuff up your terminal. If that happens, use the reset command to fix it: reset

Alternatively, `less Documents/hello` will also show you the content of the file, but with a bit more features. You navigate it using:

- spacebar to go down a page
- b to move up
- q to exit

### Editing files
For editing files, you can use Nano or Vim (both are usually already installed in most systems).

**Using Nano:**
`nano tiny_page` will open the file named tiny page.

The commands for Nano are then shown on screen. The only things to remember is that the ^ symbol refers to the control key, and “WriteOut” means to save!

**Using Vim:**
Vim is a pretty scary looking editor. Mostly because the commands and the way it works seems completely different to every other word editor and almost alien! But, it is also very powerful and has a lot of features. I am not a Vim power user, and have only learnt the very basics so that’s what I’m sharing here.

- `vim tiny_page` will open up the file named tiny page.
- It first opens up in command mode, which means you cannot edit anything in the file. To edit, first enter insert mode by typing i You can then edit as you like.
- To exit insert mode, just press esc key.
- To save, press :w
- And, most importantly, to exit press :q or :q! to exit without saving.

## Opening files

- `open hello` will open the file named hello in using the default application.
- If you’ve configured VS Code to be your default editor for git, you can use `code hello` to open the file named hello in VS Code.
- `open .` will open the current folder.
- `open -a Preview picture.jpg` will open picture.jpg in Preview.
- `open -a Microsoft/ Word hello` will open hello in Microsoft Word.

## Creating files and folders

- `touch hello` will create a file named hello. You can create multiple files in one command too: `touch file1 file2 file3` will create three files; file1, file2, and file3.
- `mkdir` will make a new folder. You can also create multiple folders in one command: `mkdir ~/folder1 ~/folder2` will make two folders (folder 1 and folder 2) inside your home directory.

## Moving and renaming files and folders

- `mv source destination` moves the file or folder from the source folder to the destination folder.
- `mv currentFileName newFileName` can be used to rename files.

## Getting help: man pages
If you want more information about any of the commands, you can bring up the relevant man page by typing man followed by the command you want information about. For example: `man ls`

To navigate around the man page, use:

- spacebar to move down a page
- b to move up
- / to search
- n to go to the next match
- q to quit

## Other tips

- Use the up and down arrow to cycle through previous commands you’ve used.
- Use tab to autocomplete file and folder names. This has the added advantage that there will be no typos in the names.
- `file HelloWorld` will show the type of the file. For example, whether it is a .jpg file or a .pdf file.

These the basic commands that I’ve learnt so far. I’ll keep learning and sharing as I continue this journey as a budding software developer. Thanks for reading!